简体   繁体   中英

Is it a good idea to place translations in JSON?

I like to forward the concept of Relation DB, so I usually create tables. But now I get one task, that made me think about using a json field with translations.

Briefly about the task: It is necessary to implement a multi-lingual application. I usually create tables:

  • contents
  • content_translations
  • contents_images
  • contents_image_translations

Often they do not turn out much (around 5-7). But now I need to import some DB with GEO data (countries, regions, cities, localities).I tried to create 1 translation table, but then we lose the ability to work with foreign keys and play with polymorphic connections. My example is not so big, but I have a lot of this tables, and should create too much tables with translations (and they only contain Id, Relation_id and Name).

That's wрy I want to try to work with JSON field. MySQL 5.7 provides this kind of fields. Structure of the field is like this:

...
json_encode([
    'ru' => ['locale' => 'ru', 'name' => $data['title_ru']],
    'en' => ['locale' => 'en', 'name' => $data['title_en']],
]);
...

Sample of selection:

SELECT
  alias, translations,
  JSON_UNQUOTE(json_extract(translations, '$.en.name')) AS like_name
FROM geo_countries
WHERE
  JSON_UNQUOTE(json_extract(translations, '$.en.name')) LIKE '%Ukraine%'
  OR
  JSON_UNQUOTE(json_extract(translations, '$.ru.name')) LIKE '%Ukraine%';

So question is: The question is: Who works with this approach, is there a profit, how does production show itself?

You should try and use a standard approach such as used with CakePHP or WordPress or with Laravel .

They all have database tables that specify which locale you are going to use. Then you can further store your translations in the database.

I would stick to a standard model for handling this structure of translations. Only in pure JavaScript implementations are they trying to use JSON data for translation tables.

  1. CakePHP Translations in the database
  2. Laravel translations
  3. WordPress translations
  4. POEdit

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