Multilingual wpf application

I have a WPF application (in English) and I want users to be able to choose different languages. I read some options for changing languages โ€‹โ€‹at runtime, but I only want to select a language during installation and never change it.

Do you think that the fastest and easiest way to do this is to develop various versions of the program (changing only the text language) and the ability for the user to select one of them during installation? It is likely to repeat the code, just changing the text box or shortcuts is not very elegant, but note that I finished the application in English and I do not need to change the language at runtime.

+7
source share
4 answers

I think the solution proposed by Aguilas is good; but you can use StaticResource instead of using DynamicResource in step 3, DynamicResource not required in your case, since you are not going to encode the language while the application is running.

Also check out these articles with details on using Resx files for localization in WPF -

Localizing a WPF Application with ResX Files

WPF Localization

WPF Localization Guide - Technical Documentation

+5
source

You can follow these steps.

  • Creating Resource Files

    Add this StringResources.xaml file to the 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 multiple files, one for each language.

  • Adding a resource (calling this when the application starts)

     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); } 
  • Using a resource, for example:

     <Button x:Name="btnLogin" Click="btnLogin_Click" Content="{DynamicResource login}" Grid.Row="3" Grid.Column="0" Padding="10" /> 
+21
source

To improve @AghilasYakoub's correct answer, I must indicate that the following code should be added to the App.xaml file, except that it said:

 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Resources/StringResources.xaml"/> <ResourceDictionary Source="Resources/StringResources.fr-CA.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> 
+2
source

If you want to use .resx files instead of resource dictionaries, you can easily do this with static links in XAML.

 <Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:res="clr-namespace:MyApp.Resources"> <Button Text="{x:Static res:MainWindow.MyTestKey}"> </Window> 

The Resource folder contains MainWindow.resx , MainWindow.de.resx , etc., and each file contains the MyTestKey key with the translation.

0
source

All Articles