Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/String/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3
---

* Made `AsciiSlugger` fallback to parent locale's symbolsMap

5.2.0
-----

Expand Down
36 changes: 30 additions & 6 deletions src/Symfony/Component/String/Slugger/AsciiSlugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public function slug(string $string, string $separator = '-', string $locale = n
}

if ($this->symbolsMap instanceof \Closure) {
// If the symbols map is passed as a closure, there is no need to fallback to the parent locale
// as the closure can just provide substitutions for all locales of interest.
$symbolsMap = $this->symbolsMap;
array_unshift($transliterator, static function ($s) use ($symbolsMap, $locale) {
return $symbolsMap($s, $locale);
Expand All @@ -119,9 +121,20 @@ public function slug(string $string, string $separator = '-', string $locale = n

$unicodeString = (new UnicodeString($string))->ascii($transliterator);

if (\is_array($this->symbolsMap) && isset($this->symbolsMap[$locale])) {
foreach ($this->symbolsMap[$locale] as $char => $replace) {
$unicodeString = $unicodeString->replace($char, ' '.$replace.' ');
if (\is_array($this->symbolsMap)) {
$map = null;
if (isset($this->symbolsMap[$locale])) {
$map = $this->symbolsMap[$locale];
} else {
$parent = self::getParentLocale($locale);
if ($parent && isset($this->symbolsMap[$parent])) {
$map = $this->symbolsMap[$parent];
}
}
if ($map) {
foreach ($map as $char => $replace) {
$unicodeString = $unicodeString->replace($char, ' '.$replace.' ');
}
}
}

Expand All @@ -143,17 +156,28 @@ private function createTransliterator(string $locale): ?\Transliterator
}

// Locale not supported and no parent, fallback to any-latin
if (false === $str = strrchr($locale, '_')) {
if (!$parent = self::getParentLocale($locale)) {
return $this->transliterators[$locale] = 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);
}

return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null;
}

private static function getParentLocale(?string $locale): ?string
{
if (!$locale) {
return null;
}
if (false === $str = strrchr($locale, '_')) {
// no parent locale
return null;
}

return substr($locale, 0, -\strlen($str));
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/String/Tests/SluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ public function testSlugCharReplacementLocaleMethod()
$this->assertSame('yo_y_tu_a_esta_direccion_slug_en_senal_test_es', $slug);
}

public function testSlugCharReplacementLocaleConstructWithoutSymbolsMap()
{
$slugger = new AsciiSlugger('en');
$slug = (string) $slugger->slug('you & me with this address [email protected]', '_');

$this->assertSame('you_and_me_with_this_address_slug_at_test_uk', $slug);
}

public function testSlugCharReplacementParentLocaleConstructWithoutSymbolsMap()
{
$slugger = new AsciiSlugger('en_GB');
$slug = (string) $slugger->slug('you & me with this address [email protected]', '_');

$this->assertSame('you_and_me_with_this_address_slug_at_test_uk', $slug);
}

public function testSlugCharReplacementParentLocaleConstruct()
{
$slugger = new AsciiSlugger('fr_FR', ['fr' => ['&' => 'et', '@' => 'chez']]);
$slug = (string) $slugger->slug('toi & moi avec cette adresse [email protected]', '_');

$this->assertSame('toi_et_moi_avec_cette_adresse_slug_chez_test_fr', $slug);
}

public function testSlugCharReplacementParentLocaleMethod()
{
$slugger = new AsciiSlugger(null, ['es' => ['&' => 'y', '@' => 'en senal']]);
$slug = (string) $slugger->slug('yo & tu a esta dirección [email protected]', '_', 'es_ES');
$this->assertSame('yo_y_tu_a_esta_direccion_slug_en_senal_test_es', $slug);
}

public function testSlugClosure()
{
$slugger = new AsciiSlugger(null, function ($s, $locale) {
Expand Down