Force locale with Asp.Net Core

一个人想着一个人 提交于 2019-12-03 17:22:46

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

using System.Globalization;

CultureInfo.CurrentCulture = new CultureInfo("sv-SE");

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].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!