Skip to content

Commit

Permalink
Better efficiency & hook compatibility in AsciiTransliterator::intl()
Browse files Browse the repository at this point in the history
Previously we were making a new \Transilterator instance for each call to AsciiTransliterator::toAscii(), which was unnecessary and inefficient. Also, if there was no language-specific transform for transliterating from the forum's default language into Latin characters, then any changes added by mods using the integrate_ascii_transliterator_id hook would end up being discarded. Both issues have now been fixed.

Signed-off-by: Jon Stovell <jonstovell@gmail.com>
  • Loading branch information
Sesquipedalian committed Feb 21, 2025
1 parent 8188978 commit d68bc08
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions Sources/Localization/AsciiTransliterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@
*/
class AsciiTransliterator
{
/****************************
* Internal static properties
****************************/

/**
* @var object
*
* An instance of \Transliterator to be used by self::intl().
*/
private static \Transliterator $transliterator;

/***********************
* Public static methods
***********************/
Expand Down Expand Up @@ -63,24 +74,24 @@ public static function toAscii(string $string): string
*/
protected static function intl(string $string): string
{
/*
* First use the specific transliterator method for converting the
* forum's default language to Latin characters, if there is one.
* Then use the generic Any-Latin to convert any other characters.
* Finally, convert Latin to ASCII to get rid of accents and such.
*/
$transliterator_id = Lang::$default . '-Latin; Any-Latin; Latin-ASCII';
if (!isset(self::$transliterator)) {
// First use the specific transliterator method for converting the
// forum's default language to Latin characters, if there is one.
// Then use the generic Any-Latin to convert any other characters.
// Finally, convert Latin to ASCII to get rid of accents and such.
self::$transliterator = ($temp = \Transliterator::create(Lang::$default . '-Latin; Any-Latin; Latin-ASCII')) instanceof \Transliterator ? $temp : \Transliterator::create('Any-Latin; Latin-ASCII');

// Allow mods to adjust the transliterator identifiers.
IntegrationHook::call('integrate_ascii_transliterator_id', [&$transliterator_id]);
// Allow mods to adjust the transliterator identifier string.
$id = self::$transliterator->id;

$transliterator = \Transliterator::create($transliterator_id);
IntegrationHook::call('integrate_ascii_transliterator_id', [&$id]);

if (!($transliterator instanceof \Transliterator)) {
$transliterator = \Transliterator::create('Any-Latin; Latin-ASCII');
if ($id !== self::$transliterator->$id) {
self::$transliterator = ($temp = \Transliterator::create($id)) instanceof \Transliterator ? $temp : self::$transliterator;
}
}

return $transliterator->transliterate($string);
return self::$transliterator->transliterate($string);
}

/**
Expand Down

0 comments on commit d68bc08

Please sign in to comment.