M
M
Max2016-10-09 14:19:06
Yii
Max, 2016-10-09 14:19:06

How to create a multilingual module in Yii2?

Hello. Recently, I began to adhere to the modular construction of the application and it became necessary to make them multilingual. I know about various Yii:t() , but this is not exactly what I need. Let me explain with a real example. There is a "Blog" module and I want the admin to be able to write articles in Russian, English and Chinese. At the moment, 3 implementation options are spinning in my head, but all have significant drawbacks. I would like to know how you solved such problems.
Conventionally, the table with the article has fields (which must be translated):
title | description | text | seo title | seo_description | seo_keywords
My theories boil down to the following:
1. Add duplicate columns to the same table with language prefixes (title_eng, title_zh, ...)
I do not like this one because it is some kind of GC and there is no possibility to flexibly add some new language. You will have to add new columns for new languages
​​2. Make something similar to the WP plugin, where the corresponding data is added to each column, separated by the language tag <:ru> <:en> <:zh> then it is all parsed and substituted into the necessary forms. The complexity of implementation, fuss with parsers, and frankly speaking, porridge in each column.
3. Make an additional table blog_article_lang which will contain all the necessary columns + the 'lang' column with the language designation. In essence, the has_many connection is obtained. It seems to me that this is a more or less acceptable option, but again, the code base becomes more complicated due to the fact that if 3 languages ​​​​are activated and there are required fields, then the article will not be added until the required fields are filled in all 3 forms.
Who thinks about this? How did you solve such problems? I would be glad for any good advice

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Дмитрий, 2016-10-09
@matios

Добрый день.
Вот ссылки, первые рассчитаны на yii1, но я переделывал под yii2 и всё работало отлично.
Обходился одной таблицей, где, например, для заголовка были два варианта, английский и русский, title_en and title_ru.
Последняя ссылка - расширение для yii2
раз
два
три
Расширением не пользовался, ничего не могу сказать. А по первым двум - адаптировал под свои нужды.

L
LAV45, 2016-10-09
@LAV45

Когда-то тоже искал подобное решение, но все что-то не то было.
В итоге написал собственное расширение https://github.com/LAV45/yii2-translated-behavior
На данный момент используется в 5 проектах в prodakshen
Расширение регулярно дорабатывается под различные варианты использования.
Есть демо https://yii2-translated-behavior.herokuapp.com
с открытым исходным кодом https://github.com/LAV45/yii2-translated-behavior-demo

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question