我以前使用过Zend_Locale,但是看起来PHP扩展有cldr信息。
我需要得到一些信息,比如为每种语言找到可用的国家?例如,en有US、UK、GB和fa有IR和AF,还有更多关于CLDR项目的可用数据。
国家名称、按每种语言列出的时区列表以及CLDR文件中存在的更多数据。
它嵌入在php上,或者我可以下载并将它们绑定到上面的类或方法上?
哪个对象或方法在PHP intl extension上给了我这个信息?
CLDR information
发布于 2015-07-26 00:21:54
我想出了一个解决方案,起点是地区。
您可以使用getLocales方法获取所有区域设置的列表。$locales = ResourceBundle::getLocales('');见此处:http://php.net/manual/en/resourcebundle.locales.php
然后,您可以使用$countryName = Locale::getDisplayRegion($locale, 'en');获取每个地区的国家名称,也可以使用Locale::getDisplayLanguage ( $locale )获得语言名称,等等。见此处:http://php.net/manual/en/class.locale.php
例如,我设法将许多时区名称与以下代码相匹配;
<?php
/* fill the array with values from
https://gist.github.com/vxnick/380904#gistcomment-1433576
Unfortunately I couldn't manage to find a proper
way to convert countrynames to short codes
*/
$countries = [];
$locales = ResourceBundle::getLocales('');
foreach ($locales as $l => $locale) {
$countryName = Locale::getDisplayRegion($locale, 'en');
$countryCode = array_search($countryName, $countries);
if($countryCode !== false) {
$timezone_identifiers = DateTimeZone::listIdentifiers( DateTimeZone::PER_COUNTRY, $countryCode);
echo "----------------".PHP_EOL;
echo $countryName.PHP_EOL;
echo Locale::getDisplayLanguage ( $locale ).PHP_EOL;
var_dump($timezone_identifiers);
}
}我知道这不是最好的答案,但至少这可能会给你一个开局。
更新
要获得每个地区的国家名称,您可以尝试这个;
<?php
$locales = ResourceBundle::getLocales('');
foreach ($locales as $l => $locale) {
$countryName = Locale::getDisplayRegion($locale, 'en');
echo $locale."===>".$countryName.PHP_EOL;
} 更新2
收集日期名称、月份名称、每个地区的货币
$locales = ResourceBundle::getLocales('');
foreach ($locales as $l => $locale) {
echo "============= ".PHP_EOL;
echo "Locale:". $locale. PHP_EOL;
echo "Language: ".Locale::getDisplayLanguage($locale, 'en');
echo PHP_EOL;
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
echo "Currency: ".$formatter->getTextAttribute(NumberFormatter::CURRENCY_CODE);
echo PHP_EOL;
echo PHP_EOL."Days :".PHP_EOL;
$dt = new DateTime('this sunday');
for($i = 0; $i<=6; $i++) {
echo IntlDateFormatter::formatObject($dt, "eeee", $locale);
$dt->add(new DateInterval('P1D'));
echo PHP_EOL;
}
echo PHP_EOL."Months :".PHP_EOL;
$dt = new DateTime('01/01/2015');
for($i = 0; $i<12; $i++) {
echo IntlDateFormatter::formatObject($dt, "MMMM", $locale);
$dt->add(new DateInterval('P1M'));
echo PHP_EOL;
}
}就我在文档上的阅读而言,用户必须使用上面这样的方法来收集每个地区的信息。有一个可用于此目的的图书馆。https://github.com/ICanBoogie/CLDR
https://stackoverflow.com/questions/31490437
复制相似问题