WPF: localization in MVVM

Well, no screenshot, no code this time.
After my last post about localization in WPF,  the solution is ready and running in Corrado‘s post and Mauro‘s post, screenshot and code included!!!
Unfortunately it’s italian lang, but that’s not a big deal… because a line of code tells more that 100 words. 😉 (cit. ?)

This solution is as simpler as powerfull: resources in viewmodel via viewmodel’s property, and INotifyPropertyChanged raised on language resources updated.

Enjoy them! (simil-cit. 🙂 )

source : Corrado’s post, Localization of Model View ViewModel based applications
source: Mauro’s post, Premessa: doverosa 🙂

Technorati tags: WPF,MVVM,Localization
Advertisements

Tags: ,

5 Responses to “WPF: localization in MVVM”

  1. Jeff Britton Says:

    Corrado’s post has the following line
    private static Strings resources = new Strings();

    However, all the members of Strings are static.
    So the variable is unable to retrieve the localized text.

  2. robertoschiabel Says:

    I’m very sorry; I can’t understand your point. 😦 (It’s me, not you!)

    In Corrado’s code, variable LocalizedText (in LocalizableViewModel class) is public, same type as resources class (Strings);
    xaml bindings refer to LocalizedText, not “resources”.

    Could you please tell me more about your observation?
    Thank you very much.

  3. Jeff Britton Says:

    Well, I just created an application using the code from Corrado’s post and I can verify that it does work. So therefore I just don’t understand something. The thing that is confusing to me is that all the members of the Strings class are static. So you can use
    Strings.WelcomeText. However, you can’t do this
    Strings().WelcomeText. WelcomeText is not accessible on an instance of the Strings class. I just learned this fact. It would be accessible in Java and C++, but not C#.
    In LocalizableViewModel an instance of the Strings class is created and this instance is returned by the LocalizedText property. So it would appear that WelcomeText should not be accessible.
    I have to conclude that some magic is happening in the binding, perhaps, getting the class type from the instance and then using reflection?
    I will need to read up on bindings. Or maybe you can clarify this?

    I suppose it is also worth noting something else. In my very limited knowledge of MVVM it would seem that the user interface will sometimes bind to a property returning a List of objects. A data template might be used to display this list in an ItemsControl. The text to be displayed is embedded in each object. I don’t know how to make this scenario work using the technique presented in Corrado’s post. Somehow the object would need to instead contain a way to find the string via the LocalizedText call.

    If you could show an example of binding to a List and displaying the result in an ItemsControl using this technique I think that would be very helpful.

  4. robertoschiabel Says:

    First of all, I’m sorry I was (… I’m still… )very busy last days.
    I have no answer to your questions, right now.

    Q#1
    but maybe you find usefull info here
    http://social.msdn.microsoft.com/forums/en-US/wpf/thread/257a41be-8168-401c-a915-cdc44e195a3f
    http://www.dreamincode.net/forums/topic/122587-wpf-two-way-binding-to-a-static-property/
    http://blogs.microsoft.co.il/blogs/tamir/archive/2008/04/22/quick-wpf-tip-how-to-bind-to-wpf-application-resources-and-settings.aspx

    also look for “x:Static” XAML attribute

    Q#2
    no example available

    I’m sorry.
    I hope I’ll have answers asap. 🙂

  5. Boernie’s Words » Blog Archive » WPF Localization with Resource Files and Dynamic Properties - All that goes through my mind that I’d like to share… Says:

    […] a developer’s breadcrumb […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: