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