有没有办法为整个应用程序设置文化?所有当前线程和新线程? [英] Is there a way of setting culture for a whole application? All current threads and new threads?

查看:23
本文介绍了有没有办法为整个应用程序设置文化?所有当前线程和新线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为整个应用程序设置文化?所有当前线程和新线程?

我们将文化的名称存储在数据库中,当我们的应用程序启动时,我们会这样做

We have the name of the culture stored in a database, and when our application starts, we do

CultureInfo ci = new CultureInfo(theCultureString);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

但是,当然,当我们想在新线程中做某事时,这会丢失".有没有办法为整个应用程序设置 CurrentCultureCurrentUICulture ?那么新线程也获得了这种文化?还是每当创建一个我可以连接的新线程时都会触发某个事件?

But, of course, this gets "lost" when we want to do something in a new thread. Is there a way of setting that CurrentCulture and CurrentUICulture for the whole application? So that new threads also gets that culture? Or is it some event fired whenever a new thread is created that I can hook up to?

推荐答案

在 .NET 4.5 中,您可以使用 CultureInfo.DefaultThreadCurrentCulture 属性,用于更改 AppDomain 的文化.

In .NET 4.5, you can use the CultureInfo.DefaultThreadCurrentCulture property to change the culture of an AppDomain.

对于 4.5 之前的版本,您必须使用反射来操纵 AppDomain 的文化.CultureInfo(.NET 2.0 mscorlib 中的 m_userDefaultCulture,.NET 4.0 mscorlib 中的 s_userDefaultCulture)上有一个私有静态字段,用于控制 CurrentCulture 如果线程没有为自己设置该属性,则返回.

For versions prior to 4.5 you have to use reflection to manipulate the culture of an AppDomain. There is a private static field on CultureInfo (m_userDefaultCulture in .NET 2.0 mscorlib, s_userDefaultCulture in .NET 4.0 mscorlib) that controls what CurrentCulture returns if a thread has not set that property on itself.

这不会改变本机线程区域设置,并且以这种方式发布改变文化的代码可能不是一个好主意.不过它可能对测试有用.

This does not change the native thread locale and it is probably not a good idea to ship code that changes the culture this way. It may be useful for testing though.

这篇关于有没有办法为整个应用程序设置文化?所有当前线程和新线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆