Zend Framework 2 - translating routes(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.
推荐答案
我能够让它工作!
首先添加一个'router_class' =>'ZendMvcRouterHttpTranslatorAwareTreeRouteStack',
你的 module.config.php
像这样:
First, add a 'router_class' => 'ZendMvcRouterHttpTranslatorAwareTreeRouteStack',
your module.config.php
like this:
return array (
'router' => array (
'router_class' => 'ZendMvcRouterHttpTranslatorAwareTreeRouteStack',
'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' => 'ZendMvcRouterHttpSegment',
'may_terminate' => true,
'options' => array (
'route' => '/{login}',
'defaults' => Array(
'controller' => '...',
)
),
),
)
);
创建翻译(在这个例子中是一个位于 module/language/routes/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 - 翻译路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Zend Framework 2 - 翻译路由
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01