JohnnyAce
JohnnyAce

Reputation: 3749

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

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 looking for something like:

public class HomeController : Controller {
  public async Task<IActionResult> Index()
  {
      // Something like the next line
      var requestCulture = GetRequestedCulture()
      return View();
  }
}

Upvotes: 56

Views: 56324

Answers (6)

max ray
max ray

Reputation: 35

You can get the current culture easily with the following code.

var culture = CultureInfo.CurrentCulture.Name;

It can be used in program.cs too.
Hope this can be helpful.

Upvotes: 1

carlin.scott
carlin.scott

Reputation: 7335

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.

Upvotes: 3

HMZ
HMZ

Reputation: 3127

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");
        }

Upvotes: 20

Lapenkov Vladimir
Lapenkov Vladimir

Reputation: 3218

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

public string GetCulture() => $"CurrentCulture:{CultureInfo.CurrentCulture.Name}, CurrentUICulture:{CultureInfo.CurrentUICulture.Name}";

Upvotes: 5

yonexbat
yonexbat

Reputation: 3012

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

Upvotes: 21

JohnnyAce
JohnnyAce

Reputation: 3749

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();
}

Upvotes: 69

Related Questions