L-Four
L-Four

Reputation: 13541

Approach for a multi-lingual WPF application

it seems there are a number of approaches on how to implement multiple languages in a WPF application. But I would like some more information about what method I should be using with the following requirements:

Can someone advice me on how to achieve this?

Thanks!

Upvotes: 9

Views: 2425

Answers (2)

L-Four
L-Four

Reputation: 13541

Meanwhile I found an open source project that works really well: http://wpflocalizeextension.codeplex.com. It's just adding a reference to the dll, adding the resources with translations, and using it in XAML. It worked in 5 minutes. I can add multiple resources to individual modules; and it works fine in visual studio designer and blend. And, locale can be changed on the fly. Meets my requirements :)

Upvotes: 3

ColinE
ColinE

Reputation: 70162

A common approach is to bind the text property of your textblocks / labels etc.. to some property on a statically defined localization resource:

<Label Content="{Binding Source={x:Static loc:LanguageContext.Instance}, 
                         Path=Dictionary,  Mode=OneWay,
                         Converter={StaticResource languageConverter},
                         ConverterParameter=TextId}" />

i.e. LanguageContext.Instance exposes a dictionary via a property Dictionary, the Converter uses the given ConverterParameter to look up the text identified via TextId.

This is a cumbersome approach, and will not fulfil all your requirements.

A better method is to defined your own markup extension to perform this sort of logic. There are a couple of solutions I have seen on the web, this high rated codeproject article:

http://www.codeproject.com/KB/WPF/realtime_multilingual.aspx

And a similar solution here that provides Blend, on-the-fly language changes, so is probably a good choice for you:

http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/10/30/wpf-localization-on-the-fly-language-selection.aspx

With the above example you define an attached property which identifies the key of the translated item, and use the Translate markup extension to identify the properties which are translated.

NOTE: it is not just text which is being translated here, often you have to change colors / graphics etc ...

Upvotes: 4

Related Questions