Zend Framework 2-翻译路线 [英] Zend Framework 2 - translating routes

查看:103
本文介绍了Zend Framework 2-翻译路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以对zf2中的路由/ uri使用转换工具。我想例如将路线 en.domain.tld / article / show / 1 转换为例如 de.domain.tld / artikel / anzeigen / 1 。我认为正则表达式不是解决问题的方法,因为它可能会导致 en.domain.tld / artikel / show / 1 之类的事情。我也想避免为每种语言创建路由,因为随着系统的扩展,它将变得很混乱。

I am wondering if it is possible to use translational tools for routes/uris in zf2. I want for example the route en.domain.tld/article/show/1 to translate for example to de.domain.tld/artikel/anzeigen/1. I don't think regex is the way to go here, because it could result in something like en.domain.tld/artikel/show/1. Also I want to avoid creating routes for every language, because it is going to get quite messy as the system scales.

推荐答案

I

首先,添加'router_class'=> 'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack',您的 module.config.php 像这样:

First, add a 'router_class' => 'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack', your module.config.php like this:

return array (
    'router' => array (
        'router_class' => 'Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack',
        'routes' => array(),
    )
);

第二,您必须提供翻译器(最好在module.php中)以及翻译文件:

Second, you must provide a translator (preferably in your module.php) as well as a translation file:

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        // Load translator
       $translator = $e->getApplication()->getServiceManager()->get('translator');
       $translator->setLocale('de_DE');        

           // setup the translation file. you can use .mo files or whatever, check the translator api
           $translator->addTranslationFile('PhpArray', __DIR__.'/language/routes/de_DE.php', 'default', 'de_DE');

       $app      = $e->getTarget();

       // Route translator
       $app->getEventManager()->attach('route', array($this, 'onPreRoute'), 100);
    }

    public function onPreRoute($e){
        $app      = $e->getTarget();
        $serviceManager       = $app->getServiceManager();
        $serviceManager->get('router')->setTranslator($serviceManager->get('translator'));
    }
}

现在,您应该可以在自己的翻译中使用翻译了路由定义如下:

now, you should be able to use translations in your route definitions like the following:

return array (
    'router' => array (
        'routes' => array(
            'login' => array (
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'may_terminate' => true,
            'options' => array (
                'route' => '/{login}',
                'defaults' => Array(
                    'controller' => '...',
                ) 
            ),
        ),
    )
);

创建翻译(在此示例中,位于模块/语言/路由/de_DE.php中的phpArray):

create the translation (in this example a phpArray located in module/language/routes/de_DE.php):

<?php
return array(
    'login' => 'anmelden',
);

如果我什么都没忘记,那你应该很好了。在我的案件中,因此,如果上面的说明中没有,不要犹豫地发表评论,我会整理一下。

If I didn't forget anything, you should be good to go. I got it working in my case, so if it doesn't with the instructions above, don't hesitate to comment and I'll sort things out.

这篇关于Zend Framework 2-翻译路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆