chemitaxis
chemitaxis

Reputation: 14889

ASP.NET 5 (MVC 6) - Resources Localization

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!!

Upvotes: 8

Views: 3688

Answers (1)

chemitaxis
chemitaxis

Reputation: 14889

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.

Upvotes: 5

Related Questions