Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 266
L-Four Avatar asked Jan 31 '11 07:01

L-Four


People also ask

What is XAML?

Extensible Application Markup Language (XAML) is a declarative language that's based on XML. XAML is used extensively in the following types of applications to build user interfaces: Windows Presentation Foundation (WPF) apps. Universal Windows Platform (UWP) apps.

What is multi language application?

What Defines a Multilingual Application or Website? A multilingual website or application is a digital service or product available in several languages serving an international audience.

Is WPF a language?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.


2 Answers

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

like image 120
ColinE Avatar answered Oct 13 '22 13:10

ColinE


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

like image 41
L-Four Avatar answered Oct 13 '22 13:10

L-Four