Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorBenaka Moorthi <benaka.moorthi@gmail.com>2013-09-12 21:57:50 +0400
committerBenaka Moorthi <benaka.moorthi@gmail.com>2013-09-12 22:10:16 +0400
commit3a2c5f41a41eb09de1a4595968e174cfdff56208 (patch)
treee9ce8fd7c1e0ec2f1cf447d8bbf1b9cd1e1911cb /core
parenta5ab907fe3d09ad1aacd1a0718566aad9768889b (diff)
Refs #4151, refactor translation JavaScript generation code and add event so plugins can specify which strings should be available client-side.
Diffstat (limited to 'core')
-rw-r--r--core/Translate.php66
1 files changed, 47 insertions, 19 deletions
diff --git a/core/Translate.php b/core/Translate.php
index bd537cc569..b38d40e2ac 100644
--- a/core/Translate.php
+++ b/core/Translate.php
@@ -18,6 +18,25 @@ use Piwik\Common;
*/
class Translate
{
+ /**
+ * This event is called before generating the JavaScript code that allows other JavaScript
+ * to access Piwik i18n strings. Plugins should handle this event to specify which translations
+ * should be available to JavaScript code.
+ *
+ * Event handlers should add whole translation keys, ie, keys that include the plugin name.
+ * For example:
+ *
+ * <code>
+ * public function getClientSideTranslationKeys(&$result)
+ * {
+ * $result[] = "MyPlugin_MyTranslation";
+ * }
+ * </code>
+ *
+ * Callback Signature: function (array &$result)
+ */
+ const GET_CLIENT_SIDE_TRANSLATION_KEYS_EVENT = 'Translate.getClientSideTranslationKeys';
+
static private $instance = null;
static private $languageToLoad = null;
private $loadedLanguage = false;
@@ -144,37 +163,47 @@ class Translate
*/
public function getJavascriptTranslations()
{
- if (!in_array('General', $moduleList)) {
- $moduleList[] = 'General';
+ $translations = $GLOBALS['Piwik_translations'];
+
+ $clientSideTranslations = array();
+ foreach ($this->getClientSideTranslationKeys() as $key) {
+ list($plugin, $stringName) = explode("_", $key, 2);
+ $clientSideTranslations[$key] = $translations[$plugin][$stringName];
}
- $js = 'var translations = {';
+ $js = 'var translations = ' . Common::json_encode($clientSideTranslations) . ';';
+ $js .= "\n" . 'if(typeof(piwik_translations) == \'undefined\') { var piwik_translations = new Object; }' .
+ 'for(var i in translations) { piwik_translations[i] = translations[i];} ';
+ $js .= 'function _pk_translate(translationStringId) { ' .
+ 'if( typeof(piwik_translations[translationStringId]) != \'undefined\' ){ return piwik_translations[translationStringId]; }' .
+ 'return "The string "+translationStringId+" was not loaded in javascript. Make sure it is suffixed with _js.";}';
+ return $js;
+ }
+ /**
+ * Returns the list of client side translations by key. These translations will be outputted
+ * to the translation JavaScript.
+ */
+ private function getClientSideTranslationKeys()
+ {
$moduleRegex = '#^.*_js$#i';
// Hack: common translations used in JS but not only, force them to be defined in JS
+ $result = array('General_Save', 'General_OrCancel');
+
+ Piwik_PostEvent(self::GET_CLIENT_SIDE_TRANSLATION_KEYS_EVENT, array(&$result));
+
$translations = $GLOBALS['Piwik_translations'];
- $toSetInJs = array('General_Save', 'General_OrCancel');
- foreach ($toSetInJs as $toSetId) {
- list($plugin, $key) = explode("_", $toSetId, 2);
- $translations[$plugin][$key . '_js'] = $translations[$plugin][$key];
- }
foreach ($translations as $module => $keys) {
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));
+ $result[] = $module . '_' . $key;
}
}
}
- $js = substr($js, 0, -1);
- $js .= '};';
- $js .= "\n" . 'if(typeof(piwik_translations) == \'undefined\') { var piwik_translations = new Object; }' .
- 'for(var i in translations) { piwik_translations[i] = translations[i];} ';
- $js .= 'function _pk_translate(translationStringId) { ' .
- 'if( typeof(piwik_translations[translationStringId]) != \'undefined\' ){ return piwik_translations[translationStringId]; }' .
- 'return "The string "+translationStringId+" was not loaded in javascript. Make sure it is suffixed with _js.";}';
- return $js;
+
+ return $result;
}
/**
@@ -189,5 +218,4 @@ class Translate
setlocale(LC_ALL, $locale, $locale_variant);
setlocale(LC_CTYPE, '');
}
-}
-
+} \ No newline at end of file