Url translation


I’m trying to build some rules to translate urls of my site.

I’ve one extension (https://github.com/codemix/yii2-localeurls) wich inserts the language code in the url but this parameter is not available to build simple rules in urlManager configuration like:


Following one tutorial (http://blog.neattutorials.com/yii2-routing-urlmanager/) i managed to write a class:


namespace common\components;

use Yii;

use yii\web\UrlRuleInterface;

use yii\base\Object;

class UrlLanguageRule extends Object implements UrlRuleInterface


    public function createUrl($manager, $route, $params)


        if ( Yii::$app->language==='it-IT'){

            if ($route === 'users/index'){

                return 'utenti';



        return false;  // this rule does not apply


    public function parseRequest($manager, $request)


        if ( Yii::$app->language==='it-IT'){

            $url = $request->getPathInfo();

            if ($url === 'utenti'){

                $route = 'users/index';

                $params = [];

                return [$route, $params];



        return false;



It function but seems a bit strange to me:in the first function ‘utenti’ is wired to ‘users/index’ in the second ‘users/index’ is wired to ‘utenti’; I have to admit I wrote this without having full understanding of what I was doing :open_mouth:

Some suggestion for better coding?



The url manager rule ‘<language:it>’ will come into play if for example you are doing this:

echo Url::to('users/index', ['language' => 'it]);

The fancy url language rule class probably appends that automatically.

Try making links like I showed you first, before applying that class.

You need to know if it works, or not.

Thanks for your reply.

In the extension documentation i find:

Anyway i gave it a try and doesn’t work.

My class works well but i think the code is a bit odd, what do you think about it?