The Idiot's Guide to using WPF Localization Extenstion (Get Started)

Dec 15 2011 at 7:31 AM
Edited Jan 2 at 1:38 PM

Hulloes!

If you're like me you need an idiot's guide to get started. Since there isn't any about, I say I'm hopefully idiot enough to write one covering the basics. By a newbie, for the newbies.

Now that the pleasentries are out of the way, let's get dirty.


Assuming you've downloaded the library and setup your project in Visual Studio, go ahead and right click the References in the Solution Explorer and import the dll (WPFLocalizationExtension.dll).

Add a resource file, I will call it Strings.resx, to your project. Make the Access Modifier of it to internal (check this link out for how-to on that)

In the Strings.resx files I add all the text I want to translate. For example, I have:
Button_Back
Button_Next

Their values? They point onwards to specific language resource files by adding like so:
@Button_Back
@Button_Next

Then we add the language specific .resx files: I have Strings.en-GB.resx and Strings.sv-SE.resx
They contain this (Name - Value):

EN
Button_Back - Back
Button_Next - Next

SV
Button_Back - Tillbaka
Button_Next - Nästa


Go ahead and add two buttons to an .xaml page.


In the imports add the following:

xmlns:Engine="clr-namespace:WPFLocalizeExtension.Engine;assembly=WPFLocalizeExtension" 
Engine:LocalizeDictionary.DesignCulture="en-GB"
xmlns:lex="clr-namespace:WPFLocalizeExtension.Extensions;assembly=WPFLocalizeExtension"

By these rows of code we've imported the WPF Localization Extention library and set the design culture (the language you see while desning stuff in Visual Studio/Blend) to English (GB).


Great! Now let's link our button's contents to the strings in our resources.
Add the following code to your buttons:

Content="{lex:LocText Key=Button_Next, Dict=Strings, Assembly=NameOfYourAssembly}"
Content="{lex:LocText Key=Button_Back, Dict=Strings, Assembly=NameOfYourAssembly}"

Alternatively, you can use the following format instead:

Content="{lex:LocText NameOfYourAssembly:Strings:Button_Next}"
Content="{lex:LocText  NameOfYourAssembly:Strings:Button_Back}"

Naturally you change NameOfYourAssembly to the name... of you assembly!
Go ahead and change the Design culture (mentioned above) to "sv-SE" to see the text of the buttons instantly change to another lagnuage. Magic - huzzah!

 

So, how do we change language in the code? Here's a way:

public void SetLocale(string locale)
{
      LocalizeDictionary.Instance.Culture = CultureInfo.GetCultureInfo(locale);
}

public void SetLocale(CultureInfo culture)
{
      LocalizeDictionary.Instance.Culture = culture;
}

(Note that you must add a using statement in the file to WPFLocalizeExtension.Engine)
The string in SetLocale is formated like this: "sv-SE", or "en-GB" or "en-US", etc. (For a list of culture codes, go here)


But how do you access the resource in the code and not in the XAML? Here's a way!

public string GetUIString(string key)
{
      string uiString;
      LocTextExtension locExtension = new LocTextExtension(key);
      locExtension.ResolveLocalizedValue(out uiString);
      return uiString;
}

(Not that you must add a using statement in the file to WPFLocalizeExtension.Extensions)
The key string looks like this: "NameOfYourAssembly:Strings:Button_Next" (Assembly : ResourceFileName : ResourceName)

 

There we go, I've only just begun using this excellent library, and I hope that this guide might help someone get started as well.

Cheers,
Amadeus

Coordinator
Dec 15 2011 at 8:41 AM

Weee!!

Thank you amadeus for this GREAT post!
i will add your post to the start page :)

here is some hint:

Content="{lex:LocTextExtension Key=Button_Next, Dict=Strings, Assembly=NameOfYourAssembly}"

is the same as

Content="{lex:LocText  Button_Next:Strings:NameOfYourAssembly}"

Because i used the naming convention for extensions, the "Extension" part can be omitted.

 

thank you so much for this,
Bernhard

Dec 15 2011 at 11:35 AM

Thanks! :)
Didn't know you could use the shortened version, will add it to the guide.

Started using this library in our project two days ago, and we find it fantastic - so a big thumbs up from us!

Cheers,
Amadeus

Coordinator
Dec 15 2011 at 11:44 AM
Edited Dec 15 2011 at 11:45 AM

nice!

may i can use your company name on the landing page like SAP?

edit: you have duplicated lines in your post...

Jan 2 at 1:21 PM

The following syntax didn't work for me: 

Content="{lex:LocText Button_Next:Strings:NameOfYourAssembly}"

 

Rather I reversed the order and it worked!

 Content="{lex:LocText NameOfYourAssembly:Strings:Button_Next}"

 

Hope this helps :)

Jan 2 at 1:40 PM
elninoisback wrote:

The following syntax didn't work for me: 

Content="{lex:LocText Button_Next:Strings:NameOfYourAssembly}"

 

Rather I reversed the order and it worked!

 Content="{lex:LocText NameOfYourAssembly:Strings:Button_Next}"

 

Hope this helps :)

Fixed it, thanks! :)

Jan 16 at 12:19 AM
Edited Jan 16 at 12:25 AM

Nice extension, i will be using this in my next wpf project :)

Also.. is there a way to update the GUI components that I created dinamically ( from code ) ?

For example, let's say I dinamically create a Label. The key string for it would be "labelContent", for which I have two values, one in Strings.resx and the other one in Strings.fr-FR.resx.

At runtime, I create the label and set it's Content property:

myLabel.Content = GetUIString("myAssembly:Strings:labelContent");

However, if I change the culture, the label content stays the same.

Coordinator
Jan 16 at 8:31 AM

Hi msmihai,

please try this:

// create label
Label testLabel = new Label();
// create extension
var locTextExtension = new LocTextExtension("WpfAppLocTest::Test");
// optional: set a forced culture -> the output stays the same
locTextExtension.ForceCulture = "en";
// set the binding to the content property of the label 
locTextExtension.SetBinding(testLabel, Label.ContentProperty);
Have a nice day!

Jan 20 at 10:59 AM

I am probably missing something because I've just started with WPF but i can't get it to work. I followed step by step the tutorial three times but I keep getting the same problem so must be me not understanding something.

http://i.imgur.com/aHnWL.png

I'm getting the following exception

System.Windows.Markup.XamlParseException {"'Provide value on 'WPFLocalizeExtension.Extensions.LocTextExtension' threw an exception.' Line number '7' and line position '24'."}

Any suggestions?

 

Thanks a lot

Coordinator
Jan 20 at 11:27 AM

Hi Curial,

ive send you a direct email.

this problem is curious because the Line number 7 contains only the definition of the namespace o.0.

i will look into it, please send me the example project as described in the direct message.

thanks

Coordinator
Jan 20 at 2:09 PM

Hi Curial,

ive updated the code on github, the downloads on codeplex and the nuget packet.

VB.net should now can use my extension.

Feb 9 at 8:41 AM

Hi,

How set Window.Title ?

<Window x:Class="FigureViewerNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Engine="clr-namespace:WPFLocalizeExtension.Engine;assembly=WPFLocalizeExtension"
        Engine:LocalizeDictionary.DesignCulture="ru-RU"
        xmlns:lex="clr-namespace:WPFLocalizeExtension.Extensions;assembly=WPFLocalizeExtension"
        Title="{lex:LocText FigureViewer:Strings:caption_App}"  //it don't work

 

Feb 9 at 8:53 AM
seriousm wrote:

Hi Curial,

ive updated the code on github, the downloads on codeplex and the nuget packet.

VB.net should now can use my extension.

Hey seriousm, I didn't thakn you, how rude of me. Thanks for the quick update :)

Feb 9 at 10:44 AM
eddy_cs wrote:

How set Window.Title ?

Use DisplayName property on the Screen.