问题描述
我正在使用 C# 开发 SL5 应用程序,并且希望将其国际化.我发现以下设置 UI 文化:
I'm working on a SL5 app with C# and I'm looking to internationalize it. I found the following to set the UI culture:
var culture = new CultureInfo(Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName);
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
像 DatePicker 这样的一些控件似乎可以接受这个.如果我使用d"格式字符串格式化任何日期时间,我仍然会得到默认格式M/dd/yyyy".
Some controls like the DatePicker seem to pick this up. If I format any datetime by using the 'd' format string, I still get the default format "M/dd/yyyy" however.
SL 究竟如何解释文化,我该如何为整个应用程序正确设置它?
Exactly how does SL interpret culture and how can I set it correctly for the entire application?
谢谢
更新:
找到答案:
首先,在Application_Startup中设置合适的文化:
First of all, set the appropriate cultures in the Application_Startup:
var culture = new CultureInfo("nl-BE");
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
然而,关键元素是添加以下内容以强制 RootVisual 的文化/语言:
The key element however is to add the following to force the RootVisual's culture/language:
var root = RootVisual as Page;
if (root != null)
{
root.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
}
推荐答案
更新了@Rumble 找到的信息.
您需要像这样将其应用到您的 UI 对象中.
You need to do it like this to apply it to your UI objects as well.
首先在您的应用程序加载时设置适当的文化.
First set the appropriate cultures when your application is loading up.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-IN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-IN");
接下来您需要设置 XML 语言属性.
Next you need to set the XML Language property.
对于 Silverlight
var root = RootVisual as Page;
if (root != null)
{
root.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
}
对于 WPF
FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
您可以找到 WPF 的解释 此处.
You can find an explanation for WPF here.
这篇关于如何正确设置 Silverlight CurrentUICulture/CurrentCulture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!