警予:国际化(i18n)和动态URL经理(yii: internationalization (i1

2019-07-30 20:17发布

我想知道,以实现Yii框架“国际化(i18n)”和“动态URL管理”更好的办法。

A(难以保持)临时解决方案:

// 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参数。 一个活生生的例子:

montmartrebutte.com

我尝试没有成功,其他一些选项:

Yii框架论坛:动态URL经理路线

谢谢!

Answer 1:

规则:

'<lang:(es|it|en)>/(turismo|visita|travel)/<slug:>' => array('visit/page', 'urlSuffix' => '.html'),

控制器:

class VisitController extends Controller
{
    public function actionPage($slug)
    {

    }
}

家长控制(如果你不使用模块):

class Controller extends CController
{
    public function beforeAction($action)
    {
        Yii::app()->language = Yii::app()->request->getQuery('lang', 'es');
        return parent::beforeAction($action);
    }
}


文章来源: yii: internationalization (i18n) and dynamic url manager