The URL pattern to support the multilanguage view

There are different ways to display the same view in different languages. A basic approach to support multilanguage views could be to insert a language code at the start of the route. For example, the previous route news/index will become en/news/index in English language, it/news/index in Italian language, fr/news/index in French language, and so on.

Append this rule in the rules property of UrlManager:

[
    'pattern' => '<lang:\w+>/<controller>/<action>',
    'route' => '<controller>/<action>',
],

All the requests that have a language ID as the prefix in the path info, will be matched and passed to the <controller>/<action> route with the $lang parameters passed in GET.

Now, create a new action named

Get Yii2 By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.