Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,

后端 未结 6 967
清酒与你
清酒与你 2020-12-01 10:32

I\'m going mad. I just want the culture used in the entire Asp.net core application to be set to \"en-US\". But nothing seems to work. Where to I set the culture for the ent

6条回答
  •  春和景丽
    2020-12-01 10:54

    Localization is configured in the Startup.ConfigureServices method:

    CultureInfo[] supportedCultures = new[]
               {
                new CultureInfo("ar"),
                new CultureInfo("fa"),
                new CultureInfo("en")
            };
    
            services.Configure(options =>
            {
                options.DefaultRequestCulture = new RequestCulture("ar");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
                options.RequestCultureProviders = new List
                    {
                        new QueryStringRequestCultureProvider(),
                        new CookieRequestCultureProvider()
                    };
    
            });
    

    Startup.Configure method

     app.UseRequestLocalization();
    

    then UseRequestLocalization initializes a RequestLocalizationOptions object. This should be placed atleast before your UseMvc call

    Change Culture:

    [HttpPost]
    public IActionResult SetLanguage(string culture, string returnUrl)
    {
        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new     RequestCulture(culture)),
        new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );
    
        return LocalRedirect(returnUrl);
    }
    

    Current language:

    var currentLanguage = HttpContext.Features.Get().RequestCulture.Culture.Name;
    

提交回复
热议问题