Permalink
Browse files

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.
  • Loading branch information...
1 parent fe2663a commit efea4dbe946442ce75e4bb33e884812b8f09b454 Hamish Friedlander committed with sminnee Aug 23, 2012
Showing with 42 additions and 14 deletions.
  1. +15 −0 _config/i18n.yml
  2. +27 −14 i18n/i18n.php
View
@@ -0,0 +1,15 @@
+---
+Name: basei18n
+Before: '/i18n'
+---
+i18n:
+ module_priority:
+ - admin
+ - framework
+ - sapphire
+---
+Name: defaulti18n
+---
+i18n:
+ module_priority:
+ - other_modules
View
@@ -2472,20 +2472,33 @@ public static function include_by_locale($locale, $clean = false) {
$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 @@ function($a, $b) use(&$prios) {
$adapter = $translator->getAdapter();
// Load translations from modules
- foreach($modules as $module) {
+ foreach($sortedModules as $module) {
$filename = $adapter->getFilenameForLocale($locale);
$filepath = "{$module}/lang/" . $filename;

0 comments on commit efea4db

Please sign in to comment.