Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Follow these steps:</p> <p><strong>1)</strong> Place all <code>String</code> fragments in a separate resource file. </p> <p>Example: <code>StringResources.xaml</code>:</p> <pre><code>&lt;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"&gt; &lt;!-- String resource that can be localized --&gt; &lt;system:String x:Key="All_Vehicles"&gt;All Vehicles&lt;/system:String&gt; &lt;/ResourceDictionary&gt; </code></pre> <p><strong>2)</strong> Make copies for each language and add them (translated) to the merged dictionaries. Don't forget to add the country's ISO code to make things easier.</p> <p>Example <code>App.xaml</code>:</p> <pre><code>&lt;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"&gt; &lt;Application.Resources&gt; &lt;ResourceDictionary &gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="StringResources.de-DE.xaml" /&gt; &lt;ResourceDictionary Source="StringResources.nl-NL.xaml" /&gt; &lt;ResourceDictionary Source="StringResources.xaml" /&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Application.Resources&gt; &lt;/Application&gt; </code></pre> <p>The last resource file with strings will be used to replace text parts in code.</p> <p><strong>3a)</strong> Use the text parts from the <code>String</code> table:</p> <p>Example <code>Window1.xaml</code>:</p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;Button Margin="51,82,108,129" Name="AllVehiclesButton" Content="{StaticResource All_Vehicles}"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>3b)</strong> Load the resource from code (Only use this code if you don't want to set via <code>XAML</code>):</p> <pre><code>void PageLoad() { string str = FindResource("All_Vehicles").ToString(); } </code></pre> <p><strong>4)</strong> Switch to new culture at start of application:</p> <p>Codesnippet from <code>App.xaml.cs</code>:</p> <pre><code>public static void SelectCulture(string culture) { if (String.IsNullOrEmpty(culture)) return; //Copy all MergedDictionarys into a auxiliar list. var dictionaryList = Application.Current.Resources.MergedDictionaries.ToList(); //Search for the specified culture. string requestedCulture = string.Format("StringResources.{0}.xaml", culture); var resourceDictionary = dictionaryList. FirstOrDefault(d =&gt; d.Source.OriginalString == requestedCulture); if (resourceDictionary == null) { //If not found, select our default language. requestedCulture = "StringResources.xaml"; resourceDictionary = dictionaryList. FirstOrDefault(d =&gt; 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 = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload