Urlmanager Translation Rules

Hi,

[size="5"]a )[/size]

I try to use translation in urlManager rules but it doesn’t work. Here is the code from config file main.php.


'urlManager'=>array(

    'urlFormat'=>'path',

    'useStrictParsing' => true,

    'showScriptName' => false,

    'rules'=>array(

        Yii::t('glob','new-user') => 'user/create',

    ),

),

After switching language from "en" to the other language there is no change in url.

I just wanted to avoid to write "redundant" rows of code for the same action in different languages - like this


'urlManager'=>array(

    'urlFormat'=>'path',

    'useStrictParsing' => true,

    'showScriptName' => false,

    'rules'=>array(

        'new-user'           => 'user/create',

        'neue-benutzer'      => 'user/create',

        'nouvel-utilisateur' => 'user/create',

    ),

),

[size="5"]b )[/size]

When I have the following rules. It’s obvious which language is selected based on the strings (new-user, neue-benutzer,…).


'urlManager'=>array(

    'urlFormat'=>'path',

    'useStrictParsing' => true,

    'showScriptName' => false,

    'rules'=>array(

        'new-user'           => 'user/create',

        'neue-benutzer'      => 'user/create',

        'nouvel-utilisateur' => 'user/create',

    ),

),

Therefore I would like to set for example $_GET[‘lang’] for every language pattern separately (to avoid the next handling in Controller). My imagination is like this:


'urlManager'=>array(

    'urlFormat'=>'path',

    'useStrictParsing' => true,

    'showScriptName' => false,

    'rules'=>array(

        'new-user'           => array('user/create', 'lang'=>'en'),

        'neue-benutzer'      => array('user/create', 'lang'=>'de'),

        'nouvel-utilisateur' => array('user/create', 'lang'=>'fr'),

    ),

),

a) you cannot do that in config file, because Yii::t looks for Yii::app()->language, but at this time application is not initialized yet… you could extend basic CUrlManager and add routes in init() handler (that is one possible solution)

B) in fact you can do that but with slightly different notation:




'new-user' => array( 'user/create', 'matchValue'=>true, 'defaultParams' => array( 'lang'=>'en' ) ),



matchValue gives you additional benefit when reverse mapping url with createUrl, normalizeUrl, etc. when you call it with ‘lang’ parameter it will choose proper language version automatically.

Thanks a lot.

Meantime I solved the [size="4"]b )[/size]. You also wrote about defaultParams.


'urlManager'=>array(

    'urlFormat'=>'path',

    'useStrictParsing' => true,

    'showScriptName' => false,

    'rules'=>array(

        'new-user'           => array('user/create', 'defaultParams'=>array('lang'=>'en')),

        'neue-benutzer'      => array('user/create', 'defaultParams'=>array('lang'=>'de')),

        'nouvel-utilisateur' => array('user/create', 'defaultParams'=>array('lang'=>'fr')),

    ),

),