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 actionInternationalIndex
in NewsController
to test the multilanguage support:
public function actionInternationalIndex() { // if missing, value will be 'en' $lang = Yii::$app->request-...