9

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

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

6

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

Ugur
  • 1,679
  • 14
  • 20
  • Thanks for answer. but i need to have all cldr information. like name of countries by each language. it's on cldr data. http://www.unicode.org/Public/cldr/27.0.1/ .. download files and see whole information. – Mohammad Hossein Fattahizadeh Jul 26 '15 at 05:56
  • 1
    @sweb can you tell me which xml file should I be looking cldr database? It's a little bit huge : ) I'm sure there are ways to map data with the predefined functions at some point, but if it's not enough maybe we should parse and map xml data. Also check my update it gives you country names per locales which includes the languages. – Ugur Jul 26 '15 at 08:40
  • i'm not expert on cldr data but seems be on core.zip [/common/main/*.xml] must be all locales data such as timezone nameof the weeks and much more .. i dont want to xml process it. cause php intl has it. name of days month and etc... i think some method or etc can give me this data. that what's i looking for. the method or class – Mohammad Hossein Fattahizadeh Jul 26 '15 at 16:08
  • more info on Zend_Locale_Data http://framework.zend.com/apidoc/1.10/_Locale_Data.html#Zend_Locale_Data::getList%28%29 take a look and try it – Mohammad Hossein Fattahizadeh Jul 26 '15 at 16:10
  • 1
    Yes @sweb parsing xml is meaningless. http://php.net/manual/en/class.resourcebundle.php does it for you if you like but the default database which comes with your build is installed by default as far as i can understand. I couldn't come up with a perfect solution to gather all the custom data (eg month names, day names, country names etc) for a locale. It needs a lot of digging in the documentation, which I managed to gather a bit of data, see my updated answer again. But https://github.com/ICanBoogie/CLDR using this repository may suit your needs to gather all the info from cldr. – Ugur Jul 26 '15 at 23:50
  • 1
    [Punic](https://github.com/punic/punic) is another PHP library that provides access to the CLDR data. To access the source data in slightly more processed format than the XML files, checkout out the [JSON files](https://github.com/unicode-cldr/) provided by the CLDR project. – Christian Schmidt May 22 '17 at 08:51