Question

I am working on localizing a GUI in WPF. The code that I post here will be from a prototype because I am trying to make this question as simple as possible.

I am using Resource Dictionaries to set the content of the strings in my window. I have two of them, one is German, and the other is English.

German Resource Dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <!-- The name of this ResourceDictionary. Should not be localized. -->
    <sys:String x:Key="ResourceDictionaryName" Localization.Comments="$Content(DoNotLocalize)">Loc-de-DE</sys:String>

    <!--- Button -->
    <sys:String x:Key="MainButton_Title">Hallo Welt!</sys:String>
    <sys:String x:Key="EnglishButton">Englisch</sys:String>
    <sys:String x:Key="GermanButton">Deutsch</sys:String>

</ResourceDictionary>

English Resource Dictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <!-- The name of this ResourceDictionary. Should not be localized. -->
    <sys:String x:Key="ResourceDictionaryName" Localization.Comments="$Content(DoNotLocalize)">Loc-en-US</sys:String>

    <!--- Buttons -->
    <sys:String x:Key="MainButton_Title">Hello World!</sys:String>
    <sys:String x:Key="EnglishButton">English</sys:String>
    <sys:String x:Key="GermanButton">German</sys:String>

</ResourceDictionary>

I also have dynamically set the content of my buttons in the XAML window:

<Button Content="{DynamicResource MainButton_Title}" Margin="59,68,0,80" Name="button1" HorizontalAlignment="Left" Width="152" />
<Button Content="{DynamicResource EnglishButton}" Height="23" HorizontalAlignment="Right" Margin="0,83,87,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<Button Content="{DynamicResource GermanButton}" Height="23" HorizontalAlignment="Right" Margin="0,0,87,90" Name="button3" VerticalAlignment="Bottom" Width="75" />

I want the language to change to English when I click the English button, and change to German when I click the German button. I have gotten this far, but what I don't know is the actual code that would be needed to tell the program to change it's Resource Dictionary. I'm guessing this would go in code-behind under the two button's click events, and might also have some code in the XAML window.

These are the resources that I've used, but I am posting this question because the end solution is not clear enough to me:

-http://www.wpfsharp.com/2012/01/27/how-to-change-language-at-run-time-in-wpf-with-loadable-resource-dictionaries-and-dynamicresource-binding/

-http://msdn.microsoft.com/en-us/library/bb613547.aspx

-http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx

Thank you for your help!

Was it helpful?

Solution

You should create new resource dictionary and after that you should merge with current dictionary:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("..\\Languages\\EnglishLang.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(dict);

If you want change language to German you should change file name in the second line.

In my example your resource dictionary should be in Languages folder.

Check my answer in this question:

How to start an internationalized WPF-Project in SharpDevelop 4.2?

OTHER TIPS

Try Wpf Localize Extension which support language change at runtime

Add resource dictionaries to App.xaml. On button click set

    ResourceDictionary _objRD = new ResourceDictionary();
    _objRD.Source = new Uri("Your Resource Dictionary Path", UriKind.Relative);
    App.Current.Resources.MergedDictionaries[0] = _objRD;

I would recommend localization using RESX file.

This worked for me

        ResourceDictionary dict = new ResourceDictionary();
        dict.Source = new Uri("Lang.pl-PL.xaml", UriKind.Relative);
        App.Current.Resources.MergedDictionaries[0] = dict;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top