Search code examples
c#architecturelocalizationglobalization

Globalization architecture


I need to store products for an e-commerce solution in a database. Each product should have descriptive information, such as name, description etc.

I need any product to be localized to x number of languages.

What I've done so far, is to make any column that should be localized and nvarchar(MAX) and then i store an XML string like this:

<cultures>
    <culture code="en-us">Super fast laptop</culture>
    <culture code="da-dk">Super hurtig bærbar</culture>
</cultures>

And when I load it from the database, into my business logic objects, I parse the XML string to a Dictionary<string, string> where the key is the culture/language code.

So when I want to display the name of a product, I do this:

lblName.Text = product.Name["en-us"];

Does anyone have a better solution?


Solution

  • You should store the current language somewhere (in a singleton, for instance) and in the product.Name property use the language setting to get the correct string. This way you only have to write the language specific code once for each field rather than thinking about languages everywhere the field is used.

    For example, assuming your singleton is defined in the Localizer class that stores an enum corresponding to the current language:

    public class Product
    {
      private idType id;
      public string Name
      {
        get
        {
          return Localizer.Instance.GetLocalString(id, "Name");
        }
      }
    }
    

    Where GetLocalString looks something like:

      public string GetLocalString(idType objectId, string fieldName)
      {
        switch (_currentLanguage)
        {
          case Language.English:
            // db access code to retrieve your string, may need to include the table
            // the object is in (e.g. "Products" "Orders" etc.)
            db.GetValue(objectId, fieldName, "en-us");
            break;
        }
      }