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

Windows Client Videos

How Do I: Use Resource Files for Localization both in XAML and Code

Using resource files can not only provide more flexibility when changing text displayed in an application, but can also allow for localization. In this video, Todd Miranda demonstrates how to use resource files to localize text in a WPF application.

Author: Todd Miranda

Comments

jayesh__patel said:

Hi Todd,

I am new to WPF and am njoying and learning a lot from your videos.

I noticed that the NameFormTitle did not display the Spanish translation after the CultureUI was changed.  This textblock had it's text bound in the XMAL to the NameFormStrings resource file.

How can we change the binding to look at the resource file associated with the current CultureUI setting?

Many Thanks,

Jayesh

# January 17, 2010 7:34 AM

mdcsoft said:

I believe it's because he set the culture on the UI thread *after* the XAML had been parsed. The NameFormTitle was set in XAML and the rest in code-behind.

I presume that if he set the culture on the machine and re-ran the app, all of the text would have been changed.

Mike

# January 21, 2010 9:20 AM

jayesh__patel said:

Thank you Mike.  Your feed-back is much appreciated.

Jayesh

# January 24, 2010 5:04 AM

johnshuttleworth@hotmail.com said:

Jayesh, Mike is correct try the following:

In the code behind file change

InitializeComponent();

Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");

to

Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");

InitializeComponent();

Now it should work.

John

# February 7, 2010 8:24 AM

jayesh__patel said:

Excellent, works a treat, thank you John.

# February 9, 2010 7:27 AM

rodagu116 said:

I have just one minor and not-so-important note:  The "foreign" language (the text) used is Italian not Spanish. ...a minor slip up.  

I thank you for your work in providing us with this example/demonstration.

# February 17, 2010 10:53 PM

alleyes said:

Very helpful.  The localization language codes were Spanish.  However the language used was Italian, not Spanish.

# May 13, 2010 9:24 AM

Thundar Viking said:

Same results here,

The CurrentUICulture must be set before the call to InitializeComponent().

public MainWindow()

{          

           Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-ES");  

           InitializeComponent();

}

//Nice to know about setting the Access Modifier to "Public" in the Resource File to generate static properties that can be used using the {x:Static .. } notation in XAML.

# June 20, 2010 6:39 PM

payini said:

Great video. Thanks.

# October 22, 2010 12:56 AM