Skip to content

[Intl] Simplify API #28846

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
Apr 15, 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
[Intl] Simplify API
  • Loading branch information
ro0NL authored and fabpot committed Apr 15, 2019
commit d6b67d469a8767d88d5745b11b711c644e340ffc
9 changes: 9 additions & 0 deletions UPGRADE-4.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ HttpKernel
* Renamed `PostResponseEvent` to `TerminateEvent`
* Deprecated `TranslatorListener` in favor of `LocaleAwareListener`

Intl
----

* Deprecated `ResourceBundle` namespace
* Deprecated `Intl::getCurrencyBundle()`, use `Currencies` instead
* Deprecated `Intl::getLanguageBundle()`, use `Languages` or `Scripts` instead
* Deprecated `Intl::getLocaleBundle()`, use `Locales` instead
* Deprecated `Intl::getRegionBundle()`, use `Regions` instead

Messenger
---------

Expand Down
9 changes: 9 additions & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ HttpKernel
* Removed `PostResponseEvent`, use `TerminateEvent` instead
* Removed `TranslatorListener` in favor of `LocaleAwareListener`

Intl
----

* Removed `ResourceBundle` namespace
* Removed `Intl::getLanguageBundle()`, use `Languages` or `Scripts` instead
* Removed `Intl::getCurrencyBundle()`, use `Currencies` instead
* Removed `Intl::getLocaleBundle()`, use `Locales` instead
* Removed `Intl::getRegionBundle()`, use `Regions` instead

Messenger
---------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Regions;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
$choiceTranslationLocale = $options['choice_translation_locale'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
return array_flip(Regions::getNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
Expand Down Expand Up @@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames()), $value);
return $this->choiceList = new ArrayChoiceList(array_flip(Regions::getNames()), $value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Currencies;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
$choiceTranslationLocale = $options['choice_translation_locale'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames($choiceTranslationLocale));
return array_flip(Currencies::getNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
Expand Down Expand Up @@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getCurrencyBundle()->getCurrencyNames()), $value);
return $this->choiceList = new ArrayChoiceList(array_flip(Currencies::getNames()), $value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Languages;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
$choiceTranslationLocale = $options['choice_translation_locale'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getLanguageBundle()->getLanguageNames($choiceTranslationLocale));
return array_flip(Languages::getNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
Expand Down Expand Up @@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLanguageBundle()->getLanguageNames()), $value);
return $this->choiceList = new ArrayChoiceList(array_flip(Languages::getNames()), $value);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Locales;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand Down Expand Up @@ -44,7 +44,7 @@ public function configureOptions(OptionsResolver $resolver)
$choiceTranslationLocale = $options['choice_translation_locale'];

return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getLocaleBundle()->getLocaleNames($choiceTranslationLocale));
return array_flip(Locales::getNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
Expand Down Expand Up @@ -83,7 +83,7 @@ public function loadChoiceList($value = null)
return $this->choiceList;
}

return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getLocaleBundle()->getLocaleNames()), $value);
return $this->choiceList = new ArrayChoiceList(array_flip(Locales::getNames()), $value);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^7.1.3",
"symfony/event-dispatcher": "^4.3",
"symfony/intl": "~3.4|~4.0",
"symfony/intl": "^4.3",
"symfony/options-resolver": "~4.2",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-mbstring": "~1.0",
Expand All @@ -43,6 +43,7 @@
"symfony/doctrine-bridge": "<3.4",
"symfony/framework-bundle": "<3.4",
"symfony/http-kernel": "<4.3",
"symfony/intl": "<4.3",
"symfony/translation": "<4.2",
"symfony/twig-bridge": "<3.4.5|<4.0.5,>=4.0"
},
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Intl/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
CHANGELOG
=========

4.3.0
-----

* deprecated `ResourceBundle` namespace
* added `Currencies` in favor of `Intl::getCurrencyBundle()`
* added `Languages` and `Scripts` in favor of `Intl::getLanguageBundle()`
* added `Locales` in favor of `Intl::getLocaleBundle()`
* added `Regions` in favor of `Intl::getRegionBundle()`

4.2.0
-----

Expand Down
117 changes: 117 additions & 0 deletions src/Symfony/Component/Intl/Currencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl;

use Symfony\Component\Intl\Exception\MissingResourceException;

/**
* Gives access to currency-related ICU data.
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class Currencies extends ResourceBundle
{
private const INDEX_SYMBOL = 0;
private const INDEX_NAME = 1;
private const INDEX_FRACTION_DIGITS = 0;
private const INDEX_ROUNDING_INCREMENT = 1;

/**
* @return string[]
*/
public static function getCurrencyCodes(): array
{
return self::readEntry(['Currencies'], 'meta');
}

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

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

public static function getName(string $currency, string $displayLocale = null): string
{
return self::readEntry(['Names', $currency, self::INDEX_NAME], $displayLocale);
}

/**
* @return string[]
*/
public static function getNames(string $displayLocale = null): array
{
// ====================================================================
// For reference: It is NOT possible to return names indexed by
// numeric code here, because some numeric codes map to multiple
// 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS")
// ====================================================================

$names = self::readEntry(['Names'], $displayLocale);

if ($names instanceof \Traversable) {
$names = iterator_to_array($names);
}

array_walk($names, function (&$value) {
$value = $value[self::INDEX_NAME];
});

return self::asort($names, $displayLocale);
}

public static function getSymbol(string $currency, string $displayLocale = null): string
{
return self::readEntry(['Names', $currency, self::INDEX_SYMBOL], $displayLocale);
}

public static function getFractionDigits(string $currency): int
{
try {
return self::readEntry(['Meta', $currency, self::INDEX_FRACTION_DIGITS], 'meta');
} catch (MissingResourceException $e) {
return self::readEntry(['Meta', 'DEFAULT', self::INDEX_FRACTION_DIGITS], 'meta');
}
}

/**
* @return float|int
*/
public static function getRoundingIncrement(string $currency)
{
try {
return self::readEntry(['Meta', $currency, self::INDEX_ROUNDING_INCREMENT], 'meta');
} catch (MissingResourceException $e) {
return self::readEntry(['Meta', 'DEFAULT', self::INDEX_ROUNDING_INCREMENT], 'meta');
}
}

public static function getNumericCode(string $currency): int
{
return self::readEntry(['Alpha3ToNumeric', $currency], 'meta');
}

public static function forNumericCode(int $numericCode): array
{
return self::readEntry(['NumericToAlpha3', (string) $numericCode], 'meta');
}

protected static function getPath(): string
{
return Intl::getDataDirectory().'/'.Intl::CURRENCY_DIR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal
* @internal to be removed in 5.0.
*/
class CurrencyDataProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal
* @internal to be removed in 5.0.
*/
class LanguageDataProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal
* @internal to be removed in 5.0.
*/
class LocaleDataProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal
* @internal to be removed in 5.0.
*/
class RegionDataProvider
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @internal
* @internal to be removed in 5.0.
*/
class ScriptDataProvider
{
Expand Down
Loading