alcohol / iso3166

A PHP library providing ISO 3166-1 data.
https://iso3166.thephpleague.com
MIT License
649 stars 60 forks source link

Adding Localization #21

Closed nyamsprod closed 7 years ago

nyamsprod commented 8 years ago

A DataLocalizer class is added. This class enables localizing the country full name using:

The public API is as follow:

public DataLocalizer::__construct($key = 'name', $locale = '')
public DataLocalizer::localize(iterable $iterable): Generator
public DataLocalizer::__invoke(iterable $iterable): Generator
// __invoke is an alias of the localize method

A simple usage will be:

use League\ISO3166\ISO3166;
use League\ISO3166\DataLocalizer;

$localizer = new DataLocalizer('local_name', 'fr');
$collection = new ISO3166;
$res1 = $localizer($collection);
$res2 = $localizer->localize($collection->listBy(ISO3166::KEY_ALPHA2);
$res3 = $localizer->__invoke([$collection->getByAlpha3('SEN')]);

The result is a generator you can iterate over where each entry has a new key local_name with the country name being localized as its value.

A possible BC break is that this feature relies on PHP intl extension which is AFAIK bundle by default since PHP5.5.

This feature may resolve #11

alcohol commented 8 years ago

As much as I appreciate the work, I think I would rather create an additional package called iso3166-intl, which both adds this support and also introduces the extra dependency on ext-intl (assuming the intl extension is the preferred solution, I am not sure of that yet).