8000 [Intl] Support ISO 3166-1 Alpha-3 country codes by terjebraten-certua · Pull Request #32988 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Intl] Support ISO 3166-1 Alpha-3 country codes #32988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Support ISO 3166-1 Alpha-3 country codes
  • Loading branch information
terjebraten-certua committed Aug 9, 2019
commit 848f60e90589e99fab02a57296b8418deba5586a
1 change: 1 addition & 0 deletions src/Symfony/Component/Intl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* excluded language code `root`
* added to both `Countries` and `Languages` the methods `getAlpha3Codes`, `getAlpha3Code`, `getAlpha2Code`, `alpha3CodeExists`, `getAlpha3Name` and `getAlpha3Names`

4.3.0
-----
Expand Down
70 changes: 65 additions & 5 deletions src/Symfony/Component/Intl/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,44 @@ public static function getCountryCodes(): array
}

/**
* @param string $country Alpha2 country code
* Returns all available countries (3 letters).
*
* Countries are returned as uppercase ISO 3166 three-letter country codes.
*
* This list only contains "officially assigned ISO 3166-1 alpha-3" country codes.
*
* @return string[] an array of canonical ISO 3166 alpha-3 country codes
*/
public static function exists(string $country): bool
public static function getAlpha3Codes(): array
{
return self::readEntry(['Alpha2ToAlpha3'], 'meta');
}

public static function getAlpha3Code(string $alpha2Code): string
{
return self::readEntry(['Alpha2ToAlpha3', $alpha2Code], 'meta');
}

public static function getAlpha2Code(string $alpha3Code): string
{
return self::readEntry(['Alpha3ToAlpha2', $alpha3Code], 'meta');
}

public static function exists(string $alpha2Code): bool
{
try {
self::readEntry(['Names', $country]);
self::readEntry(['Names', $alpha2Code]);

return true;
} catch (MissingResourceException $e) {
return false;
}
}

public static function alpha3CodeExists(string $alpha3Code): bool
{
try {
self::getAlpha2Code($alpha3Code);

return true;
} catch (MissingResourceException $e) {
Expand All @@ -53,15 +85,25 @@ public static function exists(string $country): bool
}

/**
* Gets the country name from alpha2 code.
* Gets the country name from its alpha2 code.
*
* @throws MissingResourceException if the country code does not exists
* @throws MissingResourceException if the country code does not exist
*/
public static function getName(string $country, string $displayLocale = null): string
{
return self::readEntry(['Names', $country], $displayLocale);
}

/**
* Gets the country name from its alpha3 code.
*
* @throws MissingResourceException if the country code does not exist
*/
public static function getAlpha3Name(string $alpha3Code, string $displayLocale = null): string
{
return self::getName(self::getAlpha2Code($alpha3Code), $displayLocale);
}

/**
* Gets the list of country names indexed with alpha2 codes as keys.
*
Expand All @@ -72,6 +114,24 @@ public static function getNames($displayLocale = null): array
return self::asort(self::readEntry(['Names'], $displayLocale), $displayLocale);
}

/**
* Gets the list of country names indexed with alpha3 codes as keys.
*
* Same as method getNames, but with alpha3 codes instead of alpha2 codes as keys.
*
* @return string[]
*/
public static function getAlpha3Names($displayLocale = null): array
{
$alpha2Names = self::getNames($displayLocale);
$alpha3Names = [];
foreach ($alpha2Names as $alpha2Code => $name) {
$alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
}

return $alpha3Names;
}

protected static function getPath(): string
{
return Intl::getDataDirectory().'/'.Intl::REGION_DIR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,16 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, $temp

sort($this->languageCodes);

$alpha2ToAlpha3 = $this->generateAlpha2ToAlpha3Mapping($metadataBundle);
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
asort($alpha3ToAlpha2);

return [
'Version' => $rootBundle['Version'],
'Languages' => $this->languageCodes,
'Aliases' => array_column(iterator_to_array($metadataBundle['alias']['language']), 'replacement'),
'Alpha2ToAlpha3' => $this->generateAlpha2ToAlpha3Mapping($metadataBundle),
'Alpha2ToAlpha3' => $alpha2ToAlpha3,
'Alpha3ToAlpha2' => $alpha3ToAlpha2,
];
}

Expand Down
57 changes: 57 additions & 0 deletions src/Symfony/Component/Intl/Data/Generator/RegionDataGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
*/
class RegionDataGenerator extends AbstractDataGenerator
{
/**
* Source https://www.iso.org/obp/ui/#iso:pub:PUB500001:en
*/
private static $preferredAlpha2ToAlpha3Mapping = [
'DE' => 'DEU',
'FR' => 'FRA',
'MM' => 'MMR',
'TL' => 'TLS',
'YE' => 'YEM',
];

private static $blacklist = [
// Exceptional reservations
'AC' => true, // Ascension Island
Expand Down Expand Up @@ -82,6 +93,7 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
protected function compileTemporaryBundles(BundleCompilerInterface $compiler, $sourceDir, $tempDir)
{
$compiler->compile($sourceDir.'/region', $tempDir);
$compiler->compile($sourceDir.'/misc/metadata.txt', $tempDir);
}

/**
Expand Down Expand Up @@ -125,14 +137,25 @@ protected function generateDataForRoot(BundleEntryReaderInterface $reader, $temp
protected function generateDataForMeta(BundleEntryReaderInterface $reader, $tempDir)
{
$rootBundle = $reader->read($tempDir, 'root');
$metadataBundle = $reader->read($tempDir, 'metadata');

$this->regionCodes = array_unique($this->regionCodes);

$alpha2ToAlpha3 = $this->generateAlpha3($metadataBundle);

sort($this->regionCodes);

$alpha3ToAlpha2 = [];
foreach ($this->regionCodes as $alpha2Code) {
$alpha3code = $alpha2ToAlpha3[$alpha2Code];
$alpha3ToAlpha2[$alpha3code] = $alpha2Code;
}

return [
'Version' => $rootBundle['Version'],
'Regions' => $this->regionCodes,
'Alpha2ToAlpha3' => $alpha2ToAlpha3,
'Alpha3ToAlpha2' => $alpha3ToAlpha2,
];
}

Expand All @@ -154,4 +177,38 @@ protected function generateRegionNames(ArrayAccessibleResourceBundle $localeBund

return $regionNames;
}

protected function generateAlpha3(ArrayAccessibleResourceBundle $metadataBundle)
{
$alpha2Codes = array_flip($this->regionCodes);
$alpha2ToAlpha3 = [];
foreach ($metadataBundle['alias']['territory'] as $alias => $data) {
if (3 !== \strlen($alias) || 'overlong' !== $data['reason'] || ctype_digit($alias)) {
continue;
}

$alpha2Code = $data['replacement'];
if (!isset($alpha2Codes[$alpha2Code])) {
continue;
}

if (!isset($alpha2ToAlpha3[$alpha2Code])) {
$alpha2ToAlpha3[$alpha2Code] = $alias;
continue;
}

// Found a second alias for the same country
if (isset(self::$preferredAlpha2ToAlpha3Mapping[$alpha2Code])) {
$preferred = self::$preferredAlpha2ToAlpha3Mapping[$alpha2Code];
// Only use the preferred mapping if it actually is in the mapping
if ($alias === $preferred) {
$alpha F438 2ToAlpha3[$alpha2Code] = $preferred;
}
}
}

asort($alpha2ToAlpha3);

return $alpha2ToAlpha3;
}
}
71 changes: 69 additions & 2 deletions src/Symfony/Component/Intl/Languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static function exists(string $language): bool
}

/**
* Gets the language name from alpha2 code.
* Gets the language name from its alpha2 code.
*
* @throws MissingResourceException if the language code does not exists
* @throws MissingResourceException if the language code does not exist
*/
public static function getName(string $language, string $displayLocale = null): string
{
Expand All @@ -79,6 +79,73 @@ public static function getAlpha3Code(string $language): string
return self::readEntry(['Alpha2ToAlpha3', $language], 'meta');
}

/**
* Returns the ISO 639-1 two-letter code of a language, given a three letter code.
*
* @throws MissingResourceException if the language has no corresponding three-letter code
*/
public static function getAlpha2Code(string $language): string
{
return self::readEntry(['Alpha3ToAlpha2', $language], 'meta');
}

/**
* Returns all available languages as three-letter codes.
*
* Languages are returned as lowercase ISO 639-2 three-letter language codes.
*
* @return string[] an array of canonical ISO 639-2 language codes
*/
public static function getAlpha3Codes(): array
{
return self::readEntry(['Alpha2ToAlpha3'], 'meta');
}

/**
* @param string $language ISO 639-2 three-letter language code
*/
public static function alpha3CodeExists(string $language): bool
{
try {
self::getAlpha2Code($language);

return true;
} catch (MissingResourceException $e) {
return false;
}
}

/**
* Gets the language name from its ISO 639-2 three-letter code.
*
* @throws MissingResourceException if the country code does not exists
*/
public static function getAlpha3Name(string $language, string $displayLocale = null): string
{
return self::getName(self::getAlpha2Code($language), $displayLocale);
}

/**
* Gets the list of language names indexed with ISO 639-2 three-letter codes as keys.
*
* Same as method getNames, but with ISO 639-2 three-letter codes instead of ISO 639-1 codes as keys.
*
* @return string[]
*/
public static function getAlpha3Names($displayLocale = null): array
{
$alpha2Names = self::getNames($displayLocale);
$alpha3Names = [];
foreach ($alpha2Names as $alpha2Code => $name) {
try {
$alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
} catch (MissingResourceException $e) {
}
}

return $alpha3Names;
}

protected static function getPath(): string
{
return Intl::getDataDirectory().'/'.Intl::LANGUAGE_DIR;
Expand Down
Loading
0