Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();   } } 
like image 601
JohnnyAce Avatar asked Dec 22 '16 18:12

JohnnyAce


People also ask

How can I get current culture in .NET core?

This is much easier (CultureInfo.CurrentCulture.Name) since you can assign this to a global variable in constructor of controller and use it in whatever method you want inside the controller. While getting the culture through request will have you declaring the variable each time in each of the methods you want to use.

How can use culture in asp net core?

Copy the required culture JavaScript files from the \js\culture\ folder of your Telerik UI for ASP.NET Core installation to the wwwroot/lib/kendo-ui/js/cultures/ folder of your application. Get the current culture. Include the corresponding culture JavaScript file. Set the current culture by calling the kendo.

How do I change the current culture in C#?

CurrentCulture = New CultureInfo("th-TH", False) Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP.

What is difference between CurrentCulture and CurrentUICulture?

CurrentCulture is the . NET representation of the default user locale of the system. This controls default number and date formatting and the like. CurrentUICulture refers to the default user interface language, a setting introduced in Windows 2000.


1 Answers

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(); } 
like image 119
JohnnyAce Avatar answered Oct 10 '22 04:10

JohnnyAce