Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CLDR information from Intl extension in php

Tags:

php

intl

cldr

I used Zend_Locale before but it's seems PHP intl extension have cldr information.

I need to get some info like get available countries for each language? for example en has US, UK, GB and fa has IR and AF and more data available on CLDR project.

Name of countries, list of timezones by each languages and more data exist on CLDR xml files.

It's embedded on php intl or i can download and bind them to class or method on it?

Which object or method give me this information on PHP intl extension?

CLDR information

like image 844
sweb Avatar asked Jul 18 '15 10:07

sweb


1 Answers

I come up with a solution, the starting point is the locales.

You can get the list of all the locales with getLocales method. $locales = ResourceBundle::getLocales(''); See here: http://php.net/manual/en/resourcebundle.locales.php

Then you can get the country name of each locale with $countryName = Locale::getDisplayRegion($locale, 'en'); or you can get the language name with Locale::getDisplayLanguage ( $locale ) and so on. See here: http://php.net/manual/en/class.locale.php

For example I managed to match many of the timezone names with the following code;

<?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);

    }
}

I know this is not the best answer but at least this may give you a kick start.

Update

To get country name per locale you can try this one;

<?php
$locales = ResourceBundle::getLocales('');
foreach ($locales as $l => $locale) {
    $countryName = Locale::getDisplayRegion($locale, 'en');
    echo $locale."===>".$countryName.PHP_EOL;
} 

Update 2

Gather day names, month names, currency per locale

$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;
    }
}

As far as I read on the docs, user has to gather the per locale information with methods like above. There's a library which can be beneficial for this purposes. https://github.com/ICanBoogie/CLDR

like image 102
Ugur Avatar answered Sep 22 '22 15:09

Ugur