mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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.
This commit is contained in:
parent
fe2663a140
commit
efea4dbe94
15
_config/i18n.yml
Normal file
15
_config/i18n.yml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
---
|
||||||
|
Name: basei18n
|
||||||
|
Before: '/i18n'
|
||||||
|
---
|
||||||
|
i18n:
|
||||||
|
module_priority:
|
||||||
|
- admin
|
||||||
|
- framework
|
||||||
|
- sapphire
|
||||||
|
---
|
||||||
|
Name: defaulti18n
|
||||||
|
---
|
||||||
|
i18n:
|
||||||
|
module_priority:
|
||||||
|
- other_modules
|
@ -2472,20 +2472,33 @@ class i18n extends Object implements TemplateGlobalProvider {
|
|||||||
$cache = Zend_Translate::getCache();
|
$cache = Zend_Translate::getCache();
|
||||||
if($cache) $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
|
if($cache) $cache->clean(Zend_Cache::CLEANING_MODE_ALL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort modules by inclusion priority, then alphabetically
|
// Get list of module => path pairs, and then just the names
|
||||||
// TODO Should be handled by priority flags within modules
|
|
||||||
$prios = array('sapphire' => 10, 'framework' => 10, 'admin' => 11, 'cms' => 12, project() => 90);
|
|
||||||
$modules = SS_ClassLoader::instance()->getManifest()->getModules();
|
$modules = SS_ClassLoader::instance()->getManifest()->getModules();
|
||||||
ksort($modules);
|
$moduleNames = array_keys($modules);
|
||||||
uksort(
|
|
||||||
$modules,
|
// Remove the "project" module from the list - we'll add it back specially later if needed
|
||||||
function($a, $b) use(&$prios) {
|
global $project;
|
||||||
$prioA = (isset($prios[$a])) ? $prios[$a] : 50;
|
if (($idx = array_search($project, $moduleNames)) !== false) array_splice($moduleNames, $idx, 1);
|
||||||
$prioB = (isset($prios[$b])) ? $prios[$b] : 50;
|
|
||||||
return ($prioA > $prioB);
|
// 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
|
// Loop in reverse order, meaning the translator with the highest priority goes first
|
||||||
$translators = array_reverse(self::get_translators(), true);
|
$translators = array_reverse(self::get_translators(), true);
|
||||||
@ -2494,7 +2507,7 @@ class i18n extends Object implements TemplateGlobalProvider {
|
|||||||
$adapter = $translator->getAdapter();
|
$adapter = $translator->getAdapter();
|
||||||
|
|
||||||
// Load translations from modules
|
// Load translations from modules
|
||||||
foreach($modules as $module) {
|
foreach($sortedModules as $module) {
|
||||||
$filename = $adapter->getFilenameForLocale($locale);
|
$filename = $adapter->getFilenameForLocale($locale);
|
||||||
$filepath = "{$module}/lang/" . $filename;
|
$filepath = "{$module}/lang/" . $filename;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user