Skip to content

Commit 330e167

Browse files
minor #60233 [String] ignore the current locale before transliterating ASCII codes with iconv() (xabbuh)
This PR was merged into the 6.4 branch. Discussion ---------- [String] ignore the current locale before transliterating ASCII codes with iconv() | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT see https://github.com/symfony/symfony/actions/runs/14509713498/job/40705561464?pr=58370#step:9:1891 Commits ------- d304d5d ignore the current locale before transliterating ASCII codes with iconv()
2 parents c72a7c4 + d304d5d commit 330e167

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Symfony/Component/String/AbstractUnicodeString.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,21 @@ public function ascii(array $rules = []): self
135135
} elseif (!\function_exists('iconv')) {
136136
$s = preg_replace('/[^\x00-\x7F]/u', '?', $s);
137137
} else {
138-
$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
139-
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);
140-
141-
if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
142-
throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
143-
}
138+
$previousLocale = setlocale(\LC_CTYPE, 0);
139+
try {
140+
setlocale(\LC_CTYPE, 'C');
141+
$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
142+
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);
143+
144+
if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
145+
throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
146+
}
144147

145-
return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
146-
}, $s);
148+
return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
149+
}, $s);
150+
} finally {
151+
setlocale(\LC_CTYPE, $previousLocale);
152+
}
147153
}
148154
}
149155

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,19 @@ public static function provideSlugEmojiTests(): iterable
106106
'undefined_locale', // Behaves the same as if emoji support is disabled
107107
];
108108
}
109+
110+
/**
111+
* @requires extension intl
112+
*/
113+
public function testSlugEmojiWithSetLocale()
114+
{
115+
if (!setlocale(LC_ALL, 'C.UTF-8')) {
116+
$this->markTestSkipped('Unable to switch to the "C.UTF-8" locale.');
117+
}
118+
119+
$slugger = new AsciiSlugger();
120+
$slugger = $slugger->withEmoji(true);
121+
122+
$this->assertSame('a-and-a-go-to', (string) $slugger->slug('a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛', '-'));
123+
}
109124
}

0 commit comments

Comments
 (0)