Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multilingual wpf application

I have a WPF application (in English) and I would like to let users to select different languages. I have read some possibilities to change languages in runtime applications, but I only want to choose a language during installation time and never change it.

Do you think the fastest and easiest way to do it is developing different versions of the program (changing only text language) and let the user to select one of them during the installation?? Probably to repeat code only changing textbox or labels is not very elegant, but notice that I have the application finished in English and I don´t need to change language at runtime.

like image 991
Carlos Alba Zamanillo Avatar asked Jul 04 '12 11:07

Carlos Alba Zamanillo


People also ask

Does Microsoft still support WPF?

Microsoft has faith in WPF as a UI framework Net Core 3.0. This demonstrates that Microsoft still has faith in WPF as a user interface framework, and the company is still willing to invest in it by updating and integrating it with its new offerings.”

What is localization WPF?

Localization is the translation of application resources into localized versions for the specific cultures that the application supports. When you localize in WPF, you use the APIs in the System. Windows. Markup. Localizer namespace.

Is WPF a language?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.

Can Android run WPF?

. NET 5 is s unified platform to write code once and run anywhere.


1 Answers

You can follow these steps:

  1. Creating the resource files

    Add this file StringResources.xaml to Resources directory. Here is an example:

    <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 close}"
       Grid.Row="3"
       Grid.Column="0" 
       Padding="10" />
    

Source: https://www.codeproject.com/Articles/123460/Simplest-Way-to-Implement-Multilingual-WPF-Applica

like image 100
Aghilas Yakoub Avatar answered Sep 18 '22 05:09

Aghilas Yakoub