We are developing a WPF Application which should support multiple Languages. Since we have to support some languages none of the development team knows, we agreed on allow the users (Administrators) to enter the texts for those languages. What is the best way to do so ?
2 Answers
You can follow these steps
1 Creating the Resource Files
Add this file StringResources.xaml to Ressources directory. Sample is here:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="close">Close</system:String>
</ResourceDictionary>
You can create several files, one for each language.
2 Adding the Resource (Call this when you start your application)
private void SetLanguageDictionary()
{
ResourceDictionary dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
case "en-US":
dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
break;
case "fr-CA":
dict.Source = new Uri("..\\Resources\\StringResources.fr-CA.xaml", UriKind.Relative);
break;
default :
dict.Source = new Uri("..\\Resources\\StringResources.xaml",UriKind.Relative);
break;
}
this.Resources.MergedDictionaries.Add(dict);
}
3 Using the Resource, like this -
<Button
x:Name="btnLogin"
Click="btnLogin_Click"
Content="{DynamicResource login}"
Grid.Row="3"
Grid.Column="0"
Padding="10" />
2 Comments
I have made very good experience with the WPFLocalizationExtension
It comes with loads of functionality (e.g binding to localized image ressources depending on the language) and the best: It's free. There are also some resx edit tools around that allow you to load and edit your resx localization files.