How to localize notifications and combobox in Windows store app? (C#/XAML, Multilingual App Toolkit)

好久不见. 提交于 2019-12-01 22:35:00

Interestingly, they all tie together.

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().

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

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

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

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!

For this instance, something like:

<!-- 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>

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!

I want to post an alternative to question (1) localize items in ComboBox with C# code. It is more straight forward:

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; 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!