Tuco Tuco - 2 months ago 13
C# Question

How to Localize DATA

For example I have a collection of departments

ObservableCollection<Department> Departments ...


<ComboBox ItemsSource="{Binding Departments}"
SelectedValue="{Binding SelectedDepartmentId}"
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?


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}"   
      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.