14

I've been trying to wrap my head around on how best to implement resource files for multiple languages in MVC6 but due to the changes with every release has me a bit confused on how to actually implement them properly. What is required and what are the constraints?

A few articles I've looked at:

https://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/

MVC 6 : how to use RESX files?

http://pratikvasani.github.io/archive/2015/12/25/MVC-6-localization-how-to/

I'm trying to set up resource files so that I have English and German available to my users, which would either be based on browser settings or a personal setting within their account.

What would be the best way of achieving this?

Thanks in advance!

Edit:

So as per the article, I've added the following code to Startup.cs:

        services.AddLocalization(options =>
                options.ResourcesPath = "Resources");
        services.AddMvc()
                .AddViewLocalization(Microsoft.AspNet.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
                .AddDataAnnotationsLocalization();

        var supportedCultures = new[]
        {
            new CultureInfo("de-DE"),
            new CultureInfo("en-US")
        };

        //Set Default Localization Culture
        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        }, new RequestCulture(new CultureInfo("en-US")));

Then within the Resources folder I created new resx files with file names:

  • "Views.Shared._LocalizationTest.en-US.resx"

  • "Views.Shared._LocalizationTest.de-DE.resx"

And my partial view looks like:

@using Microsoft.AspNet.Localization
@using Microsoft.AspNet.Mvc.Localization
@inject IViewLocalizer Localizer

<div>
     @Localizer["TestString"]
</div>

I still seem to be missing something as I am getting "TestString" to show instead of "Test String" for English or "German: Test String" (as per my resource files).

Any ideas?

Community
  • 1
  • 1
Reosoul
  • 197
  • 8
  • Did you figure out something ? I have also the similar kind of problem, see my question : http://stackoverflow.com/questions/42712326/localizaion-using-resource-file-in-net-core-for-different-culture – Herin Mar 10 '17 at 10:39
  • @RickAndMSFT The link is broken. – Marcel Oct 06 '17 at 16:09
  • See my article https://docs.microsoft.com/aspnet/core/fundamentals/localization – RickAndMSFT Oct 10 '17 at 02:36

1 Answers1

2

The ASP.NET core default approach is to NOT have your default language strings in a resource file, but just have them wrapped in code. So you could write the app using English strings wrapped in the localizer, and have one German resource file. See my article https://docs.microsoft.com/aspnet/core/fundamentals/localization

RickAndMSFT
  • 16,022
  • 5
  • 50
  • 66
  • 4
    Is it really such an good idea? I mean it's convenient when you start out with a small app, but sounds pretty error prone to me. I mean, what if you change the punctuation in your default language, then you'll have to go and replace the "key" in all other localization files because otherwise your localized variable won't be found anymore in the other localization files? – Tseng Apr 22 '16 at 06:38
  • That article definitely helps but I have similar thoughts to Tseng. For example, English may be my strong suit, localization happens remotely, yet the default language for the site should be German. – Reosoul Apr 27 '16 at 03:04
  • I agree with Reosoul. Right now, I'm trying to build an asp.net core mvc website with default Spanish and supported English. However, it does not work with strings that wrapped in code. It always shows the strings from the English.resx files. – SherleyDev Aug 14 '16 at 14:56