如何为整个 C# 应用程序设置默认文化信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13354211/
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
how to set default culture info for entire c# application
提问by MonsterMMORPG
I want to set default culture info for that class or for entire application.
我想为该类或整个应用程序设置默认文化信息。
For example in Turkey 3,2 = in english 3.2
例如在土耳其 3,2 = 英语 3.2
so application uses my local but i want it to use as default
所以应用程序使用我的本地,但我希望它作为默认使用
System.Globalization.CultureInfo.InvariantCulture
How can i set it to that as default for that specific class or for entire application
我如何将其设置为该特定类或整个应用程序的默认值
采纳答案by Alexei Levenkov
Not for entire application or particular class.
不适用于整个应用程序或特定类。
CurrentUICulture and CurrentCulture are settable per thread as discussed here Is there a way of setting culture for a whole application? All current threads and new threads?. You can't change InvariantCultureat all.
CurrentUICulture 和 CurrentCulture 可按线程设置,如此处讨论的有没有办法为整个应用程序设置文化?所有当前线程和新线程?. 你根本无法改变InvariantCulture。
Sample code to change cultures for current thread:
更改当前线程文化的示例代码:
CultureInfo ci = new CultureInfo(theCultureString);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
For class you can set/restore culture inside critical methods, but it would be significantly safe to use appropriate overrides for most formatting related methods that take culture as one of arguments:
对于类,您可以在关键方法中设置/恢复文化,但对于大多数将文化作为参数之一的格式相关方法使用适当的覆盖将是非常安全的:
(3.3).ToString(new CultureInfo("fr-FR"))
回答by Eric MSFT
With 4.0, you will need to manage this yourself by setting the culture for each thread as Alexei describes. But with 4.5, you can define a culture for the appdomain and that is the preferred way to handle this. The relevant apis are CultureInfo.DefaultThreadCurrentCultureand CultureInfo.DefaultThreadCurrentUICulture.
使用 4.0,您将需要通过为 Alexei 描述的每个线程设置文化来自己管理。但是在 4.5 中,您可以为 appdomain 定义一种文化,这是处理此问题的首选方式。相关的 api 是CultureInfo.DefaultThreadCurrentCulture和CultureInfo.DefaultThreadCurrentUICulture。
回答by Renzo Ciot
If you use a Language Resource file to set the labels in your application you need to set the its value:
如果您使用语言资源文件在应用程序中设置标签,则需要设置其值:
CultureInfo customCulture = new CultureInfo("en-US");
Languages.Culture = customCulture;

