WPF本地化:具有StringFormat的DynamicResource?

And*_*ark 5 .net c# wpf localization

我正在使用ResourceDictionary在.NET 4中进行本地化。有没有人有使用字符串格式的值的解决方案?

例如,假设我有一个键“ SomeKey”的值:

<ResourceDictionary ...>
    <s:String x:Key="SomeKey">You ran {0} miles</s:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

在TextBlock中使用它:

<TextBlock Text="{DynamicResource SomeKey}" />
Run Code Online (Sandbox Code Playgroud)

例如,如何将整数与SomeKey的值组合为格式字符串?

And*_*ark 2

因此,我最终想出了一个解决方案,允许我在 ResourceDictionary 中包含格式字符串,并能够在运行时动态更改语言。我认为它可以改进,但它确实有效。

此类将资源键转换为 ResourceDictionary 中的值:

public class Localization
{
    public static object GetResource(DependencyObject obj)
    {
        return (object)obj.GetValue(ResourceProperty);
    }

    public static void SetResource(DependencyObject obj, object value)
    {
        obj.SetValue(ResourceProperty, value);
    }

    // Using a DependencyProperty as the backing store for Resource.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ResourceProperty =
        DependencyProperty.RegisterAttached("Resource", typeof(object), typeof(Localization), new PropertyMetadata(null, OnResourceChanged));

    private static void OnResourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //check if ResourceReferenceExpression is already registered
        if (d.ReadLocalValue(ResourceProperty).GetType().Name == "ResourceReferenceExpression")
            return;

        var fe = d as FrameworkElement;
        if (fe == null)
            return;

        //register ResourceReferenceExpression - what DynamicResourceExtension outputs in ProvideValue
        fe.SetResourceReference(ResourceProperty, e.NewValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

此类允许将 ResourceDictionary 中的值用作 String.Format() 中的格式参数

public class FormatStringConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values[0] == DependencyProperty.UnsetValue || values[0] == null)
            return String.Empty;

        var format = (string)values[0];
        var args = values.Where((o, i) => { return i != 0; }).ToArray();

        return String.Format(format, args);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

示例用法 1:在此示例中,我使用 MultiBinding 中的 FormatStringConverter 将其 Binding 集合转换为所需的输出。例如,如果“SomeKey”的值为“对象 id 为 {0}”且“Id”的值为“1”,则输出将变为“对象 id 为 1”。

                <TextBlock ap:Localization.Resource="SomeKey">
                    <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource formatStringConverter}">
                            <Binding Path="(ap:Localization.Resource)" RelativeSource="{RelativeSource Self}" />
                            <Binding Path="Id" />
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
Run Code Online (Sandbox Code Playgroud)

示例用法 2:在此示例中,我使用与 Converter 的绑定将资源键更改为更详细的内容,以防止键冲突。例如,如果我有枚举值 Enum.Value(默认显示为“Value”),我会使用转换器附加其命名空间以创建更唯一的键。因此该值变为“My.Enums.Namespace.Enum.Value”。然后,Text 属性将解析为 ResourceDictionary 中“My.Enums.Namespace.Enum.Value”的值。

        <ComboBox ItemsSource="{Binding Enums}"
                  SelectedItem="{Binding SelectedEnum}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock ap:Localization.Resource="{Binding Converter={StaticResource enumToResourceKeyConverter}}"
                               Text="{Binding Path=ap:Localization.Resource), RelativeSource={RelativeSource Self}}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
Run Code Online (Sandbox Code Playgroud)

示例用法 3:在此示例中,键是一个文字,仅用于在 ResourceDictionary 中查找其对应的值。例如,如果“SomeKey”的值为“SomeValue”,那么它将简单地输出“SomeValue”。

                    <TextBlock ap:Localization.Resource="SomeKey"
                               Text="{Binding Path=ap:Localization.Resource), RelativeSource={RelativeSource Self}}"/>
Run Code Online (Sandbox Code Playgroud)