2

我在Christians Mosers Wpf 教程中使用带有 MarkupExtension 的本地化。在运行时更改当前语言效果很好,但我发现日期格式永远不会更新。无论选择哪种语言,它始终为 en-US 格式。

我正在像这样设置当前语言

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

我错过了什么?

4

3 回答 3

2

我遇到了同样的问题,但是使用LocBindingAdvanced WPF Localization并简单地DateTimeConverter解决了我的问题。因此,当我在运行时切换当前语言时,转换器确实会被触发。

我的 XAML 看起来像:

<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>
于 2012-11-23T12:28:14.590 回答
0

我必须编写一个转换器,以使我的日期在 XAML 中正确本地化(您的Thread.CurrentThread.CurrentUICulture并且Thread.CurrentThread.CurrentCulture可能已经包含正确的值!)!:

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();
    }
}

...然后在你的 XAML 中使用它(记得在资源字典中声明它),如下(converter:前缀与DateTimeToStringConverter声明类的命名空间有关!):

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

<TextBlock Text="{Binding Path=Date, Mode=OneWay, Converter={StaticResource DateTimeToStringConverter}}"  />
于 2012-08-23T08:36:44.693 回答
0

尝试 :

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

    }
}

并在设置线程文化后调用 SetXmlFromCurrentCulture。

于 2012-08-23T08:36:53.950 回答