3

I have developed my first ASP.NET 5 web application with this characteristics:

  • ASP.NET 5 (Core 1.0)
  • MVC 6
  • EF 7

I'm trying to translate it to multilanguage and that the user could be able to change the language with a selector, but I'm not able to achieving.

I had developed ASP.NET 2.0/3.5 projects before, and I remember that the texts were into resource (.resx) files or into an XML file. But now, as a lot of things that has changed recently in the new ASP, I think the localization also has changed.

I have found very little information about it, and that I have found, doesn't help me enough.

In the official support website doesn't explain this topic yet (docs.asp.net).

I have found an GitHub code sample, and interesting explanation in this website.

But I'm no be able to linking concepts. I'm going to explain the steps that I have done.

I have created a "Resource" folder, into my ASP.NET project. Into this folder, I have created some (.resx) files. The name of this files follows the structure:

  • Controllers.NameOfController.ca-ES.resx
  • Views.NameOfView.ca-ES.resx
  • Models.NameOfModel.ca-ES.resx

In my Startup.cs file, I have defined the following code to set which folder are the text resources:

        public void ConfigureServices(IServiceCollection services)
    {
       ...
        services
            .AddMvc()
            .AddViewLocalization(options => options.ResourcesPath = "Resources")
            .AddDataAnnotationsLocalization();
       ...
   }

In the same file, in the Configure method, I have defined:

            app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
                //template: "{culture?}/{controller}/{action}/{id?}", //we define the structure of the routes
                //defaults: new { culture = "ca", controller = "Home", action = "Index" }); //we define the default values
        });

And also in the same function, the supported cultures:

        //To set the localization configuration
        List<CultureInfo> supportedCultures = new List<CultureInfo>();
        supportedCultures.Add(new CultureInfo("ca-ES"));
        supportedCultures.Add(new CultureInfo("es-ES"));

        List<CultureInfo> supportedUICultures = new List<CultureInfo>();
        supportedUICultures.Add(new CultureInfo("ca-ES"));
        supportedUICultures.Add(new CultureInfo("es-ES"));               

        RequestLocalizationOptions requestLocalizationOptions = new RequestLocalizationOptions();            
        requestLocalizationOptions.SupportedCultures = supportedCultures;
        requestLocalizationOptions.SupportedUICultures = supportedUICultures;            

        RequestCulture defaultRequestCulture = new RequestCulture("ca-ES");
        app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture);

And now is the point that I'm lost.

  • I have read, that I have to develop an controller class that beacome the responsible to manage the localization. I'm right?
  • I have read, that exists multiple ways to define the language that I have to display: from the query string, from a cookie, from the Accept-Language HTTP header, from the DefaultRequestCulture property of the RequestLocalizationOptions class, from the thread culture. I don't know if I have to consider all of this options or one is enough.
  • .resx are active? Are they the good solution? Can I use it in ASP :NET 5?
  • How I can do a selector to commute the language?

EDIT:

I have checked the documentation again, and I have seen in the comments that Rick Anderson has been writing an entry. I haven't read it in calm yet, but I share with you if somebody is interested about this topic.

5
  • Sounds like it is still a work in progress, docs.asp.net/en/latest/fundamentals/localization.html Commented Feb 15, 2016 at 13:37
  • 1
    Thank you @LexLi for your comment! I visited this link before. I think that they are saying that they don't have the documentation ready about this topic yet, but I don't think that the localization isn't ready to use in ASP .NET 5. Commented Feb 16, 2016 at 15:27
  • @stivex check my answer ;) stackoverflow.com/a/35494177/1147273 Commented Feb 18, 2016 at 23:00
  • Thank you @chemitaxis for your example! I don't really understand your example. I'm imagining that you also created a custom way to achieve it. It seems that you are using a dictionary to get the strings. I'm right? The temporal solution that I have done is making my custom classes to achieve this point. I know that it isn't the best solution, but is a temporal solution. I have created a class that represents an entry DicResources.Add(keyResource, catalanString, spanishString) another class that fills a collection of this type of items, and also, has a method to retrieve the strings. Commented Feb 29, 2016 at 15:55
  • Hi @stivex I have done using the official ASP.NET localization provider, the custom filter is just an example... What do you dont understand? :) Commented Feb 29, 2016 at 19:43

2 Answers 2

1

I have reticently done an example which demonstrates all aspects of localization in ASP.NET 5 (ASP.NET Core 1.0). You can download it and have a look https://github.com/feradz/ASPNetCoreLocalization/wiki. Download and try it.

Answering your questions:

  1. You don't need any controller class that will be responsible to manage the localization.
  2. There are different ways to set the default language. The example project demonstrates using cookie
  3. I didn't understand this question.
  4. The example project has a language selector.

The example demonstrates the following features:

  1. Localization with IStringLocalizer
  2. Localization with IHtmlLocalizer
  3. Localization with IViewLocalizer
  4. Localization using shared resource file
  5. Localization using per class controller resource file
  6. Localization of error messages for a view model
  7. Localization of the Display attribute of a model property using prior ASP.NET Core 1.0 resources
  8. Localization for enums and enum elements using EnumHelper
  9. Localization for Views e.g. View.cshtml View.es-ES.cshtml
  10. Example of switching culture using cookie and select input CookieRequestCultureProvider
Sign up to request clarification or add additional context in comments.

Comments

0
  1. In the ConfigureServices(...) put the command

    services.AddLocalization(options => options.ResourcesPath = "Resources");

  2. It is very important that the command

    app.UseRequestLocalization(...)

    must be before all other commands in the method Configure(...). Otherwise localization just not works.

Maybe that will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.