The One
The One

Reputation: 4706

How to Localize DATA

For example I have a collection of departments

ObservableCollection<Department> Departments ...

XAML

<ComboBox ItemsSource="{Binding Departments}" 
          SelectedValue="{Binding SelectedDepartmentId}"   
          SelectedValuePath="Id" 
          DisplayMemberPath="Name" />

As you can see the DisplayMemberPath is set to "Name", the name is in english.

Since this is DATA and NOT UI I don't think I should use resx resources because if a new department and it's translation is added, then it would need to recompile and release if resx where used.

How can I localize data?

Upvotes: 0

Views: 206

Answers (2)

Mark Feldman
Mark Feldman

Reputation: 16148

I personally use a custom mark-up extension, there's a good article about it here. I also give it a custom namespace which allows me to write XAML like this:

<Label Content="{Translate 'First Name:'}" />

In addition to this I've written two utilities. The first searches all my XAML files for occurrences of the "{Translate ...}" mark-up and it saves the keys (e.g. 'First Name:') in a CSV file which can then be imported into a spreadsheet for the translators or pasted into Google translate etc for testing. The second utility then takes the values from the spreadsheet and turns them into XML which then gets loaded into the appropriate dictionary for my localization manager. The English dictionary is left empty, whenever a string isn't found in a dictionary it falls back to the key itself. This system means I can use English when writing my XAML but the process of exporting and importing the keys is completely automated and I can change language of all strings in my applications instantaneously at runtime.

Upvotes: 1

The One
The One

Reputation: 4706

So this is MY approach:

Have a table in the database with the localized values

Table Departments_Localized_Names
IdDepartment  ->  Foreign key to the original department
culture       ->  Relate to the culture/language 
LocalizedDisplayName ->  Translated department name 

Using a partial entity class or even better a data transfer object, add the "LocalizedDisplayName" property, this property will be set only when the program loads, and the value will depend on the current culture

public partial class Department 
{
   //Set it at runtime according to the current culture
   public string LocalizedDisplayName{get;set;} 
}

So at the moment of displaying data, I pull the LocalizedDisplayName value from Departments_Localized_Names depending on the application current culture and the department id and set the LocalizedDisplayName on the object, that way I can just do the following:

      <ComboBox ItemsSource="{Binding Departments}" 
      SelectedValue="{Binding SelectedDepartmentId}"   
      SelectedValuePath="Id" 
      DisplayMemberPath="LocalizedDisplayName" />

When inserting the new department, I just need to make sure the translated values are added to Departments_Localized_Names, and it should work without the need of a new release.

Upvotes: 0

Related Questions