stivex
stivex

Reputation: 667

ASP.NET 5 (Core 1.0) localization / multilanguage web application

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

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:

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.

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.

Upvotes: 3

Views: 4050

Answers (2)

feradz
feradz

Reputation: 1372

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

Upvotes: 1

iliya
iliya

Reputation: 31

  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.

Upvotes: 0

Related Questions