From efea4dbe946442ce75e4bb33e884812b8f09b454 Mon Sep 17 00:00:00 2001 From: Hamish Friedlander Date: Fri, 24 Aug 2012 11:45:11 +1200 Subject: [PATCH] NEW Allow specifying priority for translations Priority for translations was hardcoded, and hardcoded the project name as "mysite". This takes the order from a configuration property "module_prority". You can use standard config fragment before and after rules to make a module less or more important than anything else, with these tweaks: - Unless it has it's order explicitly defined, the "project" module (normally mysite) will be considered highest priority - There is an "other_modules" value in the order list which will be replaced by all the modules (except the project module) that don't have their order explicitly defined. --- _config/i18n.yml | 15 +++++++++++++++ i18n/i18n.php | 41 +++++++++++++++++++++++++++-------------- 2 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 _config/i18n.yml diff --git a/_config/i18n.yml b/_config/i18n.yml new file mode 100644 index 000000000..8daa37abf --- /dev/null +++ b/_config/i18n.yml @@ -0,0 +1,15 @@ +--- +Name: basei18n +Before: '/i18n' +--- +i18n: + module_priority: + - admin + - framework + - sapphire +--- +Name: defaulti18n +--- +i18n: + module_priority: + - other_modules diff --git a/i18n/i18n.php b/i18n/i18n.php index 5c9cd1116..f4a93b40c 100644 --- a/i18n/i18n.php +++ b/i18n/i18n.php @@ -2472,20 +2472,33 @@ class i18n extends Object implements TemplateGlobalProvider { $cache = Zend_Translate::getCache(); if($cache) $cache->clean(Zend_Cache::CLEANING_MODE_ALL); } - - // Sort modules by inclusion priority, then alphabetically - // TODO Should be handled by priority flags within modules - $prios = array('sapphire' => 10, 'framework' => 10, 'admin' => 11, 'cms' => 12, project() => 90); + + // Get list of module => path pairs, and then just the names $modules = SS_ClassLoader::instance()->getManifest()->getModules(); - ksort($modules); - uksort( - $modules, - function($a, $b) use(&$prios) { - $prioA = (isset($prios[$a])) ? $prios[$a] : 50; - $prioB = (isset($prios[$b])) ? $prios[$b] : 50; - return ($prioA > $prioB); - } - ); + $moduleNames = array_keys($modules); + + // Remove the "project" module from the list - we'll add it back specially later if needed + global $project; + if (($idx = array_search($project, $moduleNames)) !== false) array_splice($moduleNames, $idx, 1); + + // Get the order from the config syste, + $order = Config::inst()->get('i18n', 'module_priority'); + + // Find all modules that don't have their order specified by the config system + $unspecified = array_diff($moduleNames, $order); + + // If the placeholder "other_modules" exists in the order array, replace it by the unspecified modules + if (($idx = array_search('other_modules', $order)) !== false) array_splice($order, $idx, 1, $unspecified); + // Otherwise just jam them on the front + else array_splice($order, 0, 0, $unspecified); + + // Put the project module back in at the begining if it wasn't specified by the config system + if (!in_array($project, $order)) array_unshift($order, $project); + + $sortedModules = array(); + foreach ($order as $module) { + if (isset($modules[$module])) $sortedModules[$module] = $modules[$module]; + } // Loop in reverse order, meaning the translator with the highest priority goes first $translators = array_reverse(self::get_translators(), true); @@ -2494,7 +2507,7 @@ class i18n extends Object implements TemplateGlobalProvider { $adapter = $translator->getAdapter(); // Load translations from modules - foreach($modules as $module) { + foreach($sortedModules as $module) { $filename = $adapter->getFilenameForLocale($locale); $filepath = "{$module}/lang/" . $filename;