我想知道在 Yii 框架中实现“国际化(i18n)”和“动态 URL 管理”的更好方法。
一个(难以维护的)临时解决方案:
// protected/config/main.php
'language' => 'es',
...
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
// pages
'es/turismo/<slug:>' => array('visit/page', 'defaultParams' => array('lang' => 'es'), 'urlSuffix' => '.html'),
'it/visita/<slug:>' => array('visit/page', 'defaultParams' => array('lang' => 'it'), 'urlSuffix' => '.html'),
'en/travel/<slug:>' => array('visit/page', 'defaultParams' => array('lang' => 'en'), 'urlSuffix' => '.html'),
'turismo/<slug:>' => array('visit/page', 'urlSuffix' => '.html'),
// home
'es/turismo' => array('visit/index', 'defaultParams' => array('lang' => 'es'), 'urlSuffix' => '.html'),
'it/visita' => array('visit/index', 'defaultParams' => array('lang' => 'it'), 'urlSuffix' => '.html'),
'en/travel' => array('visit/index', 'defaultParams' => array('lang' => 'en'), 'urlSuffix' => '.html'),
// contact us
'es/contactenos' => array('site/contact', 'defaultParams' => array('lang' => 'es'), 'urlSuffix' => '.html'),
'it/contattaci' => array('site/contact', 'defaultParams' => array('lang' => 'it'), 'urlSuffix' => '.html'),
'en/contact-us' => array('site/contact', 'defaultParams' => array('lang' => 'en'), 'urlSuffix' => '.html'),
),
),
...
// protected/controllers/VisitController.php
...
public function actionIndex($lang = 'es'){
Yii::app()->language = $lang;
...
}
public function actionPage($slug, $lang = 'es'){
Yii::app()->language = $lang;
...
}
...
这意味着,每个控制器的新配置“urlManager”规则并始终为每个控制器操作传递 $lang 参数。一个活生生的例子:
我尝试了其他一些选项但没有成功:
谢谢!