Skip to content

[String] ignore the current locale before transliterating ASCII codes with iconv() #60233

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 1 commit into from
Apr 18, 2025
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
22 changes: 14 additions & 8 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,21 @@ public function ascii(array $rules = []): self
} elseif (!\function_exists('iconv')) {
$s = preg_replace('/[^\x00-\x7F]/u', '?', $s);
} else {
$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);

if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
}
$previousLocale = setlocale(\LC_CTYPE, 0);
try {
setlocale(\LC_CTYPE, 'C');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whta about this?

Suggested change
setlocale(\LC_CTYPE, 'C');
setlocale(\LC_CTYPE, 'C.UTF-8', 'C');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or actually C.UTF-8 creates the issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, C.UTF-8 produces the wrong result.

$s = @preg_replace_callback('/[^\x00-\x7F]/u', static function ($c) {
$c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);

if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
throw new \LogicException(sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
}

return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
}, $s);
return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
}, $s);
} finally {
setlocale(\LC_CTYPE, $previousLocale);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/String/Tests/Slugger/AsciiSluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,19 @@ public static function provideSlugEmojiTests(): iterable
'undefined_locale', // Behaves the same as if emoji support is disabled
];
}

/**
* @requires extension intl
*/
public function testSlugEmojiWithSetLocale()
{
if (!setlocale(LC_ALL, 'C.UTF-8')) {
$this->markTestSkipped('Unable to switch to the "C.UTF-8" locale.');
}

$slugger = new AsciiSlugger();
$slugger = $slugger->withEmoji(true);

$this->assertSame('a-and-a-go-to', (string) $slugger->slug('a 😺, 🐈‍⬛, and a 🦁 go to 🏞️... 😍 🎉 💛', '-'));
}
}
Loading