问题
I have the following piece of code and am trying to do something simple with the Zend framework and Zend_Locale()
$supported_langs = array(
'en' => 'English',
'zh_CN' => '中文(简体)',
'zh_HK' => '中國(傳統)',
'es' => 'Español',
'ja' => '日本',
'pt' => 'Português',
'de' => 'Deutsch',
'ar' => 'العربية',
'fr' => 'Française',
'ru' => 'Pусский',
'ko' => '한국의',
'hi' => 'हिन्दी',
'vi' => 'Việt'
);
echo '<pre>';
foreach ($supported_langs as $lang => $desc) {
print Zend_Locale::getTranslation($lang, 'language', 'en') . "\n";
}
echo '</pre>';
The output from the above is:
English
Spanish
Japanese
Portuguese
German
Arabic
French
Russian
Korean
Hindi
Vietnamese
zh_CN
, zh_HK
don't provide output. If I change one of the zh values to zh
, it prints out Chinese, which is Ok, I suppose, but doesn't quite work the way I hoped?
zh_CN and zh_HK are two different languages ... I would like to be able to print the translation for both ...without over simplifying it to just Chinese...
Edit
Turns out, if I use zh_Hans
and zh_Hant
then it prints out as correct. So I suppose:
Question: why doesn't the Zend_Locale honor the abbreviated formats like zh_HK or zh_CN?
回答1:
Computer are inherently stupid, you have to tell them everything. Zend_Locale gets its information from quite a bunch of XML files and what is defined there. Your Chinese language codes are not known, dunno what is "officially" the correct abbreviation for this.
With the following you can pull a list with all supported languages in the framework.
$locale = new Zend_Locale();
$langList = $locale->getTranslationList('language');
array [..
['zh'] => 'Chinese'
['zh_Hans'] => 'Simplified Chinese'
['zh_Hant'] => 'Traditional Chinese'
.. ]
You can pass a second $locale
argument and you will get the values for the language in that language. The default is what the browser sends.
UPDATE zh_CN and zh_HK are region reference for China versus Hongkong and not languages! There is also zh_TW for Taiwan
来源:https://stackoverflow.com/questions/6722948/why-doesnt-the-zend-locale-honor-abbreviated-formats-like-zh-hk-or-zh-cn