A
A
Andrey Pavlenko2014-06-17 11:58:17
Yii
Andrey Pavlenko, 2014-06-17 11:58:17

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.
But as I understand it, in version 2 (or maybe earlier), it became possible to give the UrlRule class as a rule, and in this class already make your own parsing rules: read the raw documentation . Is it possible to use this class to automatically add the language to the site address? For example, I do this: , and the "ru" part at the beginning of the address bar will be added transparently, without additional instructions. Will it work or do I still need to redefine UrlManager? Maybe there is a more elegant solution that I didn't know about?Yii::$app->urlManager->createUrl('video/index');
2) the link for all languages ​​is the same; on the first visit, we take information about the language from the properties of the browser or show a modal in which we suggest choosing a language. When choosing a language, we write information in cookies, and the next time we take it from there. As I understand it, this can harm SEO a little, or is there no difference?
3) combination of methods. We also write information about the language in cookies, but we also show it in the url (it seemed to be the most optimal, although it requires a dance with a tambourine).
Which method is preferable?
Also, would it be possible to create rules by extending the UrlRule class?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
O
Oleg Malinovsky, 2015-10-13
@kwazaro

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]);
}

Now, when selecting data from the first table, texts from the second table will be pulled up in the current application language.
3) To manage the current language of the application and conveniently switch from one language to another (the language parameter is stored in the link), there is a wonderful Yii2 Locale URLs component

M
Mikhail Osher, 2014-06-17
@miraage

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...

D
developinwed, 2014-06-20
@developinwed

Yii2 and the organization of multilingualism

L
LAV45, 2015-11-05
@LAV45

In fact, everything is very easy to organize, you can peek here https://github.com/LAV45/yii2-translated-behavior#...

A
at0m1x, 2016-08-14
@at0m1x

UrlManager is most likely to be overridden, but it is not necessary to do it yourself, you can use for example: https://github.com/codemix/yii2-localeurls
This component allows you to avoid editing URL rules.
You can read more about how to use it here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question