Skip to content

Commit

Permalink
Merge 9c837dd into 06d663b
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 4, 2020
2 parents 06d663b + 9c837dd commit f445e2f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 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
2 changes: 2 additions & 0 deletions src/Twig/Extension/LocaleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
/**
* LocaleExtension extends Twig with local capabilities.
*
* @final since sonata-project/intl-bundle 2.x
*
* @author Thomas Rabaix <thomas.rabaix@ekino.com>
*/
class LocaleExtension extends AbstractExtension
Expand Down
9 changes: 5 additions & 4 deletions tests/Helper/LocaleHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
use PHPUnit\Framework\TestCase;
use Sonata\IntlBundle\Locale\LocaleDetectorInterface;
use Sonata\IntlBundle\Templating\Helper\LocaleHelper;
use Symfony\Component\Templating\Helper\HelperInterface;

class LocaleHelperTest extends TestCase
{
public function getHelper()
public function getHelper(): HelperInterface
{
$localeDetector = $this->createMock(LocaleDetectorInterface::class);
$localeDetector
Expand All @@ -31,7 +32,7 @@ public function getHelper()
/**
* @group legacy
*/
public function testLanguage()
public function testLanguage(): void
{
$helper = $this->getHelper();
$this->assertSame('français', $helper->language('fr'));
Expand All @@ -40,15 +41,15 @@ public function testLanguage()
$this->assertSame('French', $helper->language('fr', 'en'));
}

public function testCountry()
public function testCountry(): void
{
$helper = $this->getHelper();
$this->assertSame('France', $helper->country('FR'));
$this->assertSame('France', $helper->country('FR', 'en'));
// $this->assertEquals('', $helper->country('FR', 'fake'));
}

public function testLocale()
public function testLocale(): void
{
$helper = $this->getHelper();

Expand Down

0 comments on commit f445e2f

Please sign in to comment.