There are many ways to "multi language" your wpf application - here's another one! Features:
- Simple to use via a custom WPF markup extension (like the "Binding" extension).
- Switching the language dynamically in runtime will update the associated controls immediately.
- Can be used with various sources like csv, excel or databases (rather than sattelite assemblies).
- No memory leaks by using weak references.
Using the code
First, you have to configure your ML resources. In this example, this is done in code using the DummyContextProvider (see below). Now, we want to use the framework, means: We have a WPF project with some WPF elements which we would like to assign our ML texts to. This is done by carrying out the following steps:
- Reference the "Technewlogic.MultiLanguage" assembly.
- Initialize the multiLanguage framework when the application starts.
- Make use of the "MultiLanguage" markup extension in your XAML.
public App()
{
// Initialize the MultiLanguage provider with just a dummy here.
// Implement another IContextProvider to get the ML configuration
// from another source, e.g. Excel / csv, etc.
MultiLanguageProvider.Instance.Initialize(new DummyContextProvider());
}
<TextBlock Text="{ext:MultiLanguage MainWindow.CloseApplicationText}" />
The Configuration Model
Internally, the framework consists of a ConfigurationContext class which is the entry point for the ML configuration. It aggregates a list of LanguageCategories. A LanguageCategory can be used to summarize certain ML keys. For example, you can create one LanguageCategory for each page in your application. A LanguageCategory consists of several LanguageEntries which represent a logical ML text in your app. For example, in the "MainPage" category you can find a "CloseText" entry which can be applied to a button that will close the page. Each LanguageEntry holds a list of LanguageTexts - one for each language you want to support. Have a look at the DummyContextProvider class. There you can see how the model can be build up.
The Editor App
To make configuration simple, I have written a small editor application:
Implementation of the Framework
The API to work with is the MultiLanguage markup extension class which can be used in XAML. To keep things simple for the user of the framework, the instances of that class access the static MultiLanguageProvider which holds the ML configuration. The required information have to be provided by an implementation of IContextProvider. Implement this interface if you want to store your ML infos in an external location, for example in a CSV of Excel file. In this example, there's just a dummy inplementation which shows how it should look like. The MultiLanguageProvider class also manages language changes and informs all the MultiLanguage instances which will update the properties they are bound to. This is done via WeakReferences.
The Sample Application
The sample application uses the code explained above. Use the combo box to dynamically switch between the configured languages!
Have fun!
VS2008 / Licence: LGPL
WpfMultiLanguage_src.zip (196,7 kb)
WpfMultiLanguage_bin.zip (199,1 kb)