C# 多语言 wpf 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11327840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:26:35  来源:igfitidea点击:

multilingual wpf application

c#.netwpfmultilingual

提问by Carlos Alba Zamanillo

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.

我有一个 WPF 应用程序(英文),我想让用户选择不同的语言。我已经阅读了一些在运行时应用程序中更改语言的可能性,但我只想在安装期间选择一种语言并且永远不要更改它。

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.

你认为最快和最简单的方法是开发不同版本的程序(仅更改文本语言)并让用户在安装过程中选择其中之一?可能只更改文本框或标签的重复代码不是很优雅,但请注意,我以英语完成了应用程序,并且我不需要在运行时更改语言。

采纳答案by akjoshi

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

我认为 Aghilas 提出的解决方案是好的;但是您可以使用StaticResource而不是DynamicResource在步骤 3中使用,DynamicResource在您的情况下不需要,因为您不会在应用程序运行时更改语言。

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

还可以查看这些文章,其中包含有关在 WPF 中使用 Resx 文件进行本地化的详细信息 -

Localizing a WPF Application with ResX Files

使用 ResX 文件本地化 WPF 应用程序

WPF Localization

WPF 本地化

WPF Localization Guidance - Whitepaper

WPF 本地化指南 - 白皮书

回答by Aghilas Yakoub

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" />
    
  1. 创建资源文件

    将此文件 StringResources.xaml 添加到 Resources 目录。下面是一个例子:

    <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>
    

    您可以创建多个文件,每种语言一个。

  2. 添加资源(在启动应用程序时调用它)

    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. 像这样使用资源 -

    <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

来源:https: //www.codeproject.com/Articles/123460/Simplest-Way-to-Implement-Multilingual-WPF-Applic

回答by Martin Braun

If you want to use RESX files instead of resource dictionaries, you can do it easily with static references in XAML.

如果要使用 RESX 文件而不是资源字典,可以使用 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 Resourcefolder is the MainWindow.resx, MainWindow.de.resx, etc. and every file contains a key MyTestKeywith a translation.

Resource文件夹是MainWindow.resxMainWindow.de.resx等,每个文件包含密钥MyTestKey与翻译。

回答by Cristian Alonso Vallejo

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.xamlapart from what he had said:

只是为了改进@AghilasYakoub 的正确答案,我想我需要指出App.xaml除了他所说的之外,还应该将以下代码添加到文件中:

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