1 min read

Google Search: C#, Finding the Locale

Paul Kinlan

Paul Kinlan

Lead of Chrome DevRel

I have had a search against my site for finding the User Locale in C#.

There are a couple of ways that this can be done. If you are using ASP.Net you can look at the Request.UserLanguages string array to see what preferences they have set up. The first locale in the array should be the locale that they normally work. Obviously this is browser dependent and user configuration dependent.

Using this information you could then set the threads culture (so that all comparisons etc are performed with the correct culture awareness) using the following piece of code:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);

If it is a Winform or Service you could get the Current Culture just by inspecting the either:Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;orThread.CurrentThread.CurrentCulture.ThreeLetterISOLanguageName;orThread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageNameorThread.CurrentThread.CurrentCulture.EnglishName;orThread.CurrentThread.CurrentCulture.DisplayName;

Stay in the loop.

I'm trialing a newsletter. Join for monthly insights into web dev, Chrome, and the open web.

alternate_email

Get in touch

Open to chat about Chrome or Web development.

Book a consultation