Skip to content

Commit

Permalink
Merge f1fa92e into 06d663b
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 6, 2020
2 parents 06d663b + f1fa92e commit 375beea
Show file tree
Hide file tree
Showing 7 changed files with 326 additions and 77 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
1 change: 1 addition & 0 deletions src/Resources/config/intl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<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"/>
Expand Down
68 changes: 46 additions & 22 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,17 @@ 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()));
}

@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,19 +73,17 @@ 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);

$name = Intl::getLanguageBundle()->getLanguageName(
$codes[0],
$codes[1] ?? null,
$locale ?: $this->localeDetector->getLocale()
);

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

@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 +95,17 @@ 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));
}

@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 375beea

Please sign in to comment.