locked
Why I can't change the default language of the website in asp.net core? RRS feed

  • Question

  • User1052024640 posted

    I set this in the Configure&ConfigureServices:

     public void ConfigureServices(IServiceCollection services)
            {
                services.Configure<CookiePolicyOptions>(options =>
                {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
                });
    
                services.AddScoped<Global>();
                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
                services.Configure<GzipCompressionProviderOptions>(options => options.Level = System.IO.Compression.CompressionLevel.Optimal);
                services.AddMvc()
                    .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix,
                    options => options.ResourcesPath = "Resources");
                services.AddLocalization(options => options.ResourcesPath = "Resources");
            }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    else
    {
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
    }
    
    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    
    var SupportedCultures = new List<CultureInfo> {
    new CultureInfo("en"),
    new CultureInfo("zh-Hans"),
    new CultureInfo("zh-Hant")
    };
    var options = new RequestLocalizationOptions
    {
    DefaultRequestCulture = new RequestCulture("zh-Hans"),
    SupportedCultures = SupportedCultures,
    SupportedUICultures = SupportedCultures
    };
    app.UseRequestLocalization(options);
    var requestProvider = new RouteDataRequestCultureProvider();
    options.RequestCultureProviders.Insert(0, requestProvider);
    app.UseStaticFiles();
    
    app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
    
    
    
    app.UseMvc(routes =>
    {
    routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}/{id?}");
    });
    }

    I have set the 

    DefaultRequestCulture = new RequestCulture("zh-Hans")

    However. the website works well but the default language of the website is still English(en) but not Chinese(zh-Hans) yet.

    Why it turns out to be this?

    Wednesday, July 3, 2019 11:10 AM

Answers

  • User1052024640 posted

    I found the problem that I did not use an IViewLocalizer but ViewData.

    I fixed it and it works.

    Thank you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 2:29 PM

All replies

  • User753101303 posted

    Hi,

    The default language configured in your browser is English ?

    Keep in mind that this value is used only if ASP.NET doesn't find first a supported language from the route (added by you), the query string, a cookie or the browser preferred languages (the last 3 being default providers).

    Wednesday, July 3, 2019 11:35 AM
  • User1052024640 posted

    No, the default language of my browser is Chinese(zh-hans,Chrome ver 75.0.3770.100).

    Wednesday, July 3, 2019 2:14 PM
  • User753101303 posted

    To be on the safe side I meant the preferred language configured in the browser not the UI language of the browser:
    https://www.lifewire.com/change-default-languages-in-google-chrome-4103615

    And you cleared cookies since you changed the default language? If it really doesn't take this value from any of those four sources, it would mean that for some reason it ends up in using the first item from SupportedCultures ?

    Wednesday, July 3, 2019 2:25 PM
  • User1052024640 posted

    I found the problem that I did not use an IViewLocalizer but ViewData.

    I fixed it and it works.

    Thank you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 3, 2019 2:29 PM