Reputation: 3558
Is there any built-in tool to support multilanguage applications in WinRT? For instance I have few buttons with text content "Add", "Remove", "Edit" in english and it should be "Dodaj", "Usun", "Edytuj" and so on in polish. Can I automatically set this text to user language? (and how to automatically detect user language?). I could use language model and bind buttons content to ViewModel property but isn't there exist better way to do it?
Upvotes: 0
Views: 560
Reputation: 6021
This is well supported, and MS have a very good sample here: http://code.msdn.microsoft.com/windowsapps/Application-resources-and-cd0c6eaa
Setting the text of "static" content using the x:uid doesn't work if the elements are databound. For example you have an observable collection in your view model containing username view models and you try and do the following (pseudo code!!):-
<List ItemSource={Binding Users}>
<List.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock x:uid="ByUser"/>
<TextBlock Text={Binding Username}/>
</StackPanel>
</DataTemplate>
</List.ItemTemplate>
</List>
The text that should set the first textbox (based on the uid ByUser) won't get set. I work around this by wrapping up the ResourceLoader
in a Globalisation Service and passing this into my "UsersName" view model, I would then expose a property called ByUserText, and bind on that. Not ideal, hopefully this will get fixed.
The only other thing you will need to use a globalisation service for is things like message boxes etc.
This is the globalisation service I pass around:-
using Windows.ApplicationModel.Resources;
public class GlobalisationService : IGlobalisationService
{
private readonly ResourceLoader resourceLoader;
public GlobalisationService()
{
resourceLoader = new ResourceLoader();
}
public string GetString(string key)
{
try
{
return resourceLoader.GetString(key);
}
catch
{
return "NOT FOUND: " + key;
}
}
public string this[string key]
{
get
{
return GetString(key);
}
}
}
Upvotes: 2