Is selected language a global state?

Feb 21, 2011 at 4:57 PM


I have an application that looks sort of like this: NameMap – TextBoxViewModel – TextBoxView.

The NameMap contains names of the form NameMap[“us_en”] = “English Name”, NameMap[“sv”] = “Swedish Name”.

In the ShellView you can select a language and TextBoxView is supposed to display the appropriate name.

How do I tell to my TextBoxViewModel what language I have selected?

  1. The first solution I came up with was to use a static variable but I would rather not use statics and global states since they make testing harder.
  2. Another solution would be to have a “ref string selectedLanguage” as an argument in the TextBoxViewModel constructor and pass it from the ShellViewModel when I create TextBoxViewModel.
  3. Or perhaps I should use some form of Inversion of Control?



Feb 21, 2011 at 5:10 PM
Edited Feb 22, 2011 at 10:56 AM

It's not clear to me if you are speaking about application-wide localization or just per-view-model localization (I mean, if the issue is related to the entire application or just a specific view-model); if you are facing the first issue, in a similar situation I implemented a specifiec service (ICultureHost) that is used to store the currently selected culture.

Such service is either passed to VMs as a dependency (in the constructor) or retrieved using an IoC container (if you define a localizable screen, for example, I would go with the explicit usage of the IoC, to make localization transparent to the developer). It exposes a CultureInfo property and an event used to notify listeners about culture changes; this way every localizable screen is able to react to the language currently selected and act in the proper way.

This approach is easily testable, and can be used even on a per view-model basis, in case multiple services are instantiated.

Is this answer useful in your case?

Feb 22, 2011 at 9:18 AM

Yes, this answers my question.

Btw, it is an application-wide localization but it is only used in the textbox case.