From d90093a3407af9ef6c6813b37cebdae2318bd9ad Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Sun, 2 Aug 2015 20:49:10 -0500 Subject: [PATCH] topic: Sort localized list case insensitively --- include/class.i18n.php | 22 ++++++++++++++++++++++ include/class.topic.php | 15 ++++----------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/include/class.i18n.php b/include/class.i18n.php index ed3df93bf..3c0da2b24 100644 --- a/include/class.i18n.php +++ b/include/class.i18n.php @@ -461,6 +461,28 @@ class Internationalization { return $locales; } + static function sortKeyedList($list, $case=false) { + global $cfg; + + if ($cfg && function_exists('collator_create')) { + $coll = Collator::create($cfg->getPrimaryLanguage()); + if (!$case) + $coll->setStrength(Collator::TERTIARY); + // UASORT is necessary to preserve the keys + uasort($list, function($a, $b) use ($coll) { + return $coll->compare($a, $b); }); + } + else { + if (!$case) + uasort($list, function($a, $b) { + return strcmp(mb_strtoupper($a), mb_strtoupper($b)); }); + else + // Really only works on ascii names + asort($list); + } + return $list; + } + static function bootstrap() { require_once INCLUDE_DIR . 'class.translation.php'; diff --git a/include/class.topic.php b/include/class.topic.php index 9b98db058..b97513636 100644 --- a/include/class.topic.php +++ b/include/class.topic.php @@ -347,10 +347,12 @@ implements TemplateVariable { $requested_names[$id] = $n; } - // XXX: If localization requested and the current locale is not the + // If localization requested and the current locale is not the // primary, the list may need to be sorted. Caching is ok here, // because the locale is not going to be changed within a single // request. + if ($localize) + return Internationalization::sortKeyedList($requested_names); return $requested_names; } @@ -520,16 +522,7 @@ implements TemplateVariable { if (!($names = static::getHelpTopics(false, true, false))) return; - if ($cfg && function_exists('collator_create')) { - $coll = Collator::create($cfg->getPrimaryLanguage()); - // UASORT is necessary to preserve the keys - uasort($names, function($a, $b) use ($coll) { - return $coll->compare($a, $b); }); - } - else { - // Really only works on English names - asort($names); - } + $names = Internationalization::sortKeyedList($names); $update = array_keys($names); foreach ($update as $idx=>&$id) { -- GitLab