Answer the question
In order to leave comments, you need to log in
Multilanguage in Yii2: how to implement URL rules?
There are enough entries on internet blogs on how to implement multilingualism in Yii, for example here or here .
But Yii2 has brought a lot of changes, and time does not stand still, so the question arose, how best to implement url-management of multilingualism now? Perhaps there are new techniques?
As I understand it, there are several approaches.
1) direct indication of the language in the link and redefinition of UrlManager. For example, add the rule:
'<language:(ru|ua|en)>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>'
and the class is redefined. This is how it was in Yii1. Yii::$app->urlManager->createUrl('video/index');
Answer the question
In order to leave comments, you need to log in
I use the following approach:
1) In the database, information about pages (or products, or other entities) is stored in two tables: for example, page and page_content . The first table is data that does not require translation (for example, id, creation date, update date, slug, etc.). The second table is text data that will be multilingual (meta_title, meta_description, meta_keywords, content). In the second table (with content), there should be a field for linking to the first table (eg page_id field ) , as well as a field for specifying the language code. It turns out that for one record in the first table, there are several records in the second table, each of which is in different languages.
2) In the models, you need to register a one-to-one relationship for these tables, in which the fields by ID and by the language field will be binding (the parameter is taken from the Yii::$app->language application settings):
public function getPageContent()
{
return $this->hasOne(PageContent::className(), ['page_id' => 'id'])->andWhere(['page_content.language_code' => Yii::$app->language]);
}
I think the second one can do the same.
Read from the item "Transparent work with language addresses" (ctrl+f).
www.elisdn.ru/blog/39/yii-based-multilanguage-site...
In fact, everything is very easy to organize, you can peek here https://github.com/LAV45/yii2-translated-behavior#...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question