Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

c# - How to set Silverlight CurrentUICulture/CurrentCulture correctly?

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;

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.

Exactly how does SL interpret culture and how can I set it correctly for the entire application?

Thanks

UPDATE:

Found the answer:

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;

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);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit: Updated with the information that @Rumble found.

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");

Next you need to set the XML Language property.

For Silverlight

var root = RootVisual as Page;
if (root != null)
{
    root.Language = XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
}

For WPF

FrameworkElement.LanguageProperty.OverrideMetadata(typeof(FrameworkElement), new FrameworkPropertyMetadata(
            XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));

You can find an explanation for WPF here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...