简体   繁体   中英

Database design for multiple language website

I need to create website that will support multiple languages, however I never done so and I need help with creating DB for my application.

Website will be commerce, so let's take ITEM table as an example. Only thing that I could quickly come up to is this model, but I don't think it's good since Language table is tied to Item table but I will have Company table as well and others maybe too.

Item
    ID
    Price

Language
    ID
    ItemID
    Language (example: en-US)
    Field (example: title)
    Value (example: Good Title)

Can someone help me design good database that will support multiple languages?

There are multiple ways on how to store multilanguage data in the database, I usually do it like this:

Item
  ID
  price
  title_translation_key_id (is a TranslationKey foreign key)
  desc_translation_key_id (is a TranslationKey foreign key)

TranslationKey
  ID
  key (string)

Translation
  ID
  translation_key_id (is a TranslationKey foreign key)
  content (string)
  language_id (is a Language foreign key)

Language 
  ID
  code

How to retrieve the data?

You can either work with sub-selects or if you database doesn't perform well, you can generate language specific tables out of it.

Another possible way is to keep a translations as a json object. For example

Item
  Id   Price                       Name
   1    100     {"en":"Car", "fr":"voiture", "am":"մեքենա"}

It's very simple and you don't need to implement any join to retrieve the data. However, you always have to get all translations. If you support a lot of languages this might be not so good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM