在URL中路由Zend Framework 2语言

dir*_*ory 5 parameters url translation routes zend-framework2

对于我的应用程序翻译,我想使用一种语言结构,例如:

  • site.com(英文)
  • site.com/de/(德语)
  • site.com/fr/(法国)
  • site.com/nl/(荷兰语)

等等..

我可以使用Literal中的路由器选项轻松地完成此操作,因为'[az] {2}'但我想排除我不支持的语言,例如site.com/it/如果不支持我想要404.我试过使用正则表达式(添加支持的语言)修复此问题但是(我不知道)出错了.

提前致谢!

'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'language' => array(
                        'type'    => 'Regex',
                        'options' => array(
                            'regex'    => '/(?<lang>(de|fr|nl))?',
                            'defaults' => array(
                                'lang' => 'en', //default
                            ),
                            'spec' => '/%lang%',
                        ),
                    ),
                ),
            ),
        ),
    ),
Run Code Online (Sandbox Code Playgroud)

Cri*_*isp 6

我认为你的正则表达式需要

'regex'    => '/(?<lang>(de|fr|nl)?)'
Run Code Online (Sandbox Code Playgroud)

您可以使用Segment路径和适当的约束来实现相同的...

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'language' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route' => '[/:lang]',
                        'defaults' => array(
                            'lang' => 'en', //default
                        ),
                        'constraints' => array(
                            'lang' => '(en|de|fr|nl)?',
                        ),
                    ),
                ),
            ),
        ),
    ),
),
Run Code Online (Sandbox Code Playgroud)