【问题标题】:Translating route segments with ZF's gettext adapter使用 ZF 的 gettext 适配器翻译路线段
【发布时间】:2011-11-23 10:05:19
【问题描述】:

我想尝试 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 字符串?它可以?万岁!怎么样?

【问题讨论】:

    标签: zend-framework gettext zend-route zend-translate


    【解决方案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 以获得 ZF 可以使用的新的闪亮的 mo 文件。

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

    More info here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多