IViewLocalizer returns invalid language

I am trying to create an ASP.NET Core application that should be available in English and German. My problem IViewLocalizer always returns German text, even if the culture is set to English. How to get the right text for the current culture?

Startup.cs

 public void ConfigureServices(IServiceCollection services) { services.AddLocalization(opt => opt.ResourcesPath = "Resources"); services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { var cultures = new[] { new CultureInfo("en"), new CultureInfo("de") }; app.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en"), SupportedCultures = cultures, SupportedUICultures = cultures }); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } 

HomeController.cs

 public class HomeController : Controller { public IActionResult Index() { return View(); } } 

Index.cshtml

 <!DOCTYPE html> @using Microsoft.AspNetCore.Mvc.Localization; @inject IViewLocalizer Localizer <html> <body> <h1>@Localizer["Hello, World!"]</h1> <ul> <li>CurrentCulture: @System.Globalization.CultureInfo.CurrentCulture</li> <li>CurrentUICulture: @System.Globalization.CultureInfo.CurrentUICulture</li> </ul> </body> </html> 

The resource file is located in Resources\Views.Home.Index.de.resx

Expected Result:

 Hello World!

 CurrentCulture: en
 CurrentUICulture: en

Page output:

 Hallo Welt!

 CurrentCulture: en
 CurrentUICulture: en

Request Header:

 GET / HTTP/1.1 Host: localhost:61904 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8,es;q=0.6,de;q=0.4 
+8
c # asp.net-core localization asp.net-core-mvc
source share
2 answers

If anyone is having this problem with an ASP.NET 1.1 kernel or newer, try adding <NeutralLanguage>YOUR_DEFAULT_LANGUAGE</NeutralLanguage> to your .csproj as follows:

 <PropertyGroup> <TargetFramework>netcoreapp1.1</TargetFramework> <ApplicationIcon /> <Win32Resource /> <NeutralLanguage>en</NeutralLanguage> </PropertyGroup> 

He solved my problem and now it works correctly.

0
source share

I had the same problem since yesterday. It seems that LocalizationProvider cannot set the correct culture. After implementing a custom LocalizationProvider, IViewLocalizer began to work fine for Views and ViewComponents .

I also had to implement the language in the URL as follows:

http://somedomain.com/<2-letter-iso-language-name>/controller/action

Here is what I did:

Startup.cs

 public static string defaultCulture = "uk-UA"; public static List<CultureInfo> supportedCultures { get { return new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("uk-UA"), new CultureInfo("de-DE") }; } } 

In the ConfigureServices method, I added CultureToUrlActionFilter() . It extracts the 2 letter language code from the url and sets the correct CultureInfo . If Url contains an invalid culture code, it redirects to the default culture.

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

In the Configure method, I configured a custom localization provider and inserted it as the first provider in the queue:

 var options = new RequestLocalizationOptions() { DefaultRequestCulture = new RequestCulture(defaultCulture), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }; options.RequestCultureProviders.Insert(0, new UrlCultureProvider()); app.UseRequestLocalization(options); 

If the URL does not contain the culture code after the domain name, I am redirecting to http://somedomain/<2-letter-culture-code> (by default). Urls must always indicate culture.

Routes

 app.UseMvc(routes => { routes.MapRoute("home_empty", "", defaults: new { culture="", controller = "Home", action = "Redirect" }); routes.MapRoute("home", "{culture}", defaults: new { controller = "Home", action = "Index" }); 

I redirect it to HomeController, Redirect action

HomeController.cs

 public IActionResult Redirect() { return RedirectToAction("Index", new { culture = Startup.defaultCulture }); } 

UrlCultureProvider.cs

 using Microsoft.AspNetCore.Localization; using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using System.Globalization; namespace astika.Models { public class UrlCultureProvider: IRequestCultureProvider { public Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext) { if (httpContext == null) throw new ArgumentNullException(nameof(httpContext)); var pathSegments = httpContext.Request.Path.Value.Trim('/').Split('/'); string url_culture = pathSegments[0].ToLower(); CultureInfo ci = Startup.supportedCultures.Find(x => x.TwoLetterISOLanguageName.ToLower() == url_culture); if (ci == null) ci = new CultureInfo(Startup.defaultCulture); CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = ci; var result = new ProviderCultureResult(ci.Name, ci.Name); return Task.FromResult(result); } } } 

CultureToUrlActionFilter.cs

 using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Routing; using System; using System.Globalization; namespace astika.Models { public class CultureToUrlActionFilter : IActionFilter { public void OnActionExecuted(ActionExecutedContext context) { } public void OnActionExecuting(ActionExecutingContext context) { bool redirect = false; string culture = context.RouteData.Values["culture"].ToString().ToLower(); redirect = String.IsNullOrEmpty(culture); if (!redirect) { try { CultureInfo ci = new CultureInfo(culture); redirect = Startup.supportedCultures.FindIndex(x => x.TwoLetterISOLanguageName.ToLower() == culture) < 0; } catch { redirect = true; } } if (redirect) { CultureInfo cid = new CultureInfo(Startup.defaultCulture); context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { culture = cid.TwoLetterISOLanguageName, controller = "Home", action = "Index" })); } } } } 

Resx files are located in the same folder /Resources/Views.Home.Index.en.resx /Resources/Views.Home.Index.de.resx /Resources/Views.Shared.Components.Navigation.Default.en.resx , etc. .

+4
source share

All Articles