Welcome to WindowsClient.net | My Blog | Sign in | Join

Windows Client Videos

How Do I: Localize Applications using the CultureInfo Class

Pat Tormey shows how to localize Windows Forms applications using the methods and properties of the CultureInfo class. He covers a number of common localization challenges, including how to format numbers, dates, and currencies.

Author: Pat Tormey

Comments

alex_simkin said:

I didn't actually see how labels on the form changed to Spanish.

# September 11, 2008 1:27 PM

dpachla said:

Microsoft Visual C# 2008 Express Edition

.NET Framework 3.5

This is good information.  To take it one step further, we can also change the text of the labels.  It really doesn't involve much.  Just place the following line into the Main function of Program.cs or into the constructor of Form1 after InitializeComponent();...

System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(CultureInfo.CurrentCulture.LCID);

Note that CurrentCulture is not the same as CurrentUICulture.  Also note that this will not change the text the labels when the regional settings are changed while the program is running.

If you look at the code in Form1.Designer.cs, you will see a line which applies the CultureInfo to the form.  It is inside the code region 'Windows Form Designer generated code'.

resources.ApplyResources(this.Label1, "Label1");

Read the Help to learn about exactly what ApplyResources does, but it uses CurrentUICulture and not CurrentCulture.  CurrentCulture is what's changed when the regional settings are changed as described in the video.

What I do not know is how to capture the event when the CurrentCulture is changed.  Then the optimal solution would be reset the CurrentUICulture when that event is triggered.  However, that would still not solve the problem of changing the text of the labels "on the fly" because the CultureInfo is only applied when initializing the form.  You would have to call resources.ApplyResources for all initialized objects in the application.

There is also another way to control the text which is under the CurrentUICulture.  It is geared more towards changing the language on the fly rather than when initializing.  Read the following.

www.dotneti18n.com/.../Chapter3.pdf

Hopefully, this helps.  Maybe in future releases we will be able to change all text on the fly without having to create special resources or code.  Or maybe we already can and my .NET knowledge is still too limited.

# September 14, 2008 5:42 PM

Bhaskarg said:

dpachla ,

To capture the event when the CurrentCulture is changed , handle the event SystemEvents.UserPreferenceChanged. This is found in the Microsoft.Win32 namespace  It has the event args UserPreferenceChangedEventArgs

which has a property 'Category' whose value will be 'Locale'. Its easy from there to determine what the settings are. However , you will stil need to refresh the form manually from code if the new settings have to reflect immediately.Hope that helps.

Happy Coding!

# September 19, 2008 5:10 AM