Silverlight MVVM : How to do Localization in the correct way?
In normal Silverlight projects, localization in the xaml view is done using:
Text="{BindingPath=ApplicationStrings.MyNewString, Source={StaticResource ResourceWrapper}}"
My question is : how to do correct localization from labels when using the MVVM pattern?
Is this done like described here ?
In xaml view:
Text="{Binding LblUsername,FallbackValue='User Name'}"
And the LoginViewModel.cs class:
public string LblUsername
{
get { return Resources.Username; 开发者_开发技巧}
}
Any tips here ?
And how about reusing the same string on multiple view/viewmodels ? (Just imagine that I need 'Username' on multiple views.) Does it make sense to to derive all viewmodels from a basemodel which defines all resources ?
Check out my article on localization using MVVM.
http://cloudringer.wordpress.com/2011/02/18/localization-in-wpf-and-silverlight-with-mvvm/
There are many ways to solve the problem depending on your needs and constraints. The approach I describe has been flexible and blendable enough to meet my needs in WPF and Silverlight on multiple projects.
Well we use the
ApplicationStrings.MyNewString, Source={StaticResource ResourceWrapper}}
And it works fine. it's a project on the solution level and i just ref. the project into the individual projects, then i can use it as a global localization. and one "label" is only defined once.
I'm Danish so we have german, swedish, danish, english etc. translations running. If this dont help you i can provide code. But you are on the right track with option one :-)
精彩评论