/**
 *	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.'
 */
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 = Core_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;
}
Exemplo n.º 2
0
 /**
  * @param Core_Module $module
  * @param string $langCode
  */
 private function loadTranslation($module, $langCode)
 {
     // we are certainly in Tracker mode, Zend is not loaded
     if (!class_exists('Zend_Loader', false)) {
         return;
     }
     $infos = $module->getInformation();
     if (!isset($infos['translationAvailable'])) {
         $infos['translationAvailable'] = false;
     }
     $translationAvailable = $infos['translationAvailable'];
     if (!$translationAvailable) {
         return;
     }
     $moduleName = $module->getClassName();
     $path = CORE_INCLUDE_PATH . '/modules/' . $moduleName . '/lang/%s.php';
     $defaultLangPath = sprintf($path, $langCode);
     $defaultEnglishLangPath = sprintf($path, 'en');
     $translations = array();
     if (file_exists($defaultLangPath)) {
         require $defaultLangPath;
     } elseif (file_exists($defaultEnglishLangPath)) {
         require $defaultEnglishLangPath;
     } else {
         throw new Exception("Language file not found for the module '{$moduleName}'.");
     }
     Core_Translate::getInstance()->mergeTranslationArray($translations);
 }