Force locale with Asp.Net Core

前端 未结 3 2120
不知归路
不知归路 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].

提交回复
热议问题