How to set Silverlight CurrentUICulture/CurrentCulture correctly?
Asked Answered
L

2

7

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);
}
Ladylove answered 6/2, 2012 at 14:58 Comment(0)
P
11

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.

Purgative answered 6/2, 2012 at 15:3 Comment(2)
First of all, thanks for the fast answer. In SL5, there appears to be no OverrideMetadata method. CurrentCulture also does not contain a IetfLanguageTag property...Ladylove
Looks like you got it sorted. I updated my answer with the information you found.Purgative
R
1

Thanks to eandersson I came up with this extension for specific controls. I had a problem with my decimal input, parsing and validation. Somewhere in the way there was this InvariantCulture with '.' as separator instead of ','. It can be easily modifed to set up specific culture.

public class ElementCultureExtension
{
    public static bool GetForceCurrentCulture( DependencyObject obj )
    {
        return (bool)obj.GetValue( ForceCurrentCultureProperty );
    }

    public static void SetForceCurrentCulture( DependencyObject obj, bool value )
    {
        obj.SetValue( ForceCurrentCultureProperty, value );
    }

    public static readonly DependencyProperty ForceCurrentCultureProperty =
        DependencyProperty.RegisterAttached(
            "ForceCurrentCulture", typeof( bool ), typeof( ElementCultureExtension ), new PropertyMetadata( false, OnForceCurrentCulturePropertyChanged ) );

    private static void OnForceCurrentCulturePropertyChanged(
        DependencyObject d,
        DependencyPropertyChangedEventArgs e )
    {
        var control = (FrameworkElement)d;
        if( (bool)e.NewValue )
        {
            control.Language = XmlLanguage.GetLanguage( Thread.CurrentThread.CurrentCulture.Name );
        }
    }
}

In Xaml:

<TextBox Text="{Binding Path=DecimalValue, Mode=TwoWay}"
                         tools:ElementCultureExtension.ForceCurrentCulture="True" />
Rafaelarafaelia answered 8/9, 2016 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.