在 App.config 中设置 WPF 程序的文化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20379253/
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-09-13 10:09:53  来源:igfitidea点击:

Setting WPF Program's culture in App.config

c#.netwpflocalizationapp-config

提问by Tony Vitabile

I've got a WPF application that I'm modifying to support localization. My program is part of a suite of products that my company offers. We have created a resource DLL that contains all of the strings that need to be translated for all of our products. I've added a reference to this DLL to my program, added string resources to it, and modified my code to use the string resources instead of hard-coded strings. Everything works fine when I run my program in English.

我有一个 WPF 应用程序,我正在修改它以支持本地化。我的程序是我公司提供的一套产品的一部分。我们创建了一个资源 DLL,其中包含需要为我们所有产品翻译的所有字符串。我已经在我的程序中添加了对这个 DLL 的引用,向它添加了字符串资源,并修改了我的代码以使用字符串资源而不是硬编码的字符串。当我用英语运行我的程序时,一切正常。

We have had the string resources translated into Spanish. I've created a resx file with the Spanish translations in it. I've rebuilt my application. Now I want to see the Spanish text appear in my program without having to change my computer's culture settings.

我们已将字符串资源翻译成西班牙语。我创建了一个包含西班牙语翻译的 resx 文件。我重建了我的应用程序。现在我想看到西班牙语文本出现在我的程序中,而不必更改我的计算机的文化设置。

It's not necessary for the program to change culture settings on the fly. The program will run in one language only for a particular installation.

程序没有必要即时更改文化设置。该程序将仅针对特定安装以一种语言运行。

The program has an App.config file. Short of adding a custom setting with the culture information in it, how do I tell my program to run in Spanish?

该程序有一个 App.config 文件。如果没有添加包含文化信息的自定义设置,我该如何告诉我的程序以西班牙语运行?

采纳答案by Tony Vitabile

I decided to add this answer because while @MauricioGarcia's answer works if you want to always display a particular language on a machine with multiple language packs installed on it, no matter what the current region settings on the machine are, we didn't implement it that way.

我决定添加这个答案,因为虽然@MauricioGarcia 的答案有效,但如果您想始终在安装了多个语言包的机器上显示特定语言,无论机器上的当前区域设置是什么,我们都没有实现它那样。

Instead, we just use whatever CultureInfoobjects are set in the current Thread.CurrentThread.CurrentCultureand Thread.CurrentThread.CurrentUICultureproperties. When you set the region settings on your computer to a particular location and language, these properties are automatically changed. So they're always right & we don't have to add any code to change anything.

相反,我们只使用CultureInfo当前Thread.CurrentThread.CurrentCultureThread.CurrentThread.CurrentUICulture属性中设置的任何对象。当您将计算机上的区域设置设置为特定位置和语言时,这些属性会自动更改。所以他们总是对的,我们不需要添加任何代码来改变任何东西。

Interestingly, the object in the CurrentUICultureproperty is used to determine which language strings to display and the object in the CurrentCultureproperty is used to format numbers & DateTime.

有趣的是,CurrentUICulture属性中的对象用于确定要显示的语言字符串,而CurrentCulture属性中的对象用于格式化数字和日期时间。

回答by Mauricio Gracia Gutierrez

You could just define a key in your App.config like this

您可以像这样在 App.config 中定义一个键

<configuration>
    <appSettings>
        <add key="DefaultCulture" value="es-CO" />
    </appSettings>
</configuration>

and in your application read that value and set the culture

并在您的应用程序中读取该值并设置文化

CultureInfo culture = new CultureInfo(ConfigurationManager.AppSettings["DefaultCulture"]);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;

In the example of the config I set it to Spanish-Colombia

在配置示例中,我将其设置为西班牙-哥伦比亚

This is a list of culture codes

这是一个文化代码列表