Skip to content

Commit

Permalink
Merge 1ef2029 into 06d663b
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 7, 2020
2 parents 06d663b + 1ef2029 commit 027b7af
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 95 deletions.
16 changes: 16 additions & 0 deletions UPGRADE-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ UPGRADE 2.x
UPGRADE FROM 2.7 to 2.8
=======================

### Locale helper

``Sonata\IntlBundle\Templating\Helper\LocaleHelper`` is marked as final, you must
not inherit this class.

``Sonata\IntlBundle\Templating\Helper\LocaleHelper::__construct()`` expects ``Twig\Extra\Intl\IntlExtension``
in argument 3.

### Number helper

``Sonata\IntlBundle\Templating\Helper\NumberHelper`` is marked as final, you must
not inherit this class.

``Sonata\IntlBundle\Templating\Helper\LocaleHelper::__construct()`` expects ``Twig\Extra\Intl\IntlExtension``
in argument 6.

### Timezone detector

``Sonata\IntlBundle\Timezone\TimezoneAwareInterface`` was added in order to provide
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"symfony/http-kernel": "^4.4",
"symfony/intl": "^4.4",
"symfony/templating": "^4.4",
"twig/extra-bundle": "^3.0",
"twig/intl-extra": "^3.0",
"twig/twig": "^2.9"
},
"conflict": {
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/number.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ For a list of available values, check the PHP_ documentation.
{{ 42|number_format_spellout }} {# => quarante-deux #}
{{ 1.999|number_format_percent }} {# => 200 % #}
{{ 1|number_format_ordinal }} {# => 1ᵉʳ #}
{{ (-1.1337)|number_format_decimal({'fraction_digits': 2}, {'negative_prefix': 'MINUS'}) }} {# => MINUS1,34 #}
{{ (-1.1337)|number_format_decimal({'fraction_digit': 2}, {'negative_prefix': 'MINUS'}) }} {# => MINUS1,34 #}
PHP usage
^^^^^^^^^
Expand All @@ -56,7 +56,7 @@ When defining your Admin, you can also provide extra parameters::
{
$listMapper
->add('amount', 'decimal', [
'attributes' => ['fraction_digits' => 2],
'attributes' => ['fraction_digit' => 2],
'textAttributes' => ['negative_prefix' => 'MINUS'],
])
;
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/config/intl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
<tag name="templating.helper" alias="locale"/>
<argument>%kernel.charset%</argument>
<argument type="service" id="sonata.intl.locale_detector"/>
<argument type="service" id="twig.extension.intl"/>
</service>
<service id="sonata.intl.templating.helper.number" class="%sonata.intl.templating.helper.number.class%" public="true">
<tag name="templating.helper" alias="number"/>
<argument>%kernel.charset%</argument>
<argument type="service" id="sonata.intl.locale_detector"/>
<argument type="collection"/>
<argument type="collection"/>
<argument type="collection"/>
<argument type="service" id="twig.extension.intl"/>
</service>
<service id="sonata.intl.templating.helper.datetime" class="%sonata.intl.templating.helper.datetime.class%" public="true">
<tag name="templating.helper" alias="datetime"/>
Expand Down
72 changes: 51 additions & 21 deletions src/Templating/Helper/LocaleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,36 @@

namespace Sonata\IntlBundle\Templating\Helper;

use Sonata\IntlBundle\Locale\LocaleDetectorInterface;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Languages;
use Symfony\Component\Intl\Locales;
use Twig\Extra\Intl\IntlExtension;

/**
* LocaleHelper displays culture information.
*
* @final since sonata-project/intl-bundle 2.x
*
* @author Thomas Rabaix <thomas.rabaix@ekino.com>
*/
class LocaleHelper extends BaseHelper
{
/**
* @var IntlExtension|null
*/
private $intlExtension;

/**
* @param string $charset The output charset of the helper
*/
public function __construct(string $charset, LocaleDetectorInterface $localeDetector, ?IntlExtension $intlExtension = null)
{
parent::__construct($charset, $localeDetector);

$this->intlExtension = $intlExtension;
}

/**
* @param string $code
* @param string|null $locale
Expand All @@ -33,13 +51,19 @@ class LocaleHelper extends BaseHelper
*/
public function country($code, $locale = null)
{
// NEXT_MAJOR: Remove this when dropping < 4.3 Symfony support
if (!class_exists(Countries::class)) {
$name = Intl::getRegionBundle()->getCountryName($code, $locale ?: $this->localeDetector->getLocale());

return $name ? $this->fixCharset($name) : '';
if ($this->intlExtension) {
return $this->fixCharset($this->intlExtension->getCountryName($code, $locale ?: $this->localeDetector->getLocale()));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 3 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception since version 3.x.',
IntlExtension::class,
__CLASS__
));

return $this->fixCharset(Countries::getName($code, $locale ?: $this->localeDetector->getLocale()));
}

Expand All @@ -51,18 +75,18 @@ public function country($code, $locale = null)
*/
public function language($code, $locale = null)
{
// NEXT_MAJOR: Remove this when dropping < 4.3 Symfony support
if (!class_exists(Languages::class)) {
$codes = explode('_', $code);
if ($this->intlExtension) {
$this->fixCharset($this->intlExtension->getLanguageName($code, $locale));
}

$name = Intl::getLanguageBundle()->getLanguageName(
$codes[0],
$codes[1] ?? null,
$locale ?: $this->localeDetector->getLocale()
);
// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

return $name ? $this->fixCharset($name) : '';
}
@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 3 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception since version 3.x.',
IntlExtension::class,
__CLASS__
));

return $this->fixCharset(Languages::getName($code, $locale ?: $this->localeDetector->getLocale()));
}
Expand All @@ -75,13 +99,19 @@ public function language($code, $locale = null)
*/
public function locale($code, $locale = null)
{
// NEXT_MAJOR: Remove this when dropping < 4.3 Symfony support
if (!class_exists(Locales::class)) {
$name = Intl::getLocaleBundle()->getLocaleName($code, $locale ?: $this->localeDetector->getLocale());

return $name ? $this->fixCharset($name) : '';
if ($this->intlExtension) {
$this->fixCharset($this->intlExtension->getLocaleName($code, $locale));
}

// NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method.

@trigger_error(sprintf(
'Not passing an instance of "%s" as argument 3 for %s::__construct() is deprecated since sonata-project/intl-bundle 2.x.'
.' and will throw an exception since version 3.x.',
IntlExtension::class,
__CLASS__
));

return $this->fixCharset(Locales::getName($code, $locale ?: $this->localeDetector->getLocale()));
}

Expand Down

0 comments on commit 027b7af

Please sign in to comment.