如何在Windows Store应用程序中本地化通知和组合框? (C#/ XAML,多语言应用程序工具包)

3
我有几个问题关于Windows Store应用的本地化。我能够本地化像TextBlock.Text或Button.Content这样的XAML内容(我正在按照此处所示的方式进行操作),但我不知道如何本地化以下内容:
1)ComboBox中的选项。
<ComboBox x:Name="comboBoxTopAppBar" Width="120" Margin="10,0,0,0" MinWidth="200"
                      SelectedItem="{Binding SelectedStatus, Mode=TwoWay}">                   
                <x:String>Item 1</x:String>
                <x:String>Item 2</x:String>
                <x:String>Item 3</x:String>                   
            </ComboBox>

2). C#代码中的MessageDialogs(由于catch块而没有使用await)

new MessageDialog("Something went wrong. Please, check your login/password and internet connection.").ShowAsync();

3). C#代码中的Toast通知(我正在使用“通过C#使用Windows Runtime”书中的类库)
ToastNotificationManager.CreateToastNotifier()
                        .Show(new ToastNotification(new ToastTemplate(ToastTemplateType.ToastText01)
                        {
                            Text = {"Fetching your data. Please, wait."},
                            Duration = ToastDuration.Short,
                        }));

如何进行本地化?
2个回答

2

有趣的是,它们都联系在一起。

对于2)和3),您需要创建一个控制器,该控制器将持有一个ResourceLoader对象。如果使用Windows 8.1,则可以使用ResourceLoader.GetForIndependentUse()

在您的ResourceController中创建一个名为GetTranslation(string resource)的方法。它会看起来像这样:

private static ResourceLoader resourceLoader = ResourceLoader.GetForIndependentUse();

public static string GetTranslation(string resource)
{
    return resourceLoader.GetString(resource);
}

那么,每当您需要静态的编码翻译时,只需调用ResourceController.GetString(*您想要的字符串的键*)

这对于简单的代码调用非常有效,但对于Xaml并不直接适用,例如您的情况1)。不过不用担心,因为您有转换器的魔力!

public class ResourceTranslationConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var valString = value as string;

        // If what is being converted is a string, return the resource translation
        // Else return something else, such as the object itself
        return valString == null ? value : ResourceController.GetString(valString);
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

然后,你所要做的就是定义转换器一次(可能在所有XAML都可以访问的地方,可能是合并到你的App.xaml字典中),然后你可以在绑定中随时引用它!

对于这个实例,可以这样:

<!-- This is defined somewhere accessible, or just in the Page Resources -->
<!-- 'converters' is whichever namespace definition your converter is in -->
<converters:ResourceTranslationConverter x:Key="ResourceTranslationConverter"/>

<ComboBox x:Name="comboBoxTopAppBar" Width="120" Margin="10,0,0,0" MinWidth="200"
          SelectedItem="{Binding SelectedStatus, Mode=TwoWay}">    
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource ResourceTranslationConverter}}"
        </DataTemplate>
    <ComboBox.ItemTemplate>

    <x:String>Item 1</x:String>
    <x:String>Item 2</x:String>
    <x:String>Item 3</x:String>                   
</ComboBox>

通过这种方式,所有文本都会在运行时自动地通过您的ResourceLoader进行传递。任何您定义的新项目也将自动翻译(只要它们在您的资源中有条目并且已被翻译)。

希望这能帮到您,编码愉快!


谢谢你的回答。这真的很有用。但是ResourceLoader没有GetForIndependentUse()方法,我使用了GetForCurrentView()。无论如何,它运行得很好。 - Hozuki Ferrari

0
我想发布一个替代方案来回答问题(1),使用C#代码本地化ComboBox中的项目更加直观:

xaml

<ComboBox x:Name="comboBoxTopAppBar"/>

C#

var loader = ResourceLoader.GetForCurrentView();
comboBoxTopAppBar.Items.Add(loader.GetString("kItem1"));
comboBoxTopAppBar.Items.Add(loader.GetString("kItem2"));
comboBoxTopAppBar.Items.Add(loader.GetString("kItem3"));
comboBoxTopAppBar.SelectedIndex = 0; 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接