Google Search: C#, Finding the Locale

Paul Kinlan

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;
or
Thread.CurrentThread.CurrentCulture.ThreeLetterISOLanguageName;
or
Thread.CurrentThread.CurrentCulture.ThreeLetterWindowsLanguageName
or
Thread.CurrentThread.CurrentCulture.EnglishName;
or
Thread.CurrentThread.CurrentCulture.DisplayName;

Paul Kinlan

Trying to make the web and developers better.

RSS Github Medium