所以,我遇到了这个问题,微软实际上把格陵兰文化(kl-GL)的月份名称弄错了。我还知道我可以将自己的字符串数组传递给 DateTimeFormatInfo.MonthNames 属性,但似乎我指定的值仅在该一个 CultureInfo 实例的范围内使用。有没有办法告诉.Net每次我有kl-GL文化的实例时都应该使用这些特定的月份名称?
我知道您可以创建用户特定的区域性,但我无法访问某些遗留代码来实际更改代码以使用我自己的用户指定的区域性。
So, i have this problem where Microsoft actually got the month names wrong for the Greenlandic culture (kl-GL). I also know that i can pass my own array of string to the DateTimeFormatInfo.MonthNames Property, but it seems like the values i specify is only used in the scope of that one CultureInfo instance. Is there a way to tell .Net that every time i have an instance of the kl-GL culture these specific monthnames should be used?
I know that you can create user specific cultures, but i don't have access to some legacy code to actually change the code to use a my own userspecified culture.
发布评论
评论(6)
为什么需要一两个以上的 CultureInfo 对象?您可以将线程区域性 (System.Threading.Thread.CurrentThread.CurrentCulture) 或线程 UI 区域性 (System.Threading.Thread.CurrentThread.CurrentUICulture) 更改为您需要的值。然后,如果需要显式引用,则可以使用当前区域性或引用 CurrentThread 中的对象。
Why do you need more than one or two CultureInfo object? You can change the threading culture (System.Threading.Thread.CurrentThread.CurrentCulture) or the threading UI culture (System.Threading.Thread.CurrentThread.CurrentUICulture) to the values you need. Then you can use current culture or refer to the objects in CurrentThread if you need an explicit reference.
给你
注意:仅适用于.NET 4.0
Here you go
NOTE: only for .NET 4.0
取代特定的文化。
不要执行这个!它将覆盖您的文化设置。根据需要进行调整。
创建新的特定文化。
在 ASP.NET 中使用新区域性就像将其添加到 web.config 中一样简单:
或者这样做
Replacing a specific culture.
Don't execute this! It will overwrite your culture settings. Adjust as you need it.
Creating a new specific culture.
Using the new culture in ASP.NET is as trivial as adding this to your web.config:
<globalization culture="kl-GL-custom" uiCulture="kl-GL-custom"/>
or do this
我认为@Dirk的答案是最好的。在此 MSDN 页面上,它解释了如何初始化 ASP.NET 页面的区域性:
例子:
I think @Dirk's answer is best. On this MSDN page, it explains how to initialize the culture for an ASP.NET page:
Example:
这完全是一个黑客/暴力答案,但您可以处理 Page_PreRender 事件,检测区域性,然后在将这些单词发送到浏览器之前对其进行字符串替换。
我不知道这对性能有何影响。
This is completely a hack/brute force answer, but you could handle the Page_PreRender event, detect the culture, and then and do a string replace on those words before they are sent out to the browser.
I have no idea what the performance implication of this is.
您可能想研究模拟框架 TypeMock Isolator。使用它,您可以覆盖 CLR 中的许多内容,包括从“新”调用返回的内容。因此,如果您要在程序入口点附近设置一个具有更正月份名称的“假”CultureInfo,则每次调用
new CultureInfo("kl-GL")
都会返回您更正后的实例。对性能的影响并不大(TypeMock 显然是通过 CLR 的分析钩子工作的),但如果正确性的好处超过了小的性能损失,则值得研究。
You may want to investigate the mocking framework TypeMock Isolator. With it you can override many things in the CLR, including what gets returned from a "new" invocation. So if you were to set up a "fake" CultureInfo with the corrected month names close to your program's entry point, each call to
new CultureInfo("kl-GL")
would return your corrected instance.The performance implications are not great (TypeMock apparently works via the CLR's profiling hooks), but it is worth investigating if the benefits of correctness outweigh a small performance penalty.