duanmei1930 2014-06-26 13:50
浏览 11

Zend Framework 2中的MultiLingual站点 - 从URL参数设置setLocale()的值

Below is what I have done so far -

In Application Module - module.config.php -

'service_manager' => array(
    [....],
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
),
'translator' => array(
    'locale' => 'en_US',
    'translation_file_patterns' => array(
        array(
            'type' => 'gettext',
            'base_dir' => __DIR__ . '/../language',
            'pattern' => '%s.mo',
        ),
    ),
),

In Album Module - module.config.php -

'router' => array(
    'routes' => array(
        'album' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '[/:lang]/album[/:action][/:id]',
                'constraints' => array(
                    'lang' => '[a-zA-Z]*',
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),

In Album Module - Module.php -

public function onBootstrap(MvcEvent $e) {
    $sm = $e->getApplication()->getServiceManager();

    $router = $sm->get('router');
    $request = $sm->get('request');
    $matchedRoute = $router->match($request);
    $params = $matchedRoute->getParams();

    if(isset($params['lang']) && $params['lang'] !== '') {
        $translator = $e->getApplication()->getServiceManager()->get('translator');
        //or
        //$translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');

        if($params['lang'] == 'en')
            $translator->setLocale('en_US');
        elseif($params['lang'] == 'fr')
            $translator->setLocale('fr_FR');
        else
            $translator->setLocale('en_US');
    }
}

In view -

echo $this->translate('Home');

Note: URL - http://zf2-tutorial.localhost/fr/album, works just fine. The translation is successful.


Query -

$translator->setLocale('en'); seems to be working with ZF1 but not with ZF2.

I need to know whether there is any way to directly set the URL param value like $translator->setLocale($params['lang']); rather then long if-else or switch statements.

Some sites might be in 20 or more languages.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douxiong2001 2014-06-26 18:11
    关注

    Simple answer: use SlmLocale. I wrote the module to implement locale detection DRY and for different use cases.

    Don't try to fit the localization into your routes. This allows you to have translatable routes as you detect the locale before routing. Also, it helps you enormously to change locale with different URIs.

    A mistake you made in your process is that you grab the route match from the event during bootstrap. However, bootstrap occurs before routing, so you can't get the route match parameters at that stage.

    TL;DR: checkout SlmLocale, it should help you with all your problems.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM