tabina
tabina

Reputation: 1155

Wpf localization at runtime does not effect date format

I am using localization with MarkupExtension like in Christians Mosers Wpf tutorial. Changing the current language at runtime works well, but I found, that the date format is never updated. It always is formatted for en-US, no matter which language is selected.

I'm setting the current language like this

Thread.CurrentThread.CurrentUICulture = value;
Thread.CurrentThread.CurrentCulture = value;

Did I miss something?

Upvotes: 2

Views: 2002

Answers (3)

Bahman_Aries
Bahman_Aries

Reputation: 4798

I had the same problem, but using LocBinding from Advanced WPF Localization and a simple DateTimeConverter solved my problem. So The converter DOES get triggered when I switch the current language at runtime.

My XAML looks like:

<TextBlock Style="{StaticResource EntryBoxHeader}">
    <TextBlock.Text>
        <LocBinding StringFormat="{}{0:d}">
           <Binding Source="{x:Static System:DateTime.Now}" Path="." Converter="{StaticResource dtConvertor}"/>
        </LocBinding>
    </TextBlock.Text>
</TextBlock>

Upvotes: 2

MAXE
MAXE

Reputation: 5122

I had to write a converter, to get my dates localized correctly in XAML (your Thread.CurrentThread.CurrentUICulture and Thread.CurrentThread.CurrentCulture probably already contain the correct values!)!:

public sealed class DateTimeToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;

        if (value is DateTime)
                // HERE YOU HAVE TO PASS YOUR CULTURE INFO:
                return ((DateTime)value).ToString("d", Thread.CurrentThread.CurrentUICulture);
        else
            throw new NotImplementedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return null;
        else
            throw new NotImplementedException();
    }
}

...and then use it in your XAML (remember to declare it in the resource dictionary), as follows (the converter: prefix is related to the namespace where the DateTimeToStringConverter class is declared!):

<Window.Resources>
    <ResourceDictionary>
        <converters:DateTimeToStringConverter x:Key="DateTimeToStringConverter" />
    </ResourceDictionary>
</Window.Resources>

<TextBlock Text="{Binding Path=Date, Mode=OneWay, Converter={StaticResource DateTimeToStringConverter}}"  />

Upvotes: 0

sam1589914
sam1589914

Reputation: 816

Try :

public static class LanguageManipulator
{
    public static void SetXmlFromCurrentCulture()
    {
        var curr = CultureInfo.CurrentCulture;
        var lang = XmlLanguage.GetLanguage(curr.Name);

        SetCulture(lang, curr);

        var meteadata = new FrameworkPropertyMetadata(lang);
        FrameworkElement.LanguageProperty.OverrideMetadata(typeof (FrameworkElement), meteadata);
    }

    private static void SetCulture(XmlLanguage lang, CultureInfo cult)
    {
        var propertynames = new[]
                                {
                                    "_equivalentCulture",
                                    "_specificCulture",
                                    "_compatibleCulture"
                                };

        const BindingFlags flags = BindingFlags.ExactBinding | BindingFlags.SetField | BindingFlags.Instance | BindingFlags.NonPublic;

        foreach (var name in propertynames)
        {
            var field = typeof (XmlLanguage).GetField(name, flags);
            if (field != null) field.SetValue(lang, cult);
        }

    }
}

And call SetXmlFromCurrentCulture after you have set the threads culture.

Upvotes: 0

Related Questions