got clientside translations working for etemplate2 apps: egw.lang("%1 timesheet(s) %2", 5, egw.lang("deleted"))

This commit is contained in:
Ralf Becker 2011-08-23 16:15:54 +00:00
parent 934e33fdd7
commit 16c6a8d4f9
4 changed files with 51 additions and 18 deletions

View File

@ -768,9 +768,10 @@ abstract class egw_framework
$java_script .= self::get_script_links(); $java_script .= self::get_script_links();
// set webserver_url for json // set webserver_url for json
$java_script .= "<script type=\"text/javascript\">\nwindow.egw_webserverUrl = '". $java_script .= "<script type=\"text/javascript\">\nwindow.egw_webserverUrl = egw.webserverUrl = '".
($GLOBALS['egw_info']['server']['enforce_ssl'] && substr($GLOBALS['egw_info']['server']['webserver_url'],0,8) != 'https://' ? 'https://'.$_SERVER['HTTP_HOST'] : ''). ($GLOBALS['egw_info']['server']['enforce_ssl'] && substr($GLOBALS['egw_info']['server']['webserver_url'],0,8) != 'https://' ? 'https://'.$_SERVER['HTTP_HOST'] : '').
$GLOBALS['egw_info']['server']['webserver_url']."';\n"; $GLOBALS['egw_info']['server']['webserver_url']."';\n";
$java_script .= 'window.egw_appName = "'.$GLOBALS['egw_info']['flags']['currentapp'].'";'."\n";
// add link registry to non-popup windows, if explicit requested (idots_framework::navbar() loads it, if not explicit specified!) // add link registry to non-popup windows, if explicit requested (idots_framework::navbar() loads it, if not explicit specified!)
if ($GLOBALS['egw_info']['flags']['js_link_registry']) if ($GLOBALS['egw_info']['flags']['js_link_registry'])

View File

@ -16,9 +16,9 @@ var egw;
/** /**
* Central object providing all kinds of api services on clientside: * Central object providing all kinds of api services on clientside:
* - preferences * - preferences: egw.preferences("dateformat")
* - configuration * - translation: egw.lang("%1 entries deleted", 5)
* - link registry * - link registry: egw.open(123, "infolog")
*/ */
if (window.opener && typeof window.opener.egw == 'object') if (window.opener && typeof window.opener.egw == 'object')
{ {
@ -33,6 +33,8 @@ else
egw = { egw = {
/** /**
* Object holding the prefences as 2-dim. associative array, use egw.preference(name[,app]) to access it * Object holding the prefences as 2-dim. associative array, use egw.preference(name[,app]) to access it
*
* @access: private, use egw.preferences() or egw.set_perferences()
*/ */
prefs: { prefs: {
common: { common: {
@ -41,6 +43,13 @@ else
lang: "en" lang: "en"
} }
}, },
/**
* base-URL of the EGroupware installation
*
* get set via egw_framework::header()
*/
webserverUrl: "/egroupware",
/** /**
* Setting prefs for an app or 'common' * Setting prefs for an app or 'common'
@ -69,7 +78,7 @@ else
*/ */
preference: function(_name, _app) preference: function(_name, _app)
{ {
if (typeof app == 'undefined') _app = 'common'; if (typeof _app == 'undefined') _app = 'common';
if (typeof this.prefs[_app] == 'undefined') if (typeof this.prefs[_app] == 'undefined')
{ {
@ -80,6 +89,8 @@ else
/** /**
* Translations * Translations
*
* @access: private, use egw.lang() or egw.set_lang_arr()
*/ */
lang_arr: {}, lang_arr: {},
@ -102,7 +113,31 @@ else
*/ */
lang: function(_msg, _arg1) lang: function(_msg, _arg1)
{ {
return _msg; var translation = _msg;
_msg = _msg.toLowerCase();
// search apps in given order for a replacement
var apps = [window.egw_appName, 'etemplate', 'common'];
for(var i = 0; i < apps.length; ++i)
{
if (typeof this.lang_arr[apps[i]][_msg] != 'undefined')
{
translation = this.lang_arr[apps[i]][_msg];
break;
}
}
if (arguments.length == 1) return translation;
if (arguments.length == 2) return translation.replace('%1', arguments[1]);
// to cope with arguments containing '%2' (eg. an urlencoded path like a referer),
// we first replace all placeholders '%N' with '|%N|' and then we replace all '|%N|' with arguments[N]
translation = translation.replace(/%([0-9]+)/g, '|%$1|');
for(var i = 1; i < arguments.length; ++i)
{
translation = translation.replace('|%'+i+'|', arguments[i]);
}
return translation;
}, },
/** /**
@ -146,7 +181,7 @@ else
alert('egw.open() type "'+type+'" is NOT defined in link registry for app "'+app+'"!'); alert('egw.open() type "'+type+'" is NOT defined in link registry for app "'+app+'"!');
return; return;
} }
var url = egw_webserverUrl+'/index.php'; var url = this.webserverUrl+'/index.php';
var delimiter = '?'; var delimiter = '?';
var params = app_registry[type]; var params = app_registry[type];
if (type == 'view' || type == 'edit') // add id parameter for type view or edit if (type == 'view' || type == 'edit') // add id parameter for type view or edit
@ -196,6 +231,8 @@ else
/** /**
* Link registry * Link registry
*
* @access: private, use egw.open() or egw.set_link_registry()
*/ */
link_registry: null, link_registry: null,

View File

@ -22,6 +22,10 @@ $GLOBALS['egw_info'] = array(
include '../header.inc.php'; include '../header.inc.php';
// just to be sure, noone tries something nasty ...
if (!preg_match('/^[a-z0-9_]+$/i', $_GET['app'])) die('No valid application-name given!');
if (!preg_match('/^[a-z]{2}(-[a-z]{2})?$/i', $_GET['lang'])) die('No valid lang-name given!');
// use an etag with app, lang and a hash over the creation-times of all lang-files // use an etag with app, lang and a hash over the creation-times of all lang-files
$etag = '"'.$_GET['app'].'-'.$_GET['lang'].'-'.md5($GLOBALS['egw_info']['server']['lang_ctimes']).'"'; $etag = '"'.$_GET['app'].'-'.$_GET['lang'].'-'.md5($GLOBALS['egw_info']['server']['lang_ctimes']).'"';
@ -44,10 +48,7 @@ if (!count(translation::$lang_arr))
translation::add_app($_GET['app'], 'en'); translation::add_app($_GET['app'], 'en');
} }
echo 'egw.set_lang_arr("'.$_GET['app'].'", '.json_encode(translation::$lang_arr).');';
echo '
if (typeof window.egw_lang == "undefined") window.egw_lang = {};
$j.extend(window.egw_lang, '.json_encode(translation::$lang_arr).');';
// Content-Lenght header is important, otherwise browsers dont cache! // Content-Lenght header is important, otherwise browsers dont cache!
Header('Content-Length: '.ob_get_length()); Header('Content-Length: '.ob_get_length());

View File

@ -87,7 +87,7 @@ class idots_framework extends egw_framework
// make sure header is output only once // make sure header is output only once
if (self::$header_done) return ''; if (self::$header_done) return '';
self::$header_done = true; self::$header_done = true;
error_log(__METHOD__."() this->tpl=".array2string($this->tpl).' '.function_backtrace());
// add a content-type header to overwrite an existing default charset in apache (AddDefaultCharset directiv) // add a content-type header to overwrite an existing default charset in apache (AddDefaultCharset directiv)
header('Content-type: text/html; charset='.translation::charset()); header('Content-type: text/html; charset='.translation::charset());
@ -174,12 +174,6 @@ egw.set_preferences('.json_encode($GLOBALS['egw_info']['user']['preferences']['c
$vars['current_users'] = $vars['quick_add'] = $vars['user_info']=''; $vars['current_users'] = $vars['quick_add'] = $vars['user_info']='';
} }
// add the name of the current application as global js variable - the
// name of the current application can be obtained by using the
// jsapi egw_getAppName() function
$content .= '<script type="text/javascript">window.egw_appName = "'.
$GLOBALS['egw_info']['flags']['currentapp'].'";</script>'."\n";
$this->tpl->set_var($vars); $this->tpl->set_var($vars);
$content .= $this->tpl->fp('out','navbar_header'); $content .= $this->tpl->fp('out','navbar_header');