Force locale with Asp.Net Core

前端 未结 3 2113
不知归路
不知归路 2021-02-10 01:17

I\'m having some odd issues with a web application written using Asp.Net Core 1.1, using the full .Net Framework v4.6.2.

I want to force the application to use a swedish

相关标签:
3条回答
  • 2021-02-10 01:34

    Take a look at Microsoft ASP.NET Core Docs - Globalization and localization.

    4) Added RequestLocalization to Configure(...) in Startup.cs

    From docs:

    The current culture on a request is set in the localization Middleware. The localization middleware is enabled in the Configure method of Startup.cs file.

    Note, the localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvc()).

    5) In my controller constructor setting the current threads culture

    6) In my controller actions setting the current threads culture

    Instead of setting in the constructor or in every action, try set the culture at OnActionExecuting filter. Here is an example code:

    public class InternationalizationAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var cultureInfo = CultureInfo.GetCultureInfo("sv-SE");
    
            Thread.CurrentThread.CurrentCulture = cultureInfo;
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
        }
    }
    

    In your controller class, put this: [Internationalization].

    0 讨论(0)
  • 2021-02-10 01:45

    I had a similar issue with ASP Net Core 3.0. The site hosting was in a different country causing issues with formats.

    I added the following to the startup:

    using Microsoft.AspNetCore.Localization;
    using System.Globalization;
    

    in ConfigureServices:

    services.AddLocalization();
    

    in Configure:

    var supportedCultures = new[]{
        new CultureInfo("en-US")
    };
    app.UseRequestLocalization(new RequestLocalizationOptions {
        DefaultRequestCulture = new RequestCulture("en-US"),
        SupportedCultures = supportedCultures,
        FallBackToParentCultures= false
    });
    CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
    
    0 讨论(0)
  • 2021-02-10 01:55

    Did you try setting the CultureInfo.CurrentCulture property in Program.cs:

    using System.Globalization;
    
    CultureInfo.CurrentCulture = new CultureInfo("sv-SE");
    
    0 讨论(0)
提交回复
热议问题