简体   繁体   English

如何为整个c#应用程序设置默认文化信息

[英]how to set default culture info for entire c# application

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 如何将其设置为特定类或整个应用程序的默认值

With 4.0, you will need to manage this yourself by setting the culture for each thread as Alexei describes. 使用4.0,您需要通过为Alexei描述的每个线程设置文化来自行管理。 But with 4.5, you can define a culture for the appdomain and that is the preferred way to handle this. 但是使用4.5,您可以为appdomain定义文化,这是处理此问题的首选方法。 The relevant apis are CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture . 相关的apis是CultureInfo.DefaultThreadCurrentCultureCultureInfo.DefaultThreadCurrentUICulture

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? CurrentUICulture和CurrentCulture可以按照此处讨论的每个线程进行设置有没有为整个应用程序设置文化的方法? All current threads and new threads? 所有当前线程和新线程? . You can't change InvariantCulture at all. 你根本无法改变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"))

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM