使用ZF的gettext适配器翻译路由段 [英] Translating route segments with ZF's gettext adapter

查看:101
本文介绍了使用ZF的gettext适配器翻译路由段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Zend Framework中尝试路由转换,但是我使用的是gettext适配器,并且大多数教程都使用PHP转换适配器,因此我在使它工作方面遇到问题.

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.

在主Bootstrap.php中,我有设置路由的方法:

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);

这适用于/about路径. 我将粘贴设置Zend_Translate的代码,但它基本上会根据当前会话语言加载*.mo文件:

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); 

这通常称为prior路由.

我的问题:gettext可以用作路由的转换适配器,因为我不知道如何用poEdit捕获@about字符串?它可以?万岁!怎么样?

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?

推荐答案

好吧,我的妈妈全都搞砸了.因此,代码没有问题.

Well, my mo's were all messed up. So there's no problem with the code.

以下是编辑mo文件的方法,以便路由可以对其进行翻译(我想您的ZF i18n工作正常-翻译,语言环境等):

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.还记得吗?

$routerRoute = new Zend_Controller_Router_Route('@about',
     array(
      'module'     => 'default',
      'controller' => 'index',
      'action'     => 'about'
    )
);

2.看到'@ about'字符串吗??这就是即将翻译的路径.那么,如何翻译字符串以便poEdit可以捕获它呢?好吧,你没有;无论如何都不能使用poEdit.您手动编辑.po file:

#ro.po
msgid  "about"
msgstr "despre"

#en.po
msgid  "about"
msgstr "about"

msgid应该与您的路径字符串匹配(减去'@'字符串,ofc).

The msgid should match your path string (minus the '@' string, ofc).

3.现在,使用poEdit打开po文件..您会看到一个新字符串(很惊讶,是吧?).编译此po以获得ZF可以使用的新的闪亮的mo文件.

3. Now go open your po file with poEdit. You'll see a new string (surprised, huh?). Compile this po to get a new shiny mo file that ZF can use.

4.现在,我的site.com/aboutsite.com/despre路径都可以使用.

4. Now my site.com/about or site.com/despre paths work.

这里有更多信息.

这篇关于使用ZF的gettext适配器翻译路由段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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