views:

26

answers:

1

I'm new to OOD so I have a question about the use of classes for creating multilingual UI.

I would like to create a class that is available to all forms in my app so i could change UI language whenever I want. The basic idea is in keeping language resources in xml files and creating data bindings for all controls so the Text property is changed whenever i load a new language resource file.

I ended up creating bindings for all controls on app's main form and when the form loads binding source's datasource class reads strings from fields of a language-storage class. Everything is ok with this until i want to change UI language of all other forms simultaneously. Data binding for static classes doesn't seem to work or i'm just too stupid to use it.

.NET 3.5, C#, VS2008 Express

A: 

Solution FOR WPF

You need to create to XML files, the first will contain localization of static controls (means controls that acquire there localization strings on OnApplyTemplate point), the second will contain localization strings for dynamic forms like a MessageBox or whereever you will change conten of control dynamically. Then for dynamic strings you need to decribe enum, which values should be keys of DynamicStrings.xml (for example).

This is base. Then on change of language you need to fill two dictionaries with keys and values for both of localization string types. Also you will need the manager, who will care about how to obtain concrete string for every place it needs appear.

For controls: bind to any string property, for example, name it EmptyText, then write converter which will obtain as a converter parameter Key of static dictionary and return value of that key:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter != null && parameter is string)
                       return MessageManager.Instance.GetUITextById((string)parameter);
            return value;
        }

For dynamic controls: simply pass as an initialize parameters for them Method of your Manager:

MessageBox.Show(MessageManager.Instance.GetDynamicMessage(Messages.HelloWorldMessage));

Where MessageManager (use singleton pattern) - is you class that handle all your localization manipulations with xmls and dictionaries, GetDynamicMessage will obtain string by enum value from DynamicDictionary, Messages - your enum.

Eugene Cheverda
He's using **Windows Forms** as stated in his comment.
Jaroslav Jandek
Eugene, thanks for the answer. Singleton pattern seems suitable for my app so i'll try using it
7kun