Get the current culture in a controller asp.net-core

前端 未结 5 915
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 01:26

I have setup the cultures for my views and changing the culture in a controller but I can\'t seem to find how to know what culture I\'m currently using in a controller, I\'m loo

5条回答
  •  一整个雨季
    2021-02-05 01:34

    JohnnysAce answer works. If you just want an easy way to get the current culture, it is done as always in .net:

    CultureInfo uiCultureInfo = Thread.CurrentThread.CurrentUICulture;
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    

    If you want to use IRequestCultureFeature (see JohnnyAces answer; because of dependency injection and better testability), you have to configure things in Startup.cs. Microsoft provided a sample here https://github.com/aspnet/Entropy/blob/2fcbabef58c2c21845848c35e9d5e5f89b19adc5/samples/Localization.StarterWeb/Startup.cs

提交回复
热议问题