How to support Multi-Languages approach in DataBase Schema?

后端 未结 2 681
小蘑菇
小蘑菇 2021-01-04 21:30

I want my database to support multi Languages for all text values in its tables.

So what is the best approach to do that?.

Edit1

2条回答
  •  粉色の甜心
    2021-01-04 22:15

    I've just implemented something which is working very well.

    Like many others, I've not had a clean slate to start with, and all tables were geared up to store English text.

    I was not particularly happy to double the number of tables, or columns to achieve a multilingual database.

    I decided to utilise XML as a way to store all translations, into a single field:

    
      good day
      bonjour
    
    

    So for example, I started with a table which only holds English:

    ProductId INT
    ProductName varchar(200)
    ProductDescription varchar(1000)
    

    I then created the multilingual fields:

    ProductId INT
    ProductNameTranslation xml
    ProductDescriptionTranslation xml
    

    If you want to be able to get a read-only value for the original fields, you can simply add a persisted computed columns:

    ProductId INT
    ProductNameTranslation xml
    ProductDescriptionTranslation xml
    ProductNameDefaultLang = dbo.GetDefaultLanguage(ProductNameTranslation)
    ProductDescriptionDefaultLang = dbo.GetDefaultLanguage(ProductDescriptionTranslation)
    

    In the business/data layer, the XML field is converted in to a dictionary type business class, with key being a language enumeration:

    ProductName {
      get {return this.ProductNameTranslation[selectedLanguage];}
      set {...}
    }
    

    This approach allowed me to avoid having to completely re-carve up the database. It also means less interaction with the database, one row to be saved instead of one main row, and 3 translation rows.

    Also it avoids the issue of having to handle cases where the main row has no translation rows

    Should be noted that the XML had a schema and XML indexes to boost performance.

    This approach is very useful where you want to do the language conversion incrementally i.e. one field at a time.

提交回复
热议问题