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



我在windows store应用本地化方面遇到了一些问题。我能够本地化xaml的东西,比如TextBlock。文本或按钮。内容(我正在做同样的方式,如这里所示),但我不知道如何本地化以下内容:

1)。项目在我的组合框

<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(没有await,因为catch块)

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

3)。c#代码中的Toast通知(我使用的类库来自"Windows Runtime via c#"一书)

ToastNotificationManager.CreateToastNotifier()
                        .Show(new ToastNotification(new ToastTemplate(ToastTemplateType.ToastText01)
                        {
                            Text = {"Fetching your data. Please, wait."},
                            Duration = ToastDuration.Short,
                        }));

如何本地化?

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

对于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(*key of the string you want*)

这对于简单的代码调用非常有效,但不能直接用于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。您定义的任何新条目也将被自动翻译(只要它们在您的资源中有一个条目并且被翻译)。

希望这有助于和快乐的编码!

我想发布一个问题(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; 

最新更新