如何在Windows应用商店应用中本地化通知和组合框? (C#/XAML,多语言应用程序工具包) [英] How to localize notifications and combobox in Windows store app? (C#/XAML, Multilingual App Toolkit)

查看:67
本文介绍了如何在Windows应用商店应用中本地化通知和组合框? (C#/XAML,多语言应用程序工具包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用商店本地化方面遇到了一些问题.我能够本地化xaml内容,例如TextBlock.Text或Button.Content(

I have a couple of issues with windows store app localization. I am able to localize xaml stuff like TextBlock.Text or Button.Content(I'm doing it in the same way as shown here), but I have no idea how can I localize following things:

1).我的ComboBox中的项目.

1). Items in my 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块而没有等待)

2). MessageDialogs in C# code(without await because of catch block)

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

3). C#代码中的Toast通知(我正在使用通过C#运行Windows"中的类库)

3). Toast notifications in C# code(I am using class library from "Windows Runtime via C#" book)

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

如何对其进行本地化?

推荐答案

有趣的是,它们都绑在一起了.

Interestingly, they all tie together.

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

For 2) and 3) you need to create a Controller which will hold a ResourceLoader object. You can use (if using Windows 8.1), ResourceLoader.GetForIndependentUse().

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

Create a method in your ResourceController called GetTranslation(string resource). It will look something like this:

private static ResourceLoader resourceLoader = ResourceLoader.GetForIndependentUse();

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

然后,每当需要静态的编码翻译时,只需调用ResourceController.GetString(*key of the string you want*).

Then, whenever you need a static, coded translation, just call ResourceController.GetString(*key of the string you want*).

这对于简单的代码调用非常有用,但不适用于Xaml,例如您的场景1).不过,不要害怕,因为您拥有Converters的魔力!

This works great for simple code calls, but doesn't work directly for Xaml, such as your scenario 1). Fear not though, as you have the magic of Converters!

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中的字典),并且您可以随时在绑定中引用它!

Then, all you have to do is define the converter once (likely somewhere accessible from all of your xaml, possibly a dictionary merged into your App.xaml) and you can refer to it in a binding whenever you want!

在这种情况下,类似:

<!-- 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传递.您定义的任何新项目也将自动翻译(只要它们在您的资源中有一个条目并被翻译).

This way all your text is being passed through your ResourceLoader at runtime and automatically. Any new Items you define will also be automatically translated (so long as they have an entry in your Resources and are translated).

希望这对您有所帮助,并且编码愉快!

Hope this helps and happy coding!

这篇关于如何在Windows应用商店应用中本地化通知和组合框? (C#/XAML,多语言应用程序工具包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆