How to detect installed operating system language in C#

Posted: (EET/GMT+2)

 

Sometimes, you need to know in your application in which language the operating system has been installed. For example, you might need to differentiate between an US English and a Norwegian Windows.

However, this seemingly simple thing becomes more complicated because Windows enables you to specify regional settings and enable Multilingual User Interfaces (MUIs). MUIs are especially common nowadays with the introduction of Windows Vista. That said, the user could be running Windows that is installed in English, but with Finnish regional settings. This is what I do all the time. And with MUI support, I could have a basic Windows Vista in US English, and then install/enable a Finnish MUI package. Then, all the operating system texts would change to Finnish.

There's a simple and important rule about these things: if you want to display messages to the user in their own language, don't detect the current regional settings, but detect the operating system language. It is irritating when users have a, say, English language Windows, but some applications, especially installation utilities try to be clever and detect the current regional settings, and display their texts in that language. This quickly leads to message boxes with the in Finnish (say) and buttons in English. Very ugly, and it quickly gets worse from there.

Ranting aside, in the Win32 API world, you could call the GetUserDefaultUILanguage and GetUserPreferredUILanguages API functions to get the information you need. In the .NET and C# world, things are even easier with the CultureInfo class from the System.Globalization namespace. This class has the static properties called CurrentCulture and CurrentUICulture which give the same information as the API functions. In fact, .NET is using P/Invoke to call the GetUserDefaultUILanguage API behind the scenes.

Now, I want to repeat what I said earlier. When displaying texts on your own, don't detect the regional settings language, but use the operating system (MUI) language. CultureInfo.CurrentUICulture returns the operating system language, and CultureInfo.CurrentCulture returns the selected regional settings culture.

Here are simple examples. English Windows Vista with U.S. regional settings:

  • CultureInfo.CurrentUICulture: en-US
  • CultureInfo.CurrentCulture: en-US

Same English Windows Vista, but this time with Finnish regional settings:

  • CultureInfo.CurrentUICulture: en-US
  • CultureInfo.CurrentCulture: fi-FI

Finnish Windows Vista, but with Danish regional settings:

  • CultureInfo.CurrentUICulture: fi-FI
  • CultureInfo.CurrentCulture: da-DK

In these property names, two characters can make a big difference. Knowing what to do is the key to correct programs.

Keywords: How to detect operating system language, howto detect windows os language, windows language.