覆盖gettext .mo文件 [英] override gettext .mo files

查看:124
本文介绍了覆盖gettext .mo文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们应用程序中的翻译,我们将 Zend Translate 与gettext一起使用适配器.每个模块中都有一个文件夹translations,其中包含所有语言的.mo文件;

For translations in our application, we're using Zend Translate with the gettext adapter. In each module is a folder translations, containing .mo files for all the languages;

  • da.mo
  • nl.mo
  • en.mo

通过addTranslation()方法进行扫描和添加的内容:

Which are scanned and added through the addTranslation() method:

if ( is_dir( $translations_dir ) ) {
    foreach ( new DirectoryIterator( $translations_dir ) as $file ) {
        if ( substr( $file, -3 ) == '.mo' ) {
            $ZendTranslate->addTranslation( $file->getPathname(), $file->getBasename('.mo') );
        }
    }
}

使用_()方法翻译应用程序中的字符串,并将当前语言作为参数传递(存储在框架的Language类中):

The strings in my application are translated using the _() method, passing the current language as a parameter (it's stored in my framework's Language class):

$ZendTranslate->_( $string, $this->language );

现在,我需要为特定客户定制荷兰语(nl)语言字符串.我不想修改nl.mo文件,因为这会影响其他客户.因此,我创建了一个名为nl_kpn.mo的文件(kpn是客户名称),并将$this->language切换为'nl_kpn'.我希望Zend Transate将nl.mo作为基本文件,覆盖在nl_kpn.mo中找到的自定义字符串.但不幸的是,我经历了手动状态:

Now I need to customize the Dutch (nl) language strings for a specific customer. I don't want to modify the nl.mo file, as that would affect other customers. So I created a file called nl_kpn.mo (kpn is the customer name), and switched the $this->language to 'nl_kpn'. I was hoping Zend Transate would take nl.mo as the base file, overriding the customized strings found in nl_kpn.mo. But unfortunately I experienced, as the manual states:

fr_CH将降级为fr

fr_CH will be downgraded to fr

因此,即使$this->language设置为'nl_kpn',所有字符串仍来自nl.mo文件.那么,如何创建一种更具体的语言版本,覆盖通用语言中的字符串呢?一定有可能吧?因为还有en_UK和en_US,它们是同一语言的不同方言".

So all the strings were still from the nl.mo file, even though $this->language was set to 'nl_kpn'. So how can I create a more specific version of a language, overriding strings from the general one? It must be possible, right? Because there's also en_UK and en_US, which are different 'dialects' of the same language.

推荐答案

正如Rijk所说,Zend_Translate将尝试调用Zend_Locale,而根本不存在.不幸的是,该清单已设置为私有,因此我们可能不能使用继承来简单地导出我们自己的实现.

As Rijk said, Zend_Translate will try to invoke a Zend_Locale, which simply does not exist. Unfortunatly, the listing has been set to private, so we may not use inheritance to simply derive our own implementation.

但是,您有两个选择.根据您的设置,您可以通过在配置中提供其他目录来为此特定客户设置自己的nl.mo文件.这样,您可以链接所有其他语言文件,并为kpn-client提供一个单独的文件.

However, you have two options. Depending on your setup, you may just set your own nl.mo-file for this specific customer by providing a different directory in the configuration. That way, you may link all other language-files and have a separate one for the kpn-client.

第二个选择是摆脱Zend_Translate并使用默认的php gettext .由于您已经在使用gettext文件,并且似乎在使用translate()函数,因此除了将$ ZendTranslate重构为gettext类之外,您将没有更多的工作要做.如果您确实在其他类中(例如,在Zend_Navigation中)自动使用Zend_Translate,则可以保持原样.

The second option you have is to get rid of Zend_Translate and use the default php gettext. As you are already using gettext-files and seem to use the translate()-function, you would not have any more work than to refactor $ZendTranslate to your gettext-class. If you do use Zend_Translate automatically in other classes (e.g. in Zend_Navigation), you can just leave those intact.

虽然第二个选项确实起作用,但有点混乱-但是,第三个干净的替代方法是继承Zend_Locale并重写所有访问私有语言列表的方法.这样,您可以添加自己的.或者,您甚至可以重写Zend_Locale的部分内容,以支持动态添加语言并发送补丁文件-这样,我们也可以从您的工作中受益. ;)

While the 2nd option does work, it is a bit messy - however, the third and clean alternative is to inherit Zend_Locale and rewriting all methods accessing the private language list. That way, you can add your own. Or you even go the lenghts of rewriting the parts of Zend_Locale to support dynamical adding of languages and send a patch file in - that way, we may benefit from your work, too. ;)

这篇关于覆盖gettext .mo文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆