Zend 框架、gettext、poedit 和编写关键字符串的人类语言

发布于 2024-09-25 09:21:47 字数 1311 浏览 10 评论 0原文

我正在使用 ZendFramework 编写一个网站,并决定使用 gettext 系统来国际化其内容。 在我看来,我用法语编写字符串,并提供此时只有一个文件“en.mo”来将句子翻译成英语。 我想避免维护一个文件“fr.mo”,它只会将一个句子翻译成完全相同的句子。

有没有办法告诉 Zend 或 gettext,当语言环境为“fr”或“fr_FR”时,无需搜索翻译文件,并且必须按原样返回密钥字符串? 或者也许有理由创建“身份翻译”文件 fr.mo ?

此时我的代码的关键引用:

我的引导程序摘录:

 protected function _initTranslations() {
        $config = $this->getApplication()->getOptions();

        Zend_Locale::setDefault('en'); // fallback if detection fails completely

        $locale = new Zend_Locale();
        if ( !in_array($locale->getLanguage(), explode(',',$config['available_translations']))) {
            $locale->setLocale('en'); // requested language is not available => use english
        }

        $translate = new Zend_Translate(array(  'adapter' => 'gettext',
                                                'content' => APPLICATION_PATH.'/../languages/gda-'.$locale->getLanguage().'.mo',
                                                'locale' => $locale));


        $translate->setLocale($locale);

        $this->bootstrap('view');
        $this->getResource('view')->getHelper('translate')->setTranslator($translate);
    }

/语言/的内容:

gda-en.mo
gda-fr.mo  <== this one I would like to avoid maintaining

谢谢

I'm writing a website using ZendFramework and decided to use the gettext system to internationalize its content.
In my views I write string in french and provide a this moment only one file "en.mo" to translate sentences to english.
I would like to avoid to maintain a file "fr.mo" which would only translate a sentence to the exact same sentence.

Is there a way to tell Zend or gettext that when the locale is "fr" or "fr_FR" then there is no need to search for a translation file and the key string must be just returned as is ?
Or maybe there is a reason to create the "identity translation" file fr.mo ?

Key-quotes of my code at this point :

Extract of my bootstrap:

 protected function _initTranslations() {
        $config = $this->getApplication()->getOptions();

        Zend_Locale::setDefault('en'); // fallback if detection fails completely

        $locale = new Zend_Locale();
        if ( !in_array($locale->getLanguage(), explode(',',$config['available_translations']))) {
            $locale->setLocale('en'); // requested language is not available => use english
        }

        $translate = new Zend_Translate(array(  'adapter' => 'gettext',
                                                'content' => APPLICATION_PATH.'/../languages/gda-'.$locale->getLanguage().'.mo',
                                                'locale' => $locale));


        $translate->setLocale($locale);

        $this->bootstrap('view');
        $this->getResource('view')->getHelper('translate')->setTranslator($translate);
    }

content of /languages/ :

gda-en.mo
gda-fr.mo  <== this one I would like to avoid maintaining

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

猫弦 2024-10-02 09:21:47

Fluxine,

当使用 Zend_Translate 的 gettext 适配器时,您将需要:

  • 包含所有 msg-id 的 gda.pot(在您的情况下为法语)
  • gda-fr.po 包含至少一个翻译,以便生成 gda-fr.mo (几乎是空的...)
  • 一些其他翻译文件
    • gda-en.po 生成 gda-en.mo
    • gda-de.po 生成 gda-de.mo
    • ...
    • gda-XX.po 生成gda-XX.mo

这里重要的事实是:

你的“身份翻译”正如你所称的,需要存在,否则 Zend 会抱怨“fr”(在你的情况下)不可用。

在 poEdit 中,只需将一个小值转换为相同的字符串,生成相应的 *。 mo 文件,保存它,然后就不用管它了。

您无需再更新 gda-fr.po 文件,也无需重新生成更新后的 gda-fr.po 文件。

适配器将返回原始的法语字符串,因为 gda-fr.mo 中没有该字符串的翻译

Fluxine,

When using the gettext Adapter of Zend_Translate, you will need:

  • the gda.pot containing all your msg-id (in french in your case)
  • the gda-fr.po containing at least one translation in order to generate the gda-fr.mo (which will be almost empty ...)
  • some other translation files
    • gda-en.po to generate the gda-en.mo
    • gda-de.po to generate the gda-de.mo
    • ....
    • gda-XX.po to generate the gda-XX.mo

The important fact here is:

Your "identity translation", as you call it, needs to exist, otherwise Zend will complain about the fact that the "fr" (in your case) is not available

In poEdit, just translate one small to the same string, generate the corresponding *.mo file, save it, and forget about it.

You won't need to update your gda-fr.po file anymore, nor regenerating an updated gda-fr.po file.

The Adapter will return the original, french, string because there is no translation for it in the gda-fr.mo

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