使用 ZF gettext 适配器翻译路线段
我想尝试 Zend Framework 中的路由翻译,但我使用的是 gettext 适配器,并且大多数教程都有 PHP 翻译适配器,所以我在使其工作时遇到问题。
在主 Bootstrap.php 中,我有设置路由的方法:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$translator = Zend_Registry::get('Zend_Translate');
Zend_Controller_Router_Route::setDefaultTranslator($translator);
$routerRoute = new Zend_Controller_Router_Route('@about',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'about'
)
);
$router->addRoute('about', $routerRoute);
这适用于 /about
路径。 我将粘贴设置 Zend_Translate 的代码,但它基本上会根据当前会话语言加载一个 *.mo
文件:
$langParam = $this->getSessionLang();
$localeString = $languages[$langParam];
$locale = new Zend_Locale($localeString);
$moFilePath = $langFolder . $langParam . '.mo';
if (!file_exists($moFilePath)) {
throw new Zend_Exception('File does not exist: ' . $moFilePath);
}
$translate = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => $moFilePath,
'locale' => $locale,
'ignore' => array('.'), // ignore SVN folders
'disableNotices' => ('production' === APPLICATION_ENV) // disable notices in Production
)
);
Zend_Registry::set('Zend_Translate', $translate);
Zend_Registry::set('Zend_Locale' , $locale);
这,ofc,它被称为 prior
到路由。
我的问题: gettext 可以用作路线的翻译适配器吗,因为我不知道如何使用 poEdit 捕获 @about
字符串?可以吗?万岁!如何?
I want to try out the route translations in Zend Framework, but I'm using the gettext adapter and the most tutorials have PHP translate adapter, so I'm having problems to make it work.
In the main Bootstrap.php I have the method in which I set the routes:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$translator = Zend_Registry::get('Zend_Translate');
Zend_Controller_Router_Route::setDefaultTranslator($translator);
$routerRoute = new Zend_Controller_Router_Route('@about',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'about'
)
);
$router->addRoute('about', $routerRoute);
This works for /about
path.
I'll paste the code in which I set Zend_Translate, but it basically loads a *.mo
file depending on current session language:
$langParam = $this->getSessionLang();
$localeString = $languages[$langParam];
$locale = new Zend_Locale($localeString);
$moFilePath = $langFolder . $langParam . '.mo';
if (!file_exists($moFilePath)) {
throw new Zend_Exception('File does not exist: ' . $moFilePath);
}
$translate = new Zend_Translate(
array(
'adapter' => 'gettext',
'content' => $moFilePath,
'locale' => $locale,
'ignore' => array('.'), // ignore SVN folders
'disableNotices' => ('production' === APPLICATION_ENV) // disable notices in Production
)
);
Zend_Registry::set('Zend_Translate', $translate);
Zend_Registry::set('Zend_Locale' , $locale);
This, ofc, it's called prior
to routing.
My question: can gettext be used as a translation adapter for a route, because I can't figure out how can I catch the @about
string with, let's say, poEdit? It can? Hooray! How?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我的妈妈都搞砸了。所以代码没有问题。
以下是编辑 mo 文件以便路线可以翻译它的方法(我假设您的 ZF i18n 正在工作 - 翻译、区域设置等):
1.还记得这个吗?
2.看到那个'@about'字符串了吗?这是即将翻译的路径。那么如何翻译一个字符串以便 poEdit 能够捕获它呢?好吧,你不知道;无论如何,不是用 poEdit 。您手动编辑
.po 文件
:msgid 应与您的路径字符串匹配(减去“@”字符串,ofc)。
3.现在用 poEdit 打开您的 po 文件。您将看到一个新字符串(很惊讶,是吗?)。编译此
po
以获得 ZF 可以使用的新的闪亮mo
文件。4.现在我的
site.com/about
或site.com/despre
路径可以工作。更多信息请参见此处。
Well, my mo's were all messed up. So there's no problem with the code.
Here's how you edit your mo files so that the route can translate it (I'm presume you have your ZF i18n working - Translate, Locale and the like):
1. Remember this?
2. See that '@about' string? That's the soon-to-be-translated path. So how do you translate a string so that poEdit will catch it? Well, you don't; not with poEdit anyway. You manually edit the
.po file
:The msgid should match your path string (minus the '@' string, ofc).
3. Now go open your po file with poEdit. You'll see a new string (surprised, huh?). Compile this
po
to get a new shinymo
file that ZF can use.4. Now my
site.com/about
orsite.com/despre
paths work.More info here.