diff options
author | Benaka Moorthi <benaka.moorthi@gmail.com> | 2013-09-12 21:38:03 +0400 |
---|---|---|
committer | Benaka Moorthi <benaka.moorthi@gmail.com> | 2013-09-12 22:10:16 +0400 |
commit | a5ab907fe3d09ad1aacd1a0718566aad9768889b (patch) | |
tree | 6bd50b5c3583b912671f2220f96d96666115ddd3 | |
parent | 9cd4cd3dd0a68e43fc9e6b9a9826c9a09fde5114 (diff) |
Refs #4151, remove loadJavascriptTranslations twig function and automatically generate translation JS for all plugins. Translation JS is now treated as an asset and is included in merged JS.
Note:
- Includes tweaks to capture.js: on webpage error stop capture program.
24 files changed, 49 insertions, 60 deletions
diff --git a/core/AssetManager.php b/core/AssetManager.php index 682ae0a09a..449d992226 100644 --- a/core/AssetManager.php +++ b/core/AssetManager.php @@ -17,6 +17,7 @@ use Piwik\Piwik; use Piwik\Common; use Piwik\Version; use Piwik\PluginsManager; +use Piwik\Translate; use lessc; /** @@ -46,6 +47,7 @@ class AssetManager { const MERGED_CSS_FILE = "asset_manager_global_css.css"; const MERGED_JS_FILE = "asset_manager_global_js.js"; + const TRANSLATIONS_JS_FILE = "asset_manager_translations_js.js"; const CSS_IMPORT_EVENT = "AssetManager.getStylesheetFiles"; const JS_IMPORT_EVENT = "AssetManager.getJsFiles"; const MERGED_FILE_DIR = "tmp/assets/"; @@ -311,7 +313,9 @@ class AssetManager */ private static function generateMergedJsFile() { - $mergedContent = ""; + // initialize the merged content to the translations JavaScript file + $translationsFile = self::getTranslationsJsFileLocation(); + $mergedContent = PHP_EOL . JSMin::minify(file_get_contents($translationsFile)); // Loop through each js file $files = self::getJsFiles(); @@ -342,8 +346,12 @@ class AssetManager */ private static function getIndividualJsIncludes() { - $jsFiles = self::getJsFiles(); $jsIncludeString = ''; + + // add translations include + $jsIncludeString .= sprintf(self::JS_IMPORT_DIRECTIVE, "index.php?module=Proxy&action=getTranslationJs"); + + $jsFiles = self::getJsFiles(); foreach ($jsFiles as $jsFile) { self::validateJsFile($jsFile); $jsIncludeString = $jsIncludeString . sprintf(self::JS_IMPORT_DIRECTIVE, $jsFile); @@ -441,6 +449,24 @@ class AssetManager } /** + * Returns the translations JS file's absolute location. + * If the file does not exist, it is generated. + * + * @return string + */ + public static function getTranslationsJsFileLocation() + { + $isGenerated = self::isGenerated(self::TRANSLATIONS_JS_FILE); + + if (!$isGenerated) { + $translationJs = Translate::getInstance()->getJavascriptTranslations(); + self::writeAssetToFile($translationJs, self::TRANSLATIONS_JS_FILE); + } + + return self::getAbsoluteMergedFileLocation(self::TRANSLATIONS_JS_FILE); + } + + /** * Check if the provided merged file is generated * * @param string $filename filename of the merged asset diff --git a/core/Translate.php b/core/Translate.php index 09d9734da6..bd537cc569 100644 --- a/core/Translate.php +++ b/core/Translate.php @@ -141,9 +141,8 @@ class Translate * Generate javascript translations array * * @param array $moduleList - * @return string containing javascript code with translations array (including <script> tag) */ - public function getJavascriptTranslations(array $moduleList) + public function getJavascriptTranslations() { if (!in_array('General', $moduleList)) { $moduleList[] = 'General'; @@ -161,10 +160,6 @@ class Translate $translations[$plugin][$key . '_js'] = $translations[$plugin][$key]; } 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)) { @@ -178,7 +173,7 @@ class Translate '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 and that you called %7BloadJavascriptTranslations plugins=\'\$YOUR_PLUGIN_NAME\'%7D before your javascript code.";}'; + 'return "The string "+translationStringId+" was not loaded in javascript. Make sure it is suffixed with _js.";}'; return $js; } diff --git a/core/Twig.php b/core/Twig.php index 5f6550512e..09dbe27940 100644 --- a/core/Twig.php +++ b/core/Twig.php @@ -71,7 +71,6 @@ class Twig $this->addFunction_includeAssets(); $this->addFunction_linkTo(); - $this->addFunction_loadJavascriptTranslations(); $this->addFunction_sparkline(); $this->addFunction_postEvent(); $this->addFunction_isPluginLoaded(); @@ -125,28 +124,6 @@ class Twig $this->twig->addFunction($sparklineFunction); } - protected function addFunction_loadJavascriptTranslations() - { - $loadJsTranslationsFunction = new Twig_SimpleFunction('loadJavascriptTranslations', function (array $plugins, $disableScriptTag = false) { - static $pluginTranslationsAlreadyLoaded = array(); - if (in_array($plugins, $pluginTranslationsAlreadyLoaded)) { - return; - } - $pluginTranslationsAlreadyLoaded[] = $plugins; - $jsTranslations = Translate::getInstance()->getJavascriptTranslations($plugins); - $jsCode = ''; - if ($disableScriptTag) { - $jsCode .= $jsTranslations; - } else { - $jsCode .= '<script type="text/javascript">'; - $jsCode .= $jsTranslations; - $jsCode .= '</script>'; - } - return $jsCode; - }, array('is_safe' => array('html'))); - $this->twig->addFunction($loadJsTranslationsFunction); - } - protected function addFunction_linkTo() { $urlFunction = new Twig_SimpleFunction('linkTo', function ($params) { diff --git a/plugins/CoreAdminHome/templates/generalSettings.twig b/plugins/CoreAdminHome/templates/generalSettings.twig index 506dea09ce..51e457a62f 100644 --- a/plugins/CoreAdminHome/templates/generalSettings.twig +++ b/plugins/CoreAdminHome/templates/generalSettings.twig @@ -1,7 +1,6 @@ {% extends 'admin.twig' %} {% block content %} -{{ loadJavascriptTranslations(['UsersManager']) }} {# load macros #} {% import 'macros.twig' as piwik %} {% import 'ajaxMacros.twig' as ajax %} diff --git a/plugins/CoreHome/templates/_periodSelect.twig b/plugins/CoreHome/templates/_periodSelect.twig index 3c38383092..3df678d645 100644 --- a/plugins/CoreHome/templates/_periodSelect.twig +++ b/plugins/CoreHome/templates/_periodSelect.twig @@ -1,5 +1,3 @@ -{{ loadJavascriptTranslations(['CoreHome'])|raw }} - <div id="periodString"> <div id="date">{{ 'General_DateRange'|translate }} <strong>{{ prettyDate }}</strong></div> <div class="calendar-icon"></div> diff --git a/plugins/CoreUpdater/templates/layout.twig b/plugins/CoreUpdater/templates/layout.twig index b289ce1292..95c2c5707c 100644 --- a/plugins/CoreUpdater/templates/layout.twig +++ b/plugins/CoreUpdater/templates/layout.twig @@ -16,10 +16,10 @@ <script type="text/javascript" src="libs/jquery/jquery-ui.js"></script> <script type="text/javascript" src="plugins/CoreHome/javascripts/donate.js"></script> <script type="text/javascript" src="plugins/CoreUpdater/javascripts/updateLayout.js"></script> + <script type="text/javascript" src="index.php?module=Proxy&action=getTranslationJs"></script> {% if 'General_LayoutDirection'|translate =='rtl' %} <link rel="stylesheet" type="text/css" href="plugins/Zeitgeist/stylesheets/rtl.css"/> {% endif %} - {{ loadJavascriptTranslations(['CoreHome'])|raw }} <link rel="shortcut icon" href="plugins/CoreHome/images/favicon.ico"/> </head> diff --git a/plugins/DBStats/templates/index.twig b/plugins/DBStats/templates/index.twig index c4c0caf655..175018677a 100755 --- a/plugins/DBStats/templates/index.twig +++ b/plugins/DBStats/templates/index.twig @@ -1,7 +1,6 @@ {% extends 'admin.twig' %} {% block content %} -{{ loadJavascriptTranslations(['CoreAdminHome','CoreHome']) }} <h2 id="databaseUsageSummary">{{ 'DBStats_DatabaseUsage'|translate }}</h2> <p> diff --git a/plugins/Dashboard/templates/_header.twig b/plugins/Dashboard/templates/_header.twig index 4a9795fc3a..864bcd5477 100644 --- a/plugins/Dashboard/templates/_header.twig +++ b/plugins/Dashboard/templates/_header.twig @@ -5,7 +5,6 @@ <meta charset="utf-8"> <title>{{ 'General_Dashboard'|translate }} - {{ 'CoreHome_WebAnalyticsReports'|translate }}</title> - {{ loadJavascriptTranslations(['CoreHome',' Dashboard'])|raw }} <!--[if lt IE 9]> <script language="javascript" type="text/javascript" src="libs/jqplot/excanvas.min.js"></script> <![endif]--> diff --git a/plugins/Dashboard/templates/embeddedIndex.twig b/plugins/Dashboard/templates/embeddedIndex.twig index a472b1b049..e509b5c4b7 100644 --- a/plugins/Dashboard/templates/embeddedIndex.twig +++ b/plugins/Dashboard/templates/embeddedIndex.twig @@ -1,5 +1,3 @@ -{{ loadJavascriptTranslations(['CoreHome','Dashboard'])|raw }} - <script type="text/javascript"> widgetsHelper.availableWidgets = {{ availableWidgets|raw }}; $(document).ready(function () { diff --git a/plugins/ExamplePlugin/Controller.php b/plugins/ExamplePlugin/Controller.php index 866428bd8a..8b1c0625ca 100644 --- a/plugins/ExamplePlugin/Controller.php +++ b/plugins/ExamplePlugin/Controller.php @@ -124,9 +124,7 @@ class Controller extends \Piwik\Controller $out .= 'In order to translate strings within Javascript code, you can use the javascript function _pk_translate( token );. <ul><li>The "token" parameter is the string unique key found in the translation file. For this token string to be available in Javascript, you must suffix your token by "_js" in the language file. For example, you can add <code>\'Goals_AddGoal_js\' => \'Add Goal\',</code> in the lang/en.php file</li> - <li>You then need to instruct Piwik to load your Javascript translations for your plugin; by default, all translation strings are not loaded in Javascript for performance reasons. This can be done by calling a custom-made Twig modifier before the Javascript code requiring translations, eg. - <code>{loadJavascriptTranslations plugins=\'$YOUR_PLUGIN_NAME\'}</code>. In our previous example, the $YOUR_PLUGIN_NAME being Goals, we would write <code>{loadJavascriptTranslations plugins=\'Goals\'}</code> - </li><li>You can then print this string from your JS code by doing <code>_pk_translate(\'Goals_AddGoal_js\');</code>. + <li>You can then print this string from your JS code by doing <code>_pk_translate(\'Goals_AddGoal_js\');</code>. </li></ul>'; $out .= '<h3>Reload a widget in the dashboard</h3>'; diff --git a/plugins/Goals/templates/_addEditGoal.twig b/plugins/Goals/templates/_addEditGoal.twig index 759905538e..41f094163f 100644 --- a/plugins/Goals/templates/_addEditGoal.twig +++ b/plugins/Goals/templates/_addEditGoal.twig @@ -50,7 +50,6 @@ <a id='bottom'></a> </div> <br/><br/> -{{ loadJavascriptTranslations(['Goals']) }} <script type="text/javascript"> var mappingMatchTypeName = { "url": "{{ 'Goals_URL'|translate }}", diff --git a/plugins/Live/templates/getVisitorProfilePopup.twig b/plugins/Live/templates/getVisitorProfilePopup.twig index bba6323006..5d88280ede 100644 --- a/plugins/Live/templates/getVisitorProfilePopup.twig +++ b/plugins/Live/templates/getVisitorProfilePopup.twig @@ -1,4 +1,3 @@ -{{ loadJavascriptTranslations(['Live']) }} <div class="visitor-profile" data-visitor-id="{{ visitorData.lastVisits.getFirstRow().getColumn('visitorId') }}" data-next-visitor="{{ visitorData.nextVisitorId }}" diff --git a/plugins/MobileMessaging/templates/index.twig b/plugins/MobileMessaging/templates/index.twig index 64a8abfdf9..7cf2d023a6 100644 --- a/plugins/MobileMessaging/templates/index.twig +++ b/plugins/MobileMessaging/templates/index.twig @@ -1,8 +1,6 @@ {% extends 'admin.twig' %} {% block content %} - {{ loadJavascriptTranslations(['MobileMessaging'])|raw }} - {% if accountManagedByCurrentUser %} <h2>{{ 'MobileMessaging_Settings_SMSAPIAccount'|translate }}</h2> {% if credentialSupplied %} diff --git a/plugins/Proxy/Controller.php b/plugins/Proxy/Controller.php index d358908805..3283e08909 100644 --- a/plugins/Proxy/Controller.php +++ b/plugins/Proxy/Controller.php @@ -23,6 +23,7 @@ use Piwik\Url; class Controller extends \Piwik\Controller { const TRANSPARENT_PNG_PIXEL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='; + const JS_MIME_TYPE = "application/javascript; charset=UTF-8"; /** * Output the merged CSS file. @@ -45,7 +46,20 @@ class Controller extends \Piwik\Controller public function getJs() { $jsMergedFile = AssetManager::getMergedJsFileLocation(); - Piwik::serveStaticFile($jsMergedFile, "application/javascript; charset=UTF-8"); + Piwik::serveStaticFile($jsMergedFile, self::JS_MIME_TYPE); + } + + /** + * Output the translations JavaScript file. + * This method is called when the asset manager is disabled (otherwise it would be part + * of the merged JS file.) + * + * @see core/AssetManager.php + */ + public function getTranslationJs() + { + $translationsJsFile = AssetManager::getTranslationsJsFileLocation(); + Piwik::serveStaticFile($translationsJsFile, self::JS_MIME_TYPE); } /** diff --git a/plugins/SitesManager/templates/displayJavascriptCode.twig b/plugins/SitesManager/templates/displayJavascriptCode.twig index 34ef90e10e..0a35d298cf 100644 --- a/plugins/SitesManager/templates/displayJavascriptCode.twig +++ b/plugins/SitesManager/templates/displayJavascriptCode.twig @@ -1,7 +1,6 @@ {% extends 'admin.twig' %} {% block content %} -{{ loadJavascriptTranslations(['SitesManager']) }} {% raw %} {% include "@SitesManager/_displayJavascriptCode.twig" %} diff --git a/plugins/SitesManager/templates/index.twig b/plugins/SitesManager/templates/index.twig index 902dc03e25..b0fc0decfa 100644 --- a/plugins/SitesManager/templates/index.twig +++ b/plugins/SitesManager/templates/index.twig @@ -3,7 +3,6 @@ {% block content %} {% import 'macros.twig' as piwik %} {% import 'ajaxMacros.twig' as ajax %} - {{ loadJavascriptTranslations(['SitesManager']) }} {% set excludedIpHelpPlain %} {{ 'SitesManager_HelpExcludedIps'|translate("1.2.3.*","1.2.*.*") }} diff --git a/plugins/UserCountry/templates/adminIndex.twig b/plugins/UserCountry/templates/adminIndex.twig index c06bd75619..5e872592f1 100755 --- a/plugins/UserCountry/templates/adminIndex.twig +++ b/plugins/UserCountry/templates/adminIndex.twig @@ -2,7 +2,6 @@ {% block content %} {% import 'macros.twig' as piwik %} -{{ loadJavascriptTranslations(['UserCountry'])|raw }} <h2 id="location-providers">{{ 'UserCountry_Geolocation'|translate }}</h2> diff --git a/plugins/UsersManager/templates/index.twig b/plugins/UsersManager/templates/index.twig index 4a299430ce..1dc7245da2 100644 --- a/plugins/UsersManager/templates/index.twig +++ b/plugins/UsersManager/templates/index.twig @@ -1,7 +1,6 @@ {% extends 'admin.twig' %} {% block content %} -{{ loadJavascriptTranslations(['UsersManager'])|raw }} <h2>{{ 'UsersManager_ManageAccess'|translate }}</h2> <div id="sites"> diff --git a/plugins/UsersManager/templates/userSettings.twig b/plugins/UsersManager/templates/userSettings.twig index 54d6f52020..9f90bcedf7 100644 --- a/plugins/UsersManager/templates/userSettings.twig +++ b/plugins/UsersManager/templates/userSettings.twig @@ -1,7 +1,6 @@ {% extends 'admin.twig' %} {% block content %} -{{ loadJavascriptTranslations(['UsersManager'])|raw }} <h2>{{ 'UsersManager_MenuUserSettings'|translate }}</h2> <br/> diff --git a/plugins/Widgetize/templates/iframe.twig b/plugins/Widgetize/templates/iframe.twig index 9edea914d7..a329ca7691 100644 --- a/plugins/Widgetize/templates/iframe.twig +++ b/plugins/Widgetize/templates/iframe.twig @@ -2,7 +2,6 @@ <html> <head> <meta charset="utf-8"> - {{ loadJavascriptTranslations(['CoreHome']) }} {% include "_jsGlobalVariables.twig" %} <!--[if lt IE 9]> <script language="javascript" type="text/javascript" src="libs/jqplot/excanvas.min.js"></script> diff --git a/plugins/Widgetize/templates/index.twig b/plugins/Widgetize/templates/index.twig index 03f548ccc6..7c25eaccfe 100644 --- a/plugins/Widgetize/templates/index.twig +++ b/plugins/Widgetize/templates/index.twig @@ -2,8 +2,6 @@ {% block content %} -{{ loadJavascriptTranslations(['Dashboard']) }} - <script type="text/javascript"> $(function () { var widgetized = new widgetize(); diff --git a/plugins/Zeitgeist/templates/admin.twig b/plugins/Zeitgeist/templates/admin.twig index fd4c1c5e5f..515c10e6a3 100644 --- a/plugins/Zeitgeist/templates/admin.twig +++ b/plugins/Zeitgeist/templates/admin.twig @@ -10,8 +10,6 @@ <meta name="generator" content="Piwik - Open Source Web Analytics"/> <link rel="shortcut icon" href="plugins/CoreHome/images/favicon.ico"/> - {{ loadJavascriptTranslations(['CoreAdminHome','CoreHome'])|raw }} - {% include "_jsGlobalVariables.twig" %} {% include "_jsCssIncludes.twig" %} <!--[if IE]> diff --git a/plugins/Zeitgeist/templates/dashboard.twig b/plugins/Zeitgeist/templates/dashboard.twig index aaa516cda4..b90a53200b 100644 --- a/plugins/Zeitgeist/templates/dashboard.twig +++ b/plugins/Zeitgeist/templates/dashboard.twig @@ -12,7 +12,6 @@ <meta name="generator" content="Piwik - Open Source Web Analytics"/> <meta name="description" content="Web Analytics report for '{{ siteName }}' - Piwik"/> <link rel="shortcut icon" href="plugins/CoreHome/images/favicon.ico"/> - {{ loadJavascriptTranslations(['CoreHome','Annotations'])|raw }} {% include "_jsGlobalVariables.twig" %} <!--[if lt IE 9]> <script language="javascript" type="text/javascript" src="libs/jqplot/excanvas.min.js"></script> diff --git a/tests/resources/screenshot-capture/capture.js b/tests/resources/screenshot-capture/capture.js index 9a0d0a4775..05738c180e 100644 --- a/tests/resources/screenshot-capture/capture.js +++ b/tests/resources/screenshot-capture/capture.js @@ -62,11 +62,12 @@ PageRenderer.prototype = { }, _setupWebpageEvents: function () { + var self = this; this.webpage.onError = function (message) { console.log("Webpage error: " + message); + app.exit(1); }; - var self = this; this.webpage.onConsoleMessage = function (message) { if (message == "__AJAX_DONE__") { try { |