Tony Vitabile
Tony Vitabile

Reputation: 8594

How to display localized text in XAML

I've got a WPF application which I need to localize for Spanish. I've read the MSDN docs and I'm left confused. This is complicated in that our company has a suite of three programs that work together (one is a Windows service, one is a web application and the third is my WPF application) and we've decided to build one resource DLL that contains the localizable strings for all three products.

So the strings are in a resource DLL, call it resources.dll. Right now, the original English strings are hard coded in the XAML. I've created resource IDs for each string of the form ControlType_StringName. Strings that are used by multiple controls are named Common_StringName.

How do I code the XAML so the proper string is displayed? For example, I have an About box. It uses a string called "About_Copyright". In the About box, there's a TextBlock that displays this string. What do I put in the Text attribute's value?

<TextBlock x:Uid="TextBlock_4" FontSize="18"
           FontWeight="Bold"
           Foreground="{DynamicResource TextForeground}"
           Grid.Row="5"
           HorizontalAlignment="Center"
           Margin="5"
           Text="Copyright © 2012, 2013, Elsag North America, Inc." />

When finished, there should be a reference in the Text attribute that references the "AboutBox_Copyright" string in the resources.

And there is a separate static class in the resource DLL for my program's resources called CarSystem, so the code-behind would refer to the string as `CarSystem.AboutBox_Copyright".

Upvotes: 1

Views: 1877

Answers (1)

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Add reference to your resource dll as

xmlns:res="clr-namespace:LocalizationDll.Properties"

and use it in your UI as

Text="{x:Static res:Resources.AboutBox_Copyright}"

Notice, that you must set up your UI culture as

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

Upvotes: 5

Related Questions