Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 (MVC 6) - Resources Localization

Tags:

asp.net-core

I have spent about one week trying to understand how Localization is going to work in ASP.NET Core 1.0. I have tested a lot of options, but I can't make it working.

I have read about the bug in Visual Studio, I have read all articles about how it's working right now (Article1, Article2, Article3) and I have check and tested all about the example in the Official GitHub Repository.

My Goal:

I just want to make it works like I did in ASP.NET MVC 5.

I have configured my Startup.cs like this:

Configure Section:

var requestLocalizationOptions = new RequestLocalizationOptions
            {
                // Set options here to change middleware behavior
                SupportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en-US"),
                    new CultureInfo("es-ES")
                },
                SupportedUICultures = new List<CultureInfo>
                {
                    new CultureInfo("en-US"),
                   new CultureInfo("es-ES")

                }
            };

            app.UseRequestLocalization(requestLocalizationOptions, defaultRequestCulture: new RequestCulture("en-US"));

Configure Services Section:

// Add MVC services to the services container.
            services
                .AddMvc()
                .AddViewLocalization(options => options.ResourcesPath = "Resources")
                .AddDataAnnotationsLocalization();

In my folder Resources, I have my .resx files. I have copied it from the official example, but no way... No errors, just not working.

If I test the Localization example of the official Repo, it works. But I can't modified to adapt to MVC 6.

I have created a repository on GitHub for my code and test it. (https://github.com/chemitaxis/Localization.StackOverflow)

Can someone please help me? I think a lot of people is having these problems.

Thanks!!

like image 407
chemitaxis Avatar asked Feb 16 '16 12:02

chemitaxis


People also ask

How ASP Net applications are localized?

App localization involves the following: Make the app's content localizable. Provide localized resources for the languages and cultures you support. Implement a strategy to select the language/culture for each request.

What is MVC localization?

Localization is the process of adapting software to meet the requirements of local markets and different languages. You can change the messages that are displayed in the Telerik UI for ASP.NET MVC helpers by including an additional script file in the document.

What is resource localization?

Localization is the process of translating an application's resources into localized versions for each culture that the application will support.

What is Localisation in asp net?

Localization, on the other hand, is the process of customization to make our application behave as per the current culture and locale. These two things go together. To do globalization we need to use Resource Files.


1 Answers

Ok, I solved it... I will update my example on GitHub tomorrow.

I have created a _ViewImports, and add it:

@addTagHelper "*, Microsoft.AspNet.Mvc.TagHelpers"
@using System.Threading.Tasks
@using AspNet5Localization
@using AspNet5Localization.Resources
@using Microsoft.AspNet.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<AmazingResource> SR

After, I have created a HomeController and Views/Home/Index.cshtml file.

Like I have injected in my Views in _ViewImports the IStringLocalizer SR I can use it in my Razor Views using just:

@SR["Name"]

I don't know if it the best way to do that, but it works. If someone can explain the best way to do that, please answer this question.

Complete solution working: https://github.com/chemitaxis/Localization.StackOverflow

Thanks.

like image 122
chemitaxis Avatar answered Oct 21 '22 18:10

chemitaxis