The localization language does not change. CurrentCulture is reset to the default value somewhere. Net Core 6.

Volk Volk 571 Reputation points
2022-08-30T08:18:03.58+00:00

Hi!

I have created a language change on my site, but it is reset somewhere at the stage between HomeController and LanguageService. I have tried to solve this problem in different ways, but no changes.

Here is the code for adding a service, a model, and screenshots at breakpoints.

Why if I change the language, it changes in the HomeController, but is immediately reset when the View requests a translation from the localization?
Everything seems to be working as it should, but why does the language reset itself?

By default, de-DE, when changing to en-En in HomeController, it changes as expected, but immediately when updating the View page when accessing localization (public LocalizedString Getkey(string key)) , the language is again magically reset to the default de-DE.

services.AddControllersWithViews();  
  
            services.AddSingleton<LanguageService>();  
            services.AddLocalization(options => options.ResourcesPath = "Resources");  
  
            services.AddMvc()  
               .AddViewLocalization()  
               .AddDataAnnotationsLocalization(options =>  
               {  
                   options.DataAnnotationLocalizerProvider = (type, factory) =>  
                   {  
                       var assemblyName = new AssemblyName(typeof(ShareResource).GetTypeInfo().Assembly.FullName);  
                       return factory.Create("ShareResource", assemblyName.Name);  
                   };  
               });  
  
            services.Configure<RequestLocalizationOptions>(  
                options =>  
                {  
                    var supportedCultures = new List<CultureInfo>  
                        {                             
                            new CultureInfo("de-DE"),  
                            new CultureInfo("en-En")  
                        };  
  
                    options.DefaultRequestCulture = new RequestCulture(culture: "de-DE", uiCulture: "de-DE");  
  
                    options.SupportedCultures = supportedCultures;  
                    options.SupportedUICultures = supportedCultures;  
  
                    options.RequestCultureProviders.Insert(0, new QueryStringRequestCultureProvider());                     
                });  



using Microsoft.Extensions.Localization;  
using System.Globalization;  
using System.Reflection;  
  
namespace Project.Models  
{  
    public class LanguageService  
 {  
        private IStringLocalizer _localizer;  
  
        public LanguageService(IStringLocalizerFactory factory)  
        {  
            var type = typeof(ShareResource);  
            var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);  
            _localizer = factory.Create("SharedResource", assemblyName.Name);  
        }  
  
        public LocalizedString Getkey(string key)  
        {  
            CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;  
            CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture;      
            return _localizer[key];  
        }  
    }  
}  

236071-a.png

236072-b.png

VIEW  
  
@inject LanguageService language  
  
<li><a asp-area="Customer" asp-controller="Home" asp-action="Catalog">@language.Getkey("catalog")</a></li>  
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,197 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Volk Volk 571 Reputation points
    2022-08-31T12:09:32.403+00:00

    While I was creating a separate project and localizing the problem, I discovered why this error occurs.

    Localization stopped working because of this line in Startup-->Configure(): app.UseCookiePolicy();

    I commented out this line and immediately everything worked as it should.
    I hope this line will not break the work of the project anywhere, but the first tests showed that everything is fine.
    Now you can also emulate this problem, if necessary.

    Thank you all, the question is closed! :)

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
            {  
                if (env.IsDevelopment())  
                {  
                    app.UseDeveloperExceptionPage();  
                }  
                else  
                {  
                    app.UseExceptionHandler("/Home/Error");  
                    app.UseHsts();  
                }  
      
                var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();  
                app.UseRequestLocalization(locOptions.Value);  
      
                app.UseHttpsRedirection();  
                app.UseStaticFiles();  
      
                //app.UseCookiePolicy(); // <------------------ Error!  
                  
                app.UseRouting();  
                app.UseAuthorization();  
      
                app.UseEndpoints(endpoints =>  
                {                  
                    endpoints.MapControllerRoute("areas", "{area=Customer}/{controller=Home}/{action=Index}/{id?}");                  
                });  
            }  
    
    0 comments No comments