blob: 8ca182854342b9b6a532c60af86f6b5b5a707059 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik
* @package SmartyPlugins
*/
/**
* Load translation strings suffixed with _js for a given list of modules.
* This function needs to be called when you want to i18n the user interface.
*
* How to use the function in smarty templates:
* {loadJavascriptTranslations plugins='SitesManager CoreHome General'}
*
* This will load the javascript translations array for the modules specified as parameters.
* Only translations string with their ids suffixed with '_js' will be loaded
* Note: You can specify disableOutputScriptTag=1 and the returned value won't be enclosed in Javascript tags.
*
* You can then translate strings in javascript by calling the javascript function:
* _pk_translate('MY_TRANSLATION_STRING_js')
*
* _pk_translate does NOT support printf() arguments, but you can call:
* sprintf(_pk_translate('MyPlugin_numberOfEggs_js'),'ten')
* where you would have the following in your translation file plugins/MyPlugin/lang/en.php:
* 'MyPlugin_numberOfEggs_js' => 'There are %s eggs.'
*
* @param array $params
* @param $smarty
* @throws Exception
* @return string
*/
function smarty_function_loadJavascriptTranslations($params, &$smarty)
{
static $pluginTranslationsAlreadyLoaded = array();
if(!isset($params['plugins']))
{
throw new Exception("The smarty function loadJavascriptTranslations needs a 'plugins' parameter.");
}
if(in_array($params['plugins'], $pluginTranslationsAlreadyLoaded))
{
return;
}
$pluginTranslationsAlreadyLoaded[] = $params['plugins'];
$jsTranslations = Piwik_Translate::getInstance()->getJavascriptTranslations(explode(' ',$params['plugins']));
$jsCode = '';
if( isset($params['disableOutputScriptTag']) )
{
$jsCode .= $jsTranslations;
}
else
{
$jsCode .= '<script type="text/javascript">';
$jsCode .= $jsTranslations;
$jsCode .= '</script>';
}
return $jsCode;
}
|