-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #33768 [String] Introduce a locale-aware Slugger in the Strin…
…g component (tgalopin) This PR was merged into the 5.0-dev branch. Discussion ---------- [String] Introduce a locale-aware Slugger in the String component | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR introduces a locale-aware Slugger in the new String component, based on transliterators provided by the PHP intl extension (or iconv if not available). It also wires this Slugger in the FrameworkBundle in order to use it easily as a service and to automatically inject the proper locale into it to choose the appropriate transliteration depending on the Request locale. See https://github.com/unicode-org/cldr/tree/master/common/transforms for CLDR mappings. Commits ------- 056d8ce [String] Introduce a locale-aware Slugger in the String component with FrameworkBundle wiring
- Loading branch information
Showing
13 changed files
with
324 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...le/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Slugger/SlugConstructArgService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Slugger; | ||
|
||
use Symfony\Component\String\Slugger\SluggerInterface; | ||
|
||
class SlugConstructArgService | ||
{ | ||
private $slugger; | ||
|
||
public function __construct(SluggerInterface $slugger) | ||
{ | ||
$this->slugger = $slugger; | ||
} | ||
|
||
public function hello(): string | ||
{ | ||
return $this->slugger->slug('Стойността трябва да бъде лъжа'); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SluggerLocaleAwareTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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\Bundle\FrameworkBundle\Tests\Functional; | ||
|
||
/** | ||
* @group functional | ||
*/ | ||
class SluggerLocaleAwareTest extends AbstractWebTestCase | ||
{ | ||
public function testLocalizedSlugger() | ||
{ | ||
$kernel = static::createKernel(['test_case' => 'Slugger', 'root_config' => 'config.yml']); | ||
$kernel->boot(); | ||
|
||
$service = $kernel->getContainer()->get('Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Slugger\SlugConstructArgService'); | ||
|
||
$this->assertSame('Stoinostta-tryabva-da-bude-luzha', $service->hello()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Slugger/bundles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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. | ||
*/ | ||
|
||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; | ||
|
||
return [ | ||
new FrameworkBundle(), | ||
new TestBundle(), | ||
]; |
14 changes: 14 additions & 0 deletions
14
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Slugger/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
- { resource: services.yml } | ||
|
||
framework: | ||
secret: '%secret%' | ||
default_locale: '%env(LOCALE)%' | ||
translator: | ||
fallbacks: | ||
- '%env(LOCALE)%' | ||
|
||
parameters: | ||
env(LOCALE): bg | ||
secret: test |
6 changes: 6 additions & 0 deletions
6
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Slugger/services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
_defaults: | ||
public: true | ||
|
||
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Slugger\SlugConstructArgService: | ||
arguments: ['@slugger'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?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\String\Slugger; | ||
|
||
use Symfony\Component\String\AbstractUnicodeString; | ||
use Symfony\Component\String\GraphemeString; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
|
||
/** | ||
* @author Titouan Galopin <galopintitouan@gmail.com> | ||
* | ||
* @experimental in 5.0 | ||
*/ | ||
class AsciiSlugger implements SluggerInterface, LocaleAwareInterface | ||
{ | ||
private const LOCALE_TO_TRANSLITERATOR_ID = [ | ||
'am' => 'Amharic-Latin', | ||
'ar' => 'Arabic-Latin', | ||
'az' => 'Azerbaijani-Latin', | ||
'be' => 'Belarusian-Latin', | ||
'bg' => 'Bulgarian-Latin', | ||
'bn' => 'Bengali-Latin', | ||
'de' => 'de-ASCII', | ||
'el' => 'Greek-Latin', | ||
'fa' => 'Persian-Latin', | ||
'he' => 'Hebrew-Latin', | ||
'hy' => 'Armenian-Latin', | ||
'ka' => 'Georgian-Latin', | ||
'kk' => 'Kazakh-Latin', | ||
'ky' => 'Kirghiz-Latin', | ||
'ko' => 'Korean-Latin', | ||
'mk' => 'Macedonian-Latin', | ||
'mn' => 'Mongolian-Latin', | ||
'or' => 'Oriya-Latin', | ||
'ps' => 'Pashto-Latin', | ||
'ru' => 'Russian-Latin', | ||
'sr' => 'Serbian-Latin', | ||
'sr_Cyrl' => 'Serbian-Latin', | ||
'th' => 'Thai-Latin', | ||
'tk' => 'Turkmen-Latin', | ||
'uk' => 'Ukrainian-Latin', | ||
'uz' => 'Uzbek-Latin', | ||
'zh' => 'Han-Latin', | ||
]; | ||
|
||
private $defaultLocale; | ||
|
||
/** | ||
* Cache of transliterators per locale. | ||
* | ||
* @var \Transliterator[] | ||
*/ | ||
private $transliterators = []; | ||
|
||
public function __construct(string $defaultLocale = null) | ||
{ | ||
$this->defaultLocale = $defaultLocale; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setLocale($locale) | ||
{ | ||
$this->defaultLocale = $locale; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getLocale() | ||
{ | ||
return $this->defaultLocale; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function slug(string $string, string $separator = '-', string $locale = null): AbstractUnicodeString | ||
{ | ||
$locale = $locale ?? $this->defaultLocale; | ||
|
||
$transliterator = []; | ||
if ('de' === $locale || 0 === strpos($locale, 'de_')) { | ||
// Use the shortcut for German in GraphemeString::ascii() if possible (faster and no requirement on intl) | ||
$transliterator = ['de-ASCII']; | ||
} elseif (\function_exists('transliterator_transliterate') && $locale) { | ||
$transliterator = (array) $this->createTransliterator($locale); | ||
} | ||
|
||
return (new GraphemeString($string)) | ||
->ascii($transliterator) | ||
->replace('@', $separator.'at'.$separator) | ||
->replaceMatches('/[^A-Za-z0-9]++/', $separator) | ||
->trim($separator) | ||
; | ||
} | ||
|
||
private function createTransliterator(string $locale): ?\Transliterator | ||
{ | ||
if (isset($this->transliterators[$locale])) { | ||
return $this->transliterators[$locale]; | ||
} | ||
|
||
// Exact locale supported, cache and return | ||
if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$locale] ?? null) { | ||
return $this->transliterators[$locale] = \Transliterator::create($id.'/BGN') ?? \Transliterator::create($id); | ||
} | ||
|
||
// Locale not supported and no parent, fallback to any-latin | ||
if (false === $str = strrchr($locale, '_')) { | ||
return null; | ||
} | ||
|
||
// Try to use the parent locale (ie. try "de" for "de_AT") and cache both locales | ||
$parent = substr($locale, 0, -\strlen($str)); | ||
|
||
if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$parent] ?? null) { | ||
$transliterator = \Transliterator::create($id.'/BGN') ?? \Transliterator::create($id); | ||
$this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator; | ||
|
||
return $transliterator; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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\String\Slugger; | ||
|
||
use Symfony\Component\String\AbstractUnicodeString; | ||
|
||
/** | ||
* Creates a URL-friendly slug from a given string. | ||
* | ||
* @author Titouan Galopin <galopintitouan@gmail.com> | ||
* | ||
* @experimental in 5.0 | ||
*/ | ||
interface SluggerInterface | ||
{ | ||
/** | ||
* Creates a slug for the given string and locale, using appropriate transliteration when needed. | ||
*/ | ||
public function slug(string $string, string $separator = '-', string $locale = null): AbstractUnicodeString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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\String\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\String\Slugger\AsciiSlugger; | ||
|
||
class SluggerTest extends TestCase | ||
{ | ||
/** | ||
* @requires extension intl | ||
* @dataProvider provideSlug | ||
*/ | ||
public function testSlug(string $string, string $locale, string $expectedSlug) | ||
{ | ||
$slugger = new AsciiSlugger($locale); | ||
|
||
$this->assertSame($expectedSlug, (string) $slugger->slug($string)); | ||
} | ||
|
||
public static function provideSlug(): array | ||
{ | ||
return [ | ||
['Стойността трябва да бъде лъжа', 'bg', 'Stoinostta-tryabva-da-bude-luzha'], | ||
['Dieser Wert sollte größer oder gleich', 'de', 'Dieser-Wert-sollte-groesser-oder-gleich'], | ||
['Dieser Wert sollte größer oder gleich', 'de_AT', 'Dieser-Wert-sollte-groesser-oder-gleich'], | ||
['Αυτή η τιμή πρέπει να είναι ψευδής', 'el', 'Avti-i-timi-prepi-na-inai-psevdhis'], | ||
['该变量的值应为', 'zh', 'gai-bian-liang-de-zhi-ying-wei'], | ||
['該變數的值應為', 'zh_TW', 'gai-bian-shu-de-zhi-ying-wei'], | ||
]; | ||
} | ||
|
||
public function testSeparatorWithoutLocale() | ||
{ | ||
$slugger = new AsciiSlugger(); | ||
|
||
$this->assertSame('hello-world', (string) $slugger->slug('hello world')); | ||
$this->assertSame('hello_world', (string) $slugger->slug('hello world', '_')); | ||
} | ||
} |