diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-29 10:03:26 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-01-05 05:24:45 +0300 |
commit | de06fd66ee7d01832ff08f13d58e21a9d7a510c7 (patch) | |
tree | 07421e1a36621c77a22d0254c2511b48d2cc0e4f /plugins/LanguagesManager | |
parent | cb68ad1ede299ce826989858171456b82a6f7ee5 (diff) |
Refactored the way translations are loaded
Translations are now lazily loaded instead of being explicitely loaded by other components (e.g. the front controller, the plugin manager, etc...). Now other parties only configure "directories" where the translator can find translations. That makes the translator decoupled from the rest of Piwik.
Diffstat (limited to 'plugins/LanguagesManager')
-rw-r--r-- | plugins/LanguagesManager/Commands/CompareKeys.php | 2 | ||||
-rw-r--r-- | plugins/LanguagesManager/LanguagesManager.php | 34 |
2 files changed, 25 insertions, 11 deletions
diff --git a/plugins/LanguagesManager/Commands/CompareKeys.php b/plugins/LanguagesManager/Commands/CompareKeys.php index df41351e64..60364ac39d 100644 --- a/plugins/LanguagesManager/Commands/CompareKeys.php +++ b/plugins/LanguagesManager/Commands/CompareKeys.php @@ -52,7 +52,7 @@ class CompareKeys extends TranslationBase /** @var Translator $translator */ $translator = StaticContainer::getContainer()->get('Piwik\Translation\Translator'); - $translator->reloadLanguage('en'); + $translator->setCurrentLanguage('en'); $availableTranslations = $translator->getAllTranslations(); $categories = array_unique(array_merge(array_keys($englishFromOTrance), array_keys($availableTranslations))); diff --git a/plugins/LanguagesManager/LanguagesManager.php b/plugins/LanguagesManager/LanguagesManager.php index 6a0fcf10e1..711de26648 100644 --- a/plugins/LanguagesManager/LanguagesManager.php +++ b/plugins/LanguagesManager/LanguagesManager.php @@ -12,10 +12,12 @@ namespace Piwik\Plugins\LanguagesManager; use Exception; use Piwik\Common; use Piwik\Config; +use Piwik\Container\StaticContainer; use Piwik\Cookie; use Piwik\Db; use Piwik\Piwik; use Piwik\Translate; +use Piwik\Translation\Translator; use Piwik\View; /** @@ -29,12 +31,12 @@ class LanguagesManager extends \Piwik\Plugin public function getListHooksRegistered() { return array( - 'AssetManager.getStylesheetFiles' => 'getStylesheetFiles', - 'AssetManager.getJavaScriptFiles' => 'getJsFiles', - 'User.getLanguage' => 'getLanguageToLoad', - 'UsersManager.deleteUser' => 'deleteUserLanguage', - 'Template.topBar' => 'addLanguagesManagerToOtherTopBar', - 'Template.jsGlobalVariables' => 'jsGlobalVariables' + 'AssetManager.getStylesheetFiles' => 'getStylesheetFiles', + 'AssetManager.getJavaScriptFiles' => 'getJsFiles', + 'Request.dispatchCoreAndPluginUpdatesScreen' => 'initLanguage', + 'UsersManager.deleteUser' => 'deleteUserLanguage', + 'Template.topBar' => 'addLanguagesManagerToOtherTopBar', + 'Template.jsGlobalVariables' => 'jsGlobalVariables' ); } @@ -89,14 +91,26 @@ class LanguagesManager extends \Piwik\Plugin return $view->render(); } - function getLanguageToLoad(&$language) + public function initLanguage() { + /** @var Translator $translator */ + $translator = StaticContainer::getContainer()->get('Piwik\Translation\Translator'); + + $language = Common::getRequestVar('language', '', 'string'); if (empty($language)) { - $language = self::getLanguageCodeForCurrentUser(); + $userLanguage = self::getLanguageCodeForCurrentUser(); + if (API::getInstance()->isLanguageAvailable($userLanguage)) { + $language = $userLanguage; + } } - if (!API::getInstance()->isLanguageAvailable($language)) { - $language = Translate::getLanguageDefault(); + if (!empty($language) && API::getInstance()->isLanguageAvailable($language)) { + $translator->setCurrentLanguage($language); } + + $locale = $translator->translate('General_Locale'); + $localeVariant = str_replace('UTF-8', 'UTF8', $locale); + setlocale(LC_ALL, $locale, $localeVariant); + setlocale(LC_CTYPE, ''); } public function deleteUserLanguage($userLogin) |