对于我们的应用程序中的翻译,我们将Zend Translate与gettext适配器一起使用。在每个模块中都有一个文件夹translations
,其中包含所有语言的.mo文件;
通过addTranslation()
方法扫描和添加的内容:
if ( is_dir( $translations_dir ) ) {
foreach ( new DirectoryIterator( $translations_dir ) as $file ) {
if ( substr( $file, -3 ) == '.mo' ) {
$ZendTranslate->addTranslation( $file->getPathname(), $file->getBasename('.mo') );
}
}
}
我的应用程序中的字符串使用_()
方法进行翻译,将当前语言作为参数传递(它存储在我的框架的语言类中):
$ZendTranslate->_( $string, $this->language );
现在我需要为特定客户自定义Dutch(nl)语言字符串。我不想修改nl.mo
文件,因为这会影响其他客户。所以我创建了一个名为nl_kpn.mo
的文件(kpn是客户名称),并将$this->language
切换为'nl_kpn'。我希望Zend Transate将nl.mo
作为基本文件,覆盖nl_kpn.mo
中的自定义字符串。但不幸的是,我经历了manual states:
fr_CH将降级为fr
所有字符串仍然来自nl.mo
文件,即使$this->language
设置为'nl_kpn'。那么如何创建一个更具体的语言版本,覆盖一般语言的字符串呢?一定是可能的,对吗?因为还有en_UK和en_US,它们是同一种语言的不同“方言”。
答案 0 :(得分:1)
我认为答案是这是不可能的。语言环境必须是现有的语言/方言,并使用Zend_Locale
中的列表进行验证:
'root' => true, 'aa_DJ' => true, 'aa_ER' => true, 'aa_ET' => true, 'aa' => true,
'af_NA' => true, 'af_ZA' => true, 'af' => true, 'ak_GH' => true, 'ak' => true,
'am_ET' => true, 'am' => true, 'ar_AE' => true, 'ar_BH' => true, 'ar_DZ' => true,
'ar_EG' => true, 'ar_IQ' => true, 'ar_JO' => true, 'ar_KW' => true, 'ar_LB' => true,
'ar_LY' => true, 'ar_MA' => true, 'ar_OM' => true, 'ar_QA' => true, 'ar_SA' => true,
'ar_SD' => true, 'ar_SY' => true, 'ar_TN' => true, 'ar_YE' => true, 'ar' => true,
'as_IN' => true, 'as' => true, 'az_AZ' => true, 'az' => true, 'be_BY' => true,
'be' => true, 'bg_BG' => true, 'bg' => true, 'bn_BD' => true, 'bn_IN' => true,
'bn' => true, 'bo_CN' => true, 'bo_IN' => true, 'bo' => true, 'bs_BA' => true,
'bs' => true, 'byn_ER'=> true, 'byn' => true, 'ca_ES' => true, 'ca' => true,
'cch_NG'=> true, 'cch' => true, 'cop' => true, 'cs_CZ' => true, 'cs' => true,
'cy_GB' => true, 'cy' => true, 'da_DK' => true, 'da' => true, 'de_AT' => true,
'de_BE' => true, 'de_CH' => true, 'de_DE' => true, 'de_LI' => true, 'de_LU' => true,
'de' => true, 'dv_MV' => true, 'dv' => true, 'dz_BT' => true, 'dz' => true,
'ee_GH' => true, 'ee_TG' => true, 'ee' => true, 'el_CY' => true, 'el_GR' => true,
'el' => true, 'en_AS' => true, 'en_AU' => true, 'en_BE' => true, 'en_BW' => true,
'en_BZ' => true, 'en_CA' => true, 'en_GB' => true, 'en_GU' => true, 'en_HK' => true,
'en_IE' => true, 'en_IN' => true, 'en_JM' => true, 'en_MH' => true, 'en_MP' => true,
'en_MT' => true, 'en_NA' => true, 'en_NZ' => true, 'en_PH' => true, 'en_PK' => true,
'en_SG' => true, 'en_TT' => true, 'en_UM' => true, 'en_US' => true, 'en_VI' => true,
'en_ZA' => true, 'en_ZW' => true, 'en' => true, 'eo' => true, 'es_AR' => true,
'es_BO' => true, 'es_CL' => true, 'es_CO' => true, 'es_CR' => true, 'es_DO' => true,
'es_EC' => true, 'es_ES' => true, 'es_GT' => true, 'es_HN' => true, 'es_MX' => true,
'es_NI' => true, 'es_PA' => true, 'es_PE' => true, 'es_PR' => true, 'es_PY' => true,
'es_SV' => true, 'es_US' => true, 'es_UY' => true, 'es_VE' => true, 'es' => true,
'et_EE' => true, 'et' => true, 'eu_ES' => true, 'eu' => true, 'fa_AF' => true,
'fa_IR' => true, 'fa' => true, 'fi_FI' => true, 'fi' => true, 'fil_PH'=> true,
'fil' => true, 'fo_FO' => true, 'fo' => true, 'fr_BE' => true, 'fr_CA' => true,
'fr_CH' => true, 'fr_FR' => true, 'fr_LU' => true, 'fr_MC' => true, 'fr_SN' => true,
'fr' => true, 'fur_IT'=> true, 'fur' => true, 'ga_IE' => true, 'ga' => true,
'gaa_GH'=> true, 'gaa' => true, 'gez_ER'=> true, 'gez_ET'=> true, 'gez' => true,
'gl_ES' => true, 'gl' => true, 'gsw_CH'=> true, 'gsw' => true, 'gu_IN' => true,
'gu' => true, 'gv_GB' => true, 'gv' => true, 'ha_GH' => true, 'ha_NE' => true,
'ha_NG' => true, 'ha_SD' => true, 'ha' => true, 'haw_US'=> true, 'haw' => true,
'he_IL' => true, 'he' => true, 'hi_IN' => true, 'hi' => true, 'hr_HR' => true,
'hr' => true, 'hu_HU' => true, 'hu' => true, 'hy_AM' => true, 'hy' => true,
'ia' => true, 'id_ID' => true, 'id' => true, 'ig_NG' => true, 'ig' => true,
'ii_CN' => true, 'ii' => true, 'in' => true, 'is_IS' => true, 'is' => true,
'it_CH' => true, 'it_IT' => true, 'it' => true, 'iu' => true, 'iw' => true,
'ja_JP' => true, 'ja' => true, 'ka_GE' => true, 'ka' => true, 'kaj_NG'=> true,
'kaj' => true, 'kam_KE'=> true, 'kam' => true, 'kcg_NG'=> true, 'kcg' => true,
'kfo_CI'=> true, 'kfo' => true, 'kk_KZ' => true, 'kk' => true, 'kl_GL' => true,
'kl' => true, 'km_KH' => true, 'km' => true, 'kn_IN' => true, 'kn' => true,
'ko_KR' => true, 'ko' => true, 'kok_IN'=> true, 'kok' => true, 'kpe_GN'=> true,
'kpe_LR'=> true, 'kpe' => true, 'ku_IQ' => true, 'ku_IR' => true, 'ku_SY' => true,
'ku_TR' => true, 'ku' => true, 'kw_GB' => true, 'kw' => true, 'ky_KG' => true,
'ky' => true, 'ln_CD' => true, 'ln_CG' => true, 'ln' => true, 'lo_LA' => true,
'lo' => true, 'lt_LT' => true, 'lt' => true, 'lv_LV' => true, 'lv' => true,
'mk_MK' => true, 'mk' => true, 'ml_IN' => true, 'ml' => true, 'mn_CN' => true,
'mn_MN' => true, 'mn' => true, 'mo' => true, 'mr_IN' => true, 'mr' => true,
'ms_BN' => true, 'ms_MY' => true, 'ms' => true, 'mt_MT' => true, 'mt' => true,
'my_MM' => true, 'my' => true, 'nb_NO' => true, 'nb' => true, 'nds_DE'=> true,
'nds' => true, 'ne_IN' => true, 'ne_NP' => true, 'ne' => true, 'nl_BE' => true,
'nl_NL' => true, 'nl' => true, 'nn_NO' => true, 'nn' => true, 'no' => true,
'nr_ZA' => true, 'nr' => true, 'nso_ZA'=> true, 'nso' => true, 'ny_MW' => true,
'ny' => true, 'oc_FR' => true, 'oc' => true, 'om_ET' => true, 'om_KE' => true,
'om' => true, 'or_IN' => true, 'or' => true, 'pa_IN' => true, 'pa_PK' => true,
'pa' => true, 'pl_PL' => true, 'pl' => true, 'ps_AF' => true, 'ps' => true,
'pt_BR' => true, 'pt_PT' => true, 'pt' => true, 'ro_MD' => true, 'ro_RO' => true,
'ro' => true, 'ru_RU' => true, 'ru_UA' => true, 'ru' => true, 'rw_RW' => true,
'rw' => true, 'sa_IN' => true, 'sa' => true, 'se_FI' => true, 'se_NO' => true,
'se' => true, 'sh_BA' => true, 'sh_CS' => true, 'sh_YU' => true, 'sh' => true,
'si_LK' => true, 'si' => true, 'sid_ET'=> true, 'sid' => true, 'sk_SK' => true,
'sk' => true, 'sl_SI' => true, 'sl' => true, 'so_DJ' => true, 'so_ET' => true,
'so_KE' => true, 'so_SO' => true, 'so' => true, 'sq_AL' => true, 'sq' => true,
'sr_BA' => true, 'sr_CS' => true, 'sr_ME' => true, 'sr_RS' => true, 'sr_YU' => true,
'sr' => true, 'ss_SZ' => true, 'ss_ZA' => true, 'ss' => true, 'st_LS' => true,
'st_ZA' => true, 'st' => true, 'sv_FI' => true, 'sv_SE' => true, 'sv' => true,
'sw_KE' => true, 'sw_TZ' => true, 'sw' => true, 'syr_SY'=> true, 'syr' => true,
'ta_IN' => true, 'ta' => true, 'te_IN' => true, 'te' => true, 'tg_TJ' => true,
'tg' => true, 'th_TH' => true, 'th' => true, 'ti_ER' => true, 'ti_ET' => true,
'ti' => true, 'tig_ER'=> true, 'tig' => true, 'tl' => true, 'tn_ZA' => true,
'tn' => true, 'to_TO' => true, 'to' => true, 'tr_TR' => true, 'tr' => true,
'trv_TW'=> true, 'trv' => true, 'ts_ZA' => true, 'ts' => true, 'tt_RU' => true,
'tt' => true, 'ug_CN' => true, 'ug' => true, 'uk_UA' => true, 'uk' => true,
'ur_IN' => true, 'ur_PK' => true, 'ur' => true, 'uz_AF' => true, 'uz_UZ' => true,
'uz' => true, 've_ZA' => true, 've' => true, 'vi_VN' => true, 'vi' => true,
'wal_ET'=> true, 'wal' => true, 'wo_SN' => true, 'wo' => true, 'xh_ZA' => true,
'xh' => true, 'yo_NG' => true, 'yo' => true, 'zh_CN' => true, 'zh_HK' => true,
'zh_MO' => true, 'zh_SG' => true, 'zh_TW' => true, 'zh' => true, 'zu_ZA' => true,
'zu' => true
我想我只需设计一种不同的方式来覆盖文件。也许只是从PHP加载一个不同的.po
文件。
答案 1 :(得分:0)
您可以尝试从Zend_Translate_Adapter覆盖_()方法并检查文件名:
public function _($messageId, $locale = null)
{
if ($locale !== null && strpos($locale, "_") !== false && $this->translate($messageId, $locale) === $messageId)
{
$languages = explode("_", $locale);
$locale = $languages[0];
}
return $this->translate($messageId, $locale);
}
答案 2 :(得分:0)
我认为最好的方法是使用isTranslated方法。你可以做
if ($translate->isTranslated('message1')) {
print "'message1' can be translated";
}
if (!($translate->isTranslated('message1', true, 'de'))) {
print "'message1' can not be translated to 'de'"
. " as it's available only in 'de_AT'";
}
if ($translate->isTranslated('message1', false, 'de')) {
print "'message1' can be translated in 'de_AT' as it falls back to 'de'";
}
isTranslated ($ messageId,$ original = false,$ locale = null)
将您要检查的文字作为第一个参数,以及作为可选的第三个参数,您要进行检查的语言环境。可选的第二个参数声明转换是固定为声明的语言还是可以使用较低的转换集。如果你有一个可以为'en'而不是'en_US'返回的文本,你通常会返回翻译,但是通过将$ original设置为true,isTranslated()将返回false。
这对你有用吗?
答案 3 :(得分:0)
正如Rijk所说,Zend_Translate将尝试调用一个根本不存在的Zend_Locale。不幸的是,列表已设置为私有,因此我们可能不会使用继承来简单地派生自己的实现。
但是,您有两种选择。根据您的设置,您可以通过在配置中提供不同的目录,为此特定客户设置自己的nl.mo
- 文件。这样,您可以链接所有其他语言文件,并为kpn-client分别设置一个。
你拥有的第二个选择是摆脱Zend_Translate并使用默认的php gettext。由于您已经在使用gettext-files并且似乎使用了translate()
- 函数,因此除了将$ ZendTranslate重构为gettext-class之外,您将没有任何其他工作。如果你在其他类中自动使用Zend_Translate(例如在Zend_Navigation中),你可以保留原样。
虽然第二个选项确实有效,但它有点乱 - 但是,第三个也是干净的选择是继承Zend_Locale并重写访问私有语言列表的所有方法。这样,您可以添加自己的。或者你甚至可以改写Zend_Locale的部分内容以支持动态添加语言并发送补丁文件 - 这样,我们也可以从你的工作中受益。 ;)