Localization of Database Strings in .Net

Posted by Aligned on Geeks with Blogs See other posts from Geeks with Blogs or by Aligned
Published on Mon, 26 Nov 2012 16:33:46 GMT Indexed on 2012/11/26 17:06 UTC
Read the original article Hit count: 164

Filed under:

I have several database tables that have a description column that I need to display in the UI. .Net has .resx files that will help with the translation of the strings, when the Thread.CurrentCulture.UICulture is set, but I needed a custom approach for the strings that are stored in the database and not in the .resx files.

Here’s my approach:

1. Create a resource file for each database table and put them in the /Resources/Database/ directory.

2. Create a method in LocalizationHelpers (GetLocalizedString) that will get the string from the table for English (which should be cached to avoid unneeded service/database calls) or the resx when not English.

3. All database tables need to have a ResxKey field that matches the key in the resx file.

4. By convention the resx file will have the same name as the database table, and the key the same as the database ResxKey.
  - if there are multiple columns that need translation, then one ResxKey will be used and Name or Description appended.

Here’s the method I’m using to pull the string:

public static string GetLocalizedString(string resourceName, string resourceKey)
{
  if (executingAssembly == null)
  {
    executingAssembly = Assembly.GetExecutingAssembly();
  }
 
  ResourceManager manager = new ResourceManager(resourceName, executingAssembly);
  return manager.GetString(resourceKey);
}

© Geeks with Blogs or respective owner