multilingual wpf application

前端 未结 4 2021
野的像风
野的像风 2020-12-23 10:50

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

相关标签:
4条回答
  • 2020-12-23 11:12

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

    Also have a look at these articles having details about using Resx files for localization in WPF -

    Localizing a WPF Application with ResX Files

    WPF Localization

    WPF Localization Guidance - Whitepaper

    0 讨论(0)
  • 2020-12-23 11:20

    Just to improve @AghilasYakoub's correct answer, I think I need to point out that the following code should be added to the file App.xaml apart from what he had said:

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/StringResources.xaml"/>
                <ResourceDictionary Source="Resources/StringResources.fr-CA.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    
    0 讨论(0)
  • 2020-12-23 11:30

    If you want to use RESX files instead of resource dictionaries, you can do it easily with static references 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>
    

    In the Resource folder is the MainWindow.resx, MainWindow.de.resx, etc. and every file contains a key MyTestKey with a translation.

    0 讨论(0)
  • 2020-12-23 11:37

    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

    0 讨论(0)
提交回复
热议问题