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

前端 未结 5 892
隐瞒了意图╮
隐瞒了意图╮ 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

    0 讨论(0)
  • 2021-02-05 01:42

    ASP.Net Core 3.1:

    I can confirm that this is working if it's configured properly (see the second code block)

    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    

    In your startup class add this to the Configure method:

                IList<CultureInfo> supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en-US"), //English US
                    new CultureInfo("ar-SY"), //Arabic SY
                };
                var localizationOptions = new RequestLocalizationOptions
                {
                    DefaultRequestCulture = new RequestCulture("en-US"), //English US will be the default culture (for new visitors)
                    SupportedCultures = supportedCultures,
                    SupportedUICultures = supportedCultures
                };
    
                app.UseRequestLocalization(localizationOptions);
    

    Then the user can change the culture by calling this action:

            [HttpPost]
            [ValidateAntiForgeryToken]
            public IActionResult SetCulture(string culture, string returnUrl)
            {
                HttpContext.Response.Cookies.Append(
                    CookieRequestCultureProvider.DefaultCookieName,
                    CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                    new CookieOptions { Path = Url.Content("~/") });
    
                if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                {
                    return LocalRedirect(returnUrl);
                }
    
                return RedirectToAction("Index", "Home");
            }
    
    0 讨论(0)
  • 2021-02-05 01:55

    There's a global property CultureInfo.CurrentCulture in the System.Globalization namespace that gets the culture for the current thread. This has existed as far back as .NET Framework 4.0, and all the way through to the current version of .NET Core 3.1.

    0 讨论(0)
  • 2021-02-05 01:57

    The answer was on the Request Object, here's the code:

    public async Task<IActionResult> Index() {
        // Retrieves the requested culture
        var rqf = Request.HttpContext.Features.Get<IRequestCultureFeature>();
        // Culture contains the information of the requested culture
        var culture = rqf.RequestCulture.Culture;
        return View();
    }
    
    0 讨论(0)
  • 2021-02-05 01:58

    This code works for getting current culture in asp core controller:

    public string GetCulture() => $"CurrentCulture:{CultureInfo.CurrentCulture.Name}, CurrentUICulture:{CultureInfo.CurrentUICulture.Name}";
    
    0 讨论(0)
提交回复
热议问题