C# 如何强制本地化文化在整个应用程序中使用 en-US

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

How can I force Localization Culture to en-US for whole application

c#.netlocalizationmultithreading

提问by

I'm having an issue with some byte conversions and a few of my calculations in one of my applications. I was able to contribute it to the person running it having an Italian Culture setting in windows. So my question is: What is the best way to for "en-US" on any computer running my application. I have a code sample below, but I am unsure if any thread I use will inhert it.

我在我的一个应用程序中遇到了一些字节转换和一些计算的问题。我能够将它贡献给在 Windows 中拥有意大利文化设置的运行它的人。所以我的问题是:在运行我的应用程序的任何计算机上进行“en-US”的最佳方法是什么。我在下面有一个代码示例,但我不确定我使用的任何线程是否会继承它。

[STAThread]
static void Main()
{
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

    ...
}

回答by Andy

Your code will set the culture of the current thread, but any new threads that are created will not have this culture 'inherited'. You have to set the culture you require yourself. (I believe that any new threads will be created with the installed Windows culture, but I'm prepared to be proved wrong on that.)

您的代码将设置当前线程的文化,但创建的任何新线程都不会“继承”该文化。你必须自己设定你需要的文化。(我相信任何新线程都将使用已安装的 Windows 文化创建,但我准备在这方面被证明是错误的。)

This is answered in this post: Is there a way of setting culture for a whole application? All current threads and new threads?

在这篇文章中回答了这个问题: 有没有办法为整个应用程序设置文化?所有当前线程和新线程?

Personally, I find this behaviour annoying, but that's the way it is.

就个人而言,我觉得这种行为很烦人,但事实就是如此。

回答by dahlbyk

The problem you describe is the reason InvariantCultureexists. Rather than change your application's culture, you should do your behind-the scenes data manipulation/persistence with the invariant culture and then let the user's culture determine how values are rendered.

您描述的问题是InvariantCulture存在的原因。与其改变您的应用程序的文化,您应该使用不变文化进行幕后数据操作/持久化,然后让用户的文化决定如何呈现值。