Zend 框架、gettext、poedit 和编写关键字符串的人类语言
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Fluxine,
当使用
Zend_Translate
的 gettext 适配器时,您将需要: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:gda-fr.po
containing at least one translation in order to generate thegda-fr.mo
(which will be almost empty ...)gda-en.po
to generate thegda-en.mo
gda-de.po
to generate thegda-de.mo
gda-XX.po
to generate thegda-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 updatedgda-fr.po
file.The Adapter will return the original, french, string because there is no translation for it in the
gda-fr.mo