我之前使用过 Zend_Locale,但似乎 PHP 国际扩展有 cldr 信息。
我需要获取一些信息,例如获取每种语言的可用国家/地区?
例如 en
有 US
、UK
、GB
和 fa
有 IR
和 AF
以及有关 CLDR 项目的更多数据。
国家名称、每种语言的时区列表以及 CLDR xml 文件中的更多数据。
它嵌入在 php intl 上,或者我可以下载并将它们绑定(bind)到类或方法上?
哪个对象或方法给我关于 PHP intl extension 的信息?
最佳答案
我想出了一个解决方案,起点是语言环境。
您可以使用 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
关于php - 从 php 中的 Intl 扩展中获取 CLDR 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31490437/