Skip to content

Commit c4b97d7

Browse files
committed
[String] Fix AsciiSlugger with emojis
1 parent 7300cc9 commit c4b97d7

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/Symfony/Component/String/Slugger/AsciiSlugger.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ public function slug(string $string, string $separator = '-', string $locale = n
113113
$transliterator = (array) $this->createTransliterator($locale);
114114
}
115115

116-
if (\is_string($this->emoji)) {
117-
$transliterator[] = EmojiTransliterator::create("emoji-{$this->emoji}");
118-
} elseif ($this->emoji && null !== $locale) {
119-
$transliterator[] = EmojiTransliterator::create("emoji-{$locale}");
116+
if ($emojiTransliterator = $this->createEmojiTransliterator(\is_string($this->emoji) ? $this->emoji : ($this->emoji && null !== $locale ? $locale : null))) {
117+
$transliterator[] = $emojiTransliterator;
120118
}
121119

122120
if ($this->symbolsMap instanceof \Closure) {
@@ -177,6 +175,19 @@ private function createTransliterator(string $locale): ?\Transliterator
177175
return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null;
178176
}
179177

178+
private function createEmojiTransliterator(?string $locale): ?EmojiTransliterator
179+
{
180+
while (null !== $locale) {
181+
try {
182+
return EmojiTransliterator::create("emoji-$locale");
183+
} catch (\IntlException) {
184+
$locale = self::getParentLocale($locale);
185+
}
186+
}
187+
188+
return null;
189+
}
190+
180191
private static function getParentLocale(?string $locale): ?string
181192
{
182193
if (!$locale) {

src/Symfony/Component/String/Tests/Slugger/AsciiSluggerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,15 @@ public function provideSlugEmojiTests(): iterable
9494
'en',
9595
'github',
9696
];
97+
yield [
98+
'un-chat-qui-sourit-chat-noir-et-un-tete-de-lion-vont-au-parc-national',
99+
'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
100+
'fr_XX', // Fallback on parent locale
101+
];
102+
yield [
103+
'un-et-un-vont-au',
104+
'un 😺, 🐈‍⬛, et un 🦁 vont au 🏞️',
105+
'undefined_locale', // Behaves the same as if emoji support is disabled
106+
];
97107
}
98108
}

0 commit comments

Comments
 (0)