Localization and Globalization in C#: Localization
.NET has an elegant way of localizing your applications. If you want to write your code well, you will store all the messages, titles, captions and other strings in the appropriate resource files (.resx) in your application. Your application has to have a main resource file (or files), for example MeritResources.resx. You can store all the default strings and other resources there. If you want to localize your application to, for example “fr-FR” culture, you can simply create another resource file and name it MeritResources.fr-FR.resx. It is simple as that.
To explain how your application is localized we need to explain how .NET framework decides where to extract the appropriate strings and other resources from. When selecting localization resource, .NET framework is using the CultureInfo.CurrentUICulture property. If your CurrentUICulture is “de-AT” .NET framework will first look for the MeritResources.de-AT.resx resource file. If this file is missing, it will look for the next best thing and it is the MeritResources.de.resx file. If this resource file is also missing, default resource file named MeritResources.resx will be used.
So, if you are using the most common version of Windows (Englis version), you are stuck with the “en-US” culture; hence, your application will always use your English resource. In order to mitigate this, you can perform a little trick at your application startup. You can mislead your application to look in your CurrentCulture property instead of CurrentUICulture property. At the application startup, you can write something like this:
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
This way, your application will use your globalization setting instead of your default windows localization setting. Your application will be localized to your globalization language and culture. If you change the Regional Settings in your Control Panel, your application language and culture will change automatically.
Tags: .NET, C#, localization