You can localize a WPF application by using resource files and the WPF Localization Extension.
Install WPF Localization Extension
Start by installing the extension. You can do that through NuGet.
Create resource files
Now create your resource files.
The default file is Resources.resx
and you can create localized versions by making a copy and adding the language and country extension such as Resources.pt-PT.resx
for Portuguese from Portugal.
Here is the default (English) file:
And the Portuguese file:
Using the Localization Extension
In XAML you need to specify the Localization Extension namespace and also the default assembly and dictionary where the resources are located.
You call localized resources with the Loc
markup extension.
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
xmlns:lex="http://wpflocalizeextension.codeplex.com"
lex:LocalizeDictionary.DesignCulture="en"
lex:ResxLocalizationProvider.DefaultAssembly="WpfApplication1"
lex:ResxLocalizationProvider.DefaultDictionary="Resources"
Title="{lex:Loc Title}">
<Label Content="{lex:Loc Welcome}" />
</Window>
Specify the current language
You can specify the current culture in the LocalizeDictionary.Instance
singleton.
LocalizeDictionary.Instance.SetCurrentThreadCulture = true;
LocalizeDictionary.Instance.Culture = new CultureInfo("pt-PT");
By using the SetCurrentThreadCulture
property we also make sure that the culture is updated for the current thread.
Final result
Using the default language:
When switching to Portuguese: