1

We are developing a WPF Application which should support multiple Languages. Since we have to support some languages none of the development team knows, we agreed on allow the users (Administrators) to enter the texts for those languages. What is the best way to do so ?

2 Answers 2

3

You can follow these steps

1 Creating the Resource Files

Add this file StringResources.xaml to Ressources directory. Sample is here:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:system="clr-namespace:System;assembly=mscorlib">

     <system:String x:Key="close">Close</system:String>
</ResourceDictionary>

You can create several files, one for each language.

2 Adding the Resource (Call this when you start your application)

private void SetLanguageDictionary()
{
     ResourceDictionary dict = new ResourceDictionary();
     switch (Thread.CurrentThread.CurrentCulture.ToString())
     { 
       case "en-US":
         dict.Source = new Uri("..\\Resources\\StringResources.xaml", UriKind.Relative);
         break;
       case "fr-CA":
         dict.Source = new Uri("..\\Resources\\StringResources.fr-CA.xaml", UriKind.Relative);
         break;
       default :
         dict.Source = new Uri("..\\Resources\\StringResources.xaml",UriKind.Relative);
         break;
     }
     this.Resources.MergedDictionaries.Add(dict);
}

3 Using the Resource, like this -

<Button      
   x:Name="btnLogin"
   Click="btnLogin_Click"
   Content="{DynamicResource login}"
   Grid.Row="3"
   Grid.Column="0" 
   Padding="10" />
Sign up to request clarification or add additional context in comments.

2 Comments

How can I use this DynamicResource in CodeBehind, for example for MessageBoxes? This seems to be only a (XAML) UI-Localization.
@SeTo Y you have sample here : <Button Background="{DynamicResource brush}" /> and this code <Window.Resources> <SolidColorBrush x:Key="brush" Color="Red" /> </Window.Resources>
2

I have made very good experience with the WPFLocalizationExtension

It comes with loads of functionality (e.g binding to localized image ressources depending on the language) and the best: It's free. There are also some resx edit tools around that allow you to load and edit your resx localization files.

4 Comments

This looks promising. Though i fail to get it running. Do you know where i can get a sample?
Yes, Download the Source Code and open the solution \Tests\XamlLocalizationTest.sln there are some samples provided. And I would suggest to read through the "documentation". Key is to get the ResourceDictionaries to work in your assemblies ...
and you can read "The Idiot's Guide to using WPF Localization Extenstion (Get Started)" wpflocalizeextension.codeplex.com/discussions/283041
The WPFLocalizationExtension isn't working with Visual Studio 2012. Switched back to VS2012 and it worked like charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.