Skip to content

Commit

Permalink
Merge 03f4f43 into 06d663b
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys authored Jun 6, 2020
2 parents 06d663b + 03f4f43 commit 6023824
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 93 deletions.
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
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
Loading

0 comments on commit 6023824

Please sign in to comment.