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

后端 未结 6 935
清酒与你
清酒与你 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:36

    This is what solves it for me:

    Setting the following in StartUp.Configure

    var cultureInfo = new CultureInfo("en-US");
    cultureInfo.NumberFormat.CurrencySymbol = "€";
    
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
    
    0 讨论(0)
  • 2020-12-01 10:37

    Only configuring in startup didn't work for me

     services.Configure<RequestLocalizationOptions>(options =>
                {
                    var ksCultureInfo = new CultureInfo("sq");
                    var enCultureInfo = new CultureInfo("en");
                    var srCultureInfo = new CultureInfo("sr");
    
                    ksCultureInfo.NumberFormat.NumberDecimalSeparator = ".";
    
                    var supportedCultures = new[]
                    {
                        ksCultureInfo,
                        enCultureInfo,
                        srCultureInfo
                    };
    
                    options.DefaultRequestCulture = new RequestCulture(culture: enCultureInfo, uiCulture: ksCultureInfo);
                    options.SupportedCultures = supportedCultures;
                    options.SupportedUICultures = supportedCultures;
                    options.RequestCultureProviders = new List<IRequestCultureProvider>
                    {
                        new QueryStringRequestCultureProvider(),
                        new CookieRequestCultureProvider()
                    };
                }); 
    

    I added jquery globalize validation plugins: Then you need to use Globalize with jquery-validation-globalize plugin. Saw this here

    Now it works as expected.

    0 讨论(0)
  • 2020-12-01 10:45

    A bit late but here is what worked for me :

    var defaultDateCulture = "fr-FR";
    var ci = new CultureInfo(defaultDateCulture);
    ci.NumberFormat.NumberDecimalSeparator = ".";
    ci.NumberFormat.CurrencyDecimalSeparator = ".";
    
    // Configure the Localization middleware
    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture(ci),
        SupportedCultures = new List<CultureInfo>
        {
            ci,
        },
        SupportedUICultures = new List<CultureInfo>
        {
            ci,
        }
    });
    
    0 讨论(0)
  • 2020-12-01 10:45

    this worked for me

     Response.Cookies.Append(
             CookieRequestCultureProvider.DefaultCookieName,
             CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(lang)),
             new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
            );
    
    0 讨论(0)
  • 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<RequestLocalizationOptions>(options =>
            {
                options.DefaultRequestCulture = new RequestCulture("ar");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
                options.RequestCultureProviders = new List<IRequestCultureProvider>
                    {
                        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<IRequestCultureFeature>().RequestCulture.Culture.Name;
    
    0 讨论(0)
  • 2020-12-01 10:59

    Your code looks all fine. The issue is your call to

    app.UseRequestLocalization();
    

    Needs to happen before your call to

    app.UseMvc();
    

    The reason your breakpoint is never hit is because it never goes that far. UseMVC completes the request and returns the result. Remember, Middleware happens in order and any one of the middleware can shortcircuit the process and halt processing going any further.

    0 讨论(0)
提交回复
热议问题