Step 1) Place all string fragments in a separate stringtable resource file.
example: stringResources.xaml:
<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">
<!-- String resource that can be localized -->
<system:String x:Key="All_Vehicles">All Vehicles</system:String>
</ResourceDictionary>
step 2: make copies for each language and add them (translated) to the merged dictionaries.
example App.xaml
<Application x:Class="WpfStringTables.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="StringResources.de-DE.xaml" />
<ResourceDictionary Source="StringResources.nl-NL.xaml" />
<ResourceDictionary Source="StringResources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
The last resource file with strings will be used to replace text parts in code.
step 3a: Use the text parts from the string table
example Window1.xaml
<Window x:Class="WpfStringTables.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Button Margin="51,82,108,129" Name="button1" Content="{StaticResource All_Vehicles}"/>
</Grid>
</Window>
step 3b: load the resource from code
void Page_Load()
{
string str = FindResource("All_Vehicles").ToString();
}
step 4: switch to new culture at start of application
Codesnippet from App.xaml.cs
public static void SelectCulture( string culture )
{
// List all our resources
List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
foreach ( ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries )
{
dictionaryList.Add( dictionary );
}
// We want our specific culture
string requestedCulture = string.Format( "StringResources.{0}.xaml", culture );
ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault( d => d.Source.OriginalString == requestedCulture );
if ( resourceDictionary == null )
{
// If not found, we select our default language
//
requestedCulture = "StringResources.xaml";
resourceDictionary = dictionaryList.FirstOrDefault( d => d.Source.OriginalString == requestedCulture );
}
// If we have the requested resource, remove it from the list and place at the end.\
// Then this language will be our string table to use.
if ( resourceDictionary != null )
{
Application.Current.Resources.MergedDictionaries.Remove( resourceDictionary );
Application.Current.Resources.MergedDictionaries.Add( resourceDictionary );
}
// Inform the threads of the new culture
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture( culture );
Thread.CurrentThread.CurrentUICulture = new CultureInfo( culture );
}