-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[String] Introduce a locale-aware Slugger in the String component #33768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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_')) { | ||
tgalopin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// 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 hidden or 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 hidden or 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', '_')); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.