使用 ZF gettext 适配器翻译路线段

发布于 2024-12-06 06:03:53 字数 1609 浏览 0 评论 0原文

我想尝试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2024-12-13 06:03:53

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

以下是编辑 mo 文件以便路线可以翻译它的方法(我假设您的 ZF i18n 正在工作 - 翻译、区域设置等):

1.还记得这个吗?

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

2.看到那个'@about'字符串了吗?这是即将翻译的路径。那么如何翻译一个字符串以便 poEdit 能够捕获它呢?好吧,你不知道;无论如何,不​​是用 poEdit 。您手动编辑 .po 文件

#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 路径可以工作。

更多信息请参见此处。

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?

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

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:

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

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

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 shiny mo file that ZF can use.

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

More info here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文