Home / WPF / WPF Resources / WPF Localization based on Database with Design support

WPF Localization based on Database with Design support

Get the lastest source code with examples from my github repository.

Time ago i wrote a post on using a library that permitted to get strings from a database and using them as a resx resource to localize the application.

As pointed out by Scott Whitlock, getting the strings in the code-behind was not “the right way”, it would be better to use markup extension so it’s not needed to mantain a lot of code-behind for translations.

Using the resx files would be okay, but there are still problems (check this article for more):

  • The localization process is not integrated into the standard Visual Studio build mechanism (as it is for Windows Forms applications).
  • There is no way to view or edit the localized XAML within the Visual Studio designer.
  • Locbaml uses CSV files and has issues when the translated text includes commas. The use of CSV files forces translators to work with two separate mechanisms since they still have to work with standard .NET RESX files for programmatically translated strings. Additionally, you might need to compare csv files side by side to know about the change in the content of the file before translation and after translation.
  • The Locbaml approach results in the complete binary XAML for the window being replicated in the satellite assemblies for each localized language. This results in a much larger footprint for localized applications compared to the Windows Forms approach where only those resources that differ from the invariant culture are compiled into the satellite assemblies.
  • There is no way to dynamically change the language of the application at runtime without closing and recreating windows.

So after searching for a good solution i decided to integrate the DatabaseResourceManager library inside this one: http://localizationsupport.codeplex.com/

You can find a working example here: https://www.mesta-automation.com/Downloads/LocalizationSupport.rar

To use this library with your solution you need to:

1- Add as reference the library

2- Create the database

3- When you load the ResourceFileProvider you have to set the connection string for your database

ResourceFileProvider  _provider  =  new  ResourceFileProvider( [HERE YOUR CONNECTION STRING] );
public  ResourceFileProvider  Provider
{
    get  {  return  _provider;  }
}

4- Check the class DatabaseResourceManager (inside file Translation.cs) for queries ONLY IF YOU MODIFIED THE TABLE NAME OR FIELDS.
With this class you can use this xaml:

<Button  Content="{l:Localize  Key=btnButton,DefaultValue=Button}"  Height="31"  HorizontalAlignment="Left"  Margin="327,12,0,0"  Name="btnButton"  VerticalAlignment="Top"  Width="124"  />
<Label  Content="{l:Localize  Key=lblLabel,DefaultValue=Label}"  Height="29"  HorizontalAlignment="Left"  Margin="327,54,0,0"  Name="lblLabel"  VerticalAlignment="Top"  Width="123"  BorderThickness="1"  BorderBrush="Black"  />
<Label  Content="{Binding  CurrentDate}"  BorderBrush="Black"  BorderThickness="1"  Height="29"  HorizontalAlignment="Left"  Margin="327,113,0,0"  VerticalAlignment="Top"  Width="124"  />
<Label  Content="{l:Localize  Key=lblDateTime,DefaultValue=Data:}"  Height="27"  HorizontalAlignment="Left"  Margin="327,93,0,0"  Name="lblDateTime"  VerticalAlignment="Top"  Width="124"  />

And forget the code behind.

Download the sample application from here to check how it works.

3 comments

  1. Hey Mesta,

    i’ve been looking all around the web for something like this Provider.
    But you use an Access DB – did you ever try it with an SQL Server with multiple Tables?
    I tryed to change your example as so that it should connect an SQL DB – with no results.
    I couldnt get it to work.

    Any help – i would appreciate it

    regards from switzerland
    Mike

Leave a Reply

Your email address will not be published.