How to support multiple languages in WPF
If you are developing many solutions for many costomer, one day one of them will require a multilingual WPF-solution. So there is the question: how to make a WPF form multilingual?
At first you need at least two languages that you want to support. You can add some other languages at the end very easy. First of all create a folder in your project and call it "Languages", to keep an overview.
Once it is done you can create Resource Dictionaries in this folder. You can call them however you want, but make sure you can assign them to the language. You can use language codes (like "en-us") or you can call it like the language is called. At the end it can look like this:
I recommend to create just one language file for the beginning and duplicate it when you are done. In the XAML code of the file you need to create nodes of the type strings. They consist of a key (working like an ID) and a value.
The english version should look like the following:
After that you can duplicate the file (all keys will be the same and that is what we want) and change the values to the language you wish. In this case german:
Now you can bind the string as DynamicResource with the key into your label (or another control you want).
So now everything should be ready, there is just one little thing: how does
the application know what language it should use? So you need to look for the current language and load the needed resource.
Now you can call this function in the constructor in your main windows and your application is multilingual.
At first you need at least two languages that you want to support. You can add some other languages at the end very easy. First of all create a folder in your project and call it "Languages", to keep an overview.
Once it is done you can create Resource Dictionaries in this folder. You can call them however you want, but make sure you can assign them to the language. You can use language codes (like "en-us") or you can call it like the language is called. At the end it can look like this:
I recommend to create just one language file for the beginning and duplicate it when you are done. In the XAML code of the file you need to create nodes of the type strings. They consist of a key (working like an ID) and a value.
The english version should look like the following:
<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="keyTitle">This is a Demo</system:String> </ResourceDictionary>
After that you can duplicate the file (all keys will be the same and that is what we want) and change the values to the language you wish. In this case german:
<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="keyTitle">Das ist eine Demo</system:String> </ResourceDictionary>
Now you can bind the string as DynamicResource with the key into your label (or another control you want).
<Label x:Name="DemoLabel" Content="{DynamicResource keyTitle}" />
So now everything should be ready, there is just one little thing: how does
the application know what language it should use? So you need to look for the current language and load the needed resource.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | private void GetLanguageResources() { try { // prefix to the relative Uri for resource (xaml file) string _prefix = String.Concat(typeof(App).Namespace, ";component/"); // clear all ResourceDictionaries this.Resources.MergedDictionaries.Clear(); // get correct file string filename = ""; switch (Thread.CurrentThread.CurrentCulture.ToString().Substring(0, 2)) { case "de": filename = "Languages\\German.xaml"; break; case "en": filename = "Languages\\English.xaml"; break; default: filename = "Languages\\English.xaml"; break; } // add ResourceDictionary this.Resources.MergedDictionaries.Add ( new ResourceDictionary { Source = new Uri(String.Concat(_prefix + filename), UriKind.Relative) } ); } catch (Exception ex) { throw ex; } } |
Now you can call this function in the constructor in your main windows and your application is multilingual.
Comments
Post a Comment