2

我想在 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 可以用作路由的翻译适配器吗,因为我不知道如何@about使用 poEdit 捕获字符串?它可以?万岁!如何?

4

1 回答 1

1

好吧,我的妈妈都搞砸了。所以代码没有问题。

以下是您编辑 mo 文件以便路线可以翻译它的方法(我假设您的 ZF i18n 可以正常工作 - 翻译、语言环境等):

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

3. 现在用 poEdit 打开你的 po 文件。您会看到一个新字符串(很惊讶,嗯?)。编译它po以获得moZF 可以使用的新闪亮文件。

4. 现在我的site.com/aboutorsite.com/despre路径可以工作了。

更多信息在这里。

于 2011-09-24T06:39:17.747 回答