我想出了一个解决方案,起点是语言环境。
您可以使用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