From 058c83e3814860b48e63a1fe3d6ea972897c118f Mon Sep 17 00:00:00 2001 From: Fabian Becker <fabian.becker@uni-tuebingen.de> Date: Fri, 16 Aug 2013 13:33:05 +0200 Subject: [PATCH] Adjust Piwik_Translate function to handle nested translation array Adjust Translate class to properly load .json translation into array Adjust JavaScript translation filter Adjust plugins manager to load plugin translations properly refs #4086 --- core/PluginsManager.php | 14 ++++++++++---- core/Translate.php | 35 +++++++++++++++++++---------------- core/functions.php | 8 ++++++-- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/core/PluginsManager.php b/core/PluginsManager.php index 51a22ba8df..32c469c744 100644 --- a/core/PluginsManager.php +++ b/core/PluginsManager.php @@ -617,7 +617,7 @@ class PluginsManager $pluginName = $plugin->getPluginName(); - $path = self::getPluginsDirectory() . $pluginName . '/lang/%s.php'; + $path = self::getPluginsDirectory() . $pluginName . '/lang/%s.json'; $defaultLangPath = sprintf($path, $langCode); $defaultEnglishLangPath = sprintf($path, 'en'); @@ -625,13 +625,19 @@ class PluginsManager $translations = array(); if (file_exists($defaultLangPath)) { - require $defaultLangPath; + $data = file_get_contents($defaultLangPath); + $translations = json_decode($data, true); } elseif (file_exists($defaultEnglishLangPath)) { - require $defaultEnglishLangPath; + $data = file_get_contents($defaultEnglishLangPath); + $translations = json_decode($data, true); } else { return false; } - Translate::getInstance()->mergeTranslationArray($translations); + + if(isset($translations[$pluginName])) { + // only merge translations of plugin - prevents overwritten strings + Translate::getInstance()->mergeTranslationArray(array($pluginName => $translations[$pluginName])); + } return true; } diff --git a/core/Translate.php b/core/Translate.php index 6ff5b3d6d8..045774b855 100644 --- a/core/Translate.php +++ b/core/Translate.php @@ -73,12 +73,12 @@ class Translate private function loadCoreTranslationFile($language) { - $translations = array(); - $path = PIWIK_INCLUDE_PATH . '/lang/' . $language . '.php'; + $path = PIWIK_INCLUDE_PATH . '/lang/' . $language . '.json'; if (!Common::isValidFilename($language) || !is_readable($path)) { throw new Exception(Piwik_TranslateException('General_ExceptionLanguageFileNotFound', array($language))); } - require $path; + $data = file_get_contents($path); + $translations = json_decode($data, true); $this->mergeTranslationArray($translations); $this->setLocale(); $this->loadedLanguage = $language; @@ -90,7 +90,7 @@ class Translate $GLOBALS['Piwik_translations'] = array(); } // we could check that no string overlap here - $GLOBALS['Piwik_translations'] = array_merge($GLOBALS['Piwik_translations'], array_filter($translation, 'strlen')); + $GLOBALS['Piwik_translations'] = array_merge_recursive($GLOBALS['Piwik_translations'], $translation); } /** @@ -140,22 +140,25 @@ class Translate $js = 'var translations = {'; - $moduleRegex = '#^('; - foreach ($moduleList as $module) { - $moduleRegex .= $module . '|'; - } - $moduleRegex = substr($moduleRegex, 0, -1); - $moduleRegex .= ')_.*_js$#i'; + $moduleRegex = '#^.*_js$#i'; - // Hack: common translations used in JS but not only, force as them to be defined in JS + // Hack: common translations used in JS but not only, force them to be defined in JS $translations = $GLOBALS['Piwik_translations']; $toSetInJs = array('General_Save', 'General_OrCancel'); foreach ($toSetInJs as $toSetId) { - $translations[$toSetId . '_js'] = $translations[$toSetId]; + list($plugin, $key) = explode("_", $toSetId, 2); + $translations[$plugin][$key . '_js'] = $translations[$plugin][$key]; } - foreach ($translations as $key => $value) { - if (preg_match($moduleRegex, $key)) { - $js .= '"' . $key . '": "' . str_replace('"', '\"', $value) . '",'; + foreach ($translations as $module => $keys) { + // Skip modules + if(!in_array($module, $moduleList)) { + continue; + } + foreach($keys as $key => $value) { + // Find keys ending with _js + if (preg_match($moduleRegex, $key)) { + $js .= sprintf('"%s_%s": "%s",', $module, $key, str_replace('"', '\"', $value)); + } } } $js = substr($js, 0, -1); @@ -175,7 +178,7 @@ class Translate */ private function setLocale() { - $locale = $GLOBALS['Piwik_translations']['General_Locale']; + $locale = $GLOBALS['Piwik_translations']['General']['Locale']; $locale_variant = str_replace('UTF-8', 'UTF8', $locale); setlocale(LC_ALL, $locale, $locale_variant); setlocale(LC_CTYPE, ''); diff --git a/core/functions.php b/core/functions.php index 98c27e322a..c706a17b6a 100644 --- a/core/functions.php +++ b/core/functions.php @@ -66,8 +66,12 @@ namespace { if (!is_array($args)) { $args = array($args); } - if (isset($GLOBALS['Piwik_translations'][$string])) { - $string = $GLOBALS['Piwik_translations'][$string]; + + if(strpos($string, "_") !== FALSE) { + list($plugin, $key) = explode("_", $string, 2); + if (isset($GLOBALS['Piwik_translations'][$plugin]) && isset($GLOBALS['Piwik_translations'][$plugin][$key])) { + $string = $GLOBALS['Piwik_translations'][$plugin][$key]; + } } if (count($args) == 0) { return $string; -- GitLab