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). 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). 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). 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,
}));
How can I localize it?
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
calledGetTranslation(string resource)
. It will look something like this: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!
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 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
C#