开发者

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:

  • It's a PRISM application, so a number of independent modules (assemblies) working together. I would like that each assembly has its own translations of UI elements.
  • I need开发者_如何学编程 a simple approach, no tools needed to generate stuff
  • Should still be able to use blend to design the UI
  • Optionally be able to switch language without restarting the application (not a dealbreaker)

Can someone advice me on how to achieve this?

Thanks!


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 ...


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜