2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* EGroupware clientside API object
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel (as AT stylite.de)
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:uses
|
|
|
|
egw_core;
|
2012-03-13 17:05:52 +01:00
|
|
|
egw_files;
|
|
|
|
egw_ready;
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
|
|
|
|
2013-10-07 19:00:03 +02:00
|
|
|
/**
|
|
|
|
* @augments Class
|
|
|
|
*/
|
2016-02-29 16:50:24 +01:00
|
|
|
egw.extend('lang', egw.MODULE_GLOBAL, function()
|
|
|
|
{
|
|
|
|
"use strict";
|
2012-03-01 17:24:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translations
|
2014-01-09 16:32:07 +01:00
|
|
|
*
|
2012-03-01 17:24:29 +01:00
|
|
|
* @access: private, use egw.lang() or egw.set_lang_arr()
|
|
|
|
*/
|
|
|
|
var lang_arr = {};
|
|
|
|
|
|
|
|
// Return the actual extension
|
|
|
|
return {
|
|
|
|
/**
|
|
|
|
* Set translation for a given application
|
2014-01-09 16:32:07 +01:00
|
|
|
*
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {string} _app
|
|
|
|
* @param {object} _messages message => translation pairs
|
2015-02-02 20:49:18 +01:00
|
|
|
* @param {boolean} _need_clone _messages need to be cloned, as it is from different window context
|
|
|
|
* and therefore will be inaccessible in IE, after that window is closed
|
2013-10-07 19:00:03 +02:00
|
|
|
* @memberOf egw
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
2015-02-02 20:49:18 +01:00
|
|
|
set_lang_arr: function(_app, _messages, _need_clone)
|
2012-03-01 17:24:29 +01:00
|
|
|
{
|
2012-06-12 20:47:42 +02:00
|
|
|
if(!jQuery.isArray(_messages))
|
|
|
|
{
|
2015-02-02 20:49:18 +01:00
|
|
|
// no deep clone jQuery.extend(true,...) neccessary, as _messages contains only string values
|
|
|
|
lang_arr[_app] = _need_clone ? jQuery.extend({}, _messages) : _messages;
|
2012-06-12 20:47:42 +02:00
|
|
|
}
|
2012-03-01 17:24:29 +01:00
|
|
|
},
|
2014-01-09 16:32:07 +01:00
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
/**
|
|
|
|
* Translate a given phrase replacing optional placeholders
|
2014-01-09 16:32:07 +01:00
|
|
|
*
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {string} _msg message to translate
|
|
|
|
* @param {...string} _arg1 ... _argN
|
|
|
|
* @return {string}
|
2012-03-01 17:24:29 +01:00
|
|
|
*/
|
|
|
|
lang: function(_msg, _arg1)
|
|
|
|
{
|
2015-04-21 21:47:23 +02:00
|
|
|
if(_msg === null)
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2015-04-16 18:19:41 +02:00
|
|
|
if(typeof _msg !== "string" && _msg)
|
2012-03-27 01:24:56 +02:00
|
|
|
{
|
|
|
|
egw().debug("warn", "Cannot translate an object", _msg);
|
|
|
|
return _msg;
|
|
|
|
}
|
2012-03-01 17:24:29 +01:00
|
|
|
var translation = _msg;
|
|
|
|
_msg = _msg.toLowerCase();
|
2014-01-09 16:32:07 +01:00
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
// search apps in given order for a replacement
|
2018-01-11 17:23:58 +01:00
|
|
|
var apps = this.lang_order || ['custom', this.getAppName(), 'etemplate', 'common', 'notifications'];
|
2012-03-01 17:24:29 +01:00
|
|
|
for(var i = 0; i < apps.length; ++i)
|
|
|
|
{
|
|
|
|
if (typeof lang_arr[apps[i]] != "undefined" &&
|
|
|
|
typeof lang_arr[apps[i]][_msg] != 'undefined')
|
|
|
|
{
|
|
|
|
translation = lang_arr[apps[i]][_msg];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (arguments.length == 1) return translation;
|
2014-01-09 16:32:07 +01:00
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
if (arguments.length == 2) return translation.replace('%1', arguments[1]);
|
2014-01-09 16:32:07 +01:00
|
|
|
|
2012-03-01 17:24:29 +01:00
|
|
|
// 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;
|
2012-03-13 17:05:52 +01:00
|
|
|
},
|
|
|
|
|
2014-01-23 11:52:44 +01:00
|
|
|
/**
|
|
|
|
* Load default langfiles for an application: common, _appname, custom
|
|
|
|
*
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {DOMElement} _window
|
2014-01-23 11:52:44 +01:00
|
|
|
* @param {string} _appname name of application to load translations for
|
|
|
|
* @param {function} _callback
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {object} _context
|
2014-01-23 11:52:44 +01:00
|
|
|
*/
|
|
|
|
langRequireApp: function(_window, _appname, _callback, _context)
|
|
|
|
{
|
|
|
|
var lang = egw.preference('lang');
|
|
|
|
var langs = [{app: 'common', lang: lang}];
|
|
|
|
|
|
|
|
if (_appname && _appname != 'eGroupWare')
|
|
|
|
{
|
|
|
|
langs.push({app: _appname, lang: lang});
|
|
|
|
}
|
|
|
|
langs.push({app: 'custom', lang: 'en'});
|
|
|
|
|
|
|
|
egw.langRequire(_window, langs, _callback, _context);
|
|
|
|
},
|
|
|
|
|
2012-03-13 17:05:52 +01:00
|
|
|
/**
|
|
|
|
* Includes the language files for the given applications -- if those
|
|
|
|
* do not already exist, include them.
|
|
|
|
*
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {DOMElement} _window is the window which needs the language -- this is
|
2012-03-13 17:05:52 +01:00
|
|
|
* needed as the "ready" event has to be postponed in that window until
|
|
|
|
* all lang files are included.
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {array} _apps is an array containing the applications for which the
|
2012-03-13 17:05:52 +01:00
|
|
|
* data is needed as objects of the following form:
|
|
|
|
* {
|
|
|
|
* app: <APPLICATION NAME>,
|
|
|
|
* lang: <LANGUAGE CODE>
|
|
|
|
* }
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {function} _callback called after loading, if not given ready event will be postponed instead
|
|
|
|
* @param {object} _context for callback
|
2012-03-13 17:05:52 +01:00
|
|
|
*/
|
2013-10-08 10:55:15 +02:00
|
|
|
langRequire: function(_window, _apps, _callback, _context) {
|
2012-03-13 17:05:52 +01:00
|
|
|
// Get the ready and the files module for the given window
|
|
|
|
var ready = this.module("ready", _window);
|
|
|
|
var files = this.module("files", this.window);
|
|
|
|
|
|
|
|
// Build the file names which should be included
|
|
|
|
var jss = [];
|
2013-10-07 19:00:03 +02:00
|
|
|
var apps = [];
|
2012-03-13 17:05:52 +01:00
|
|
|
for (var i = 0; i < _apps.length; i++)
|
|
|
|
{
|
2016-05-05 11:23:06 +02:00
|
|
|
if (!_apps[i].app) continue;
|
2012-03-13 17:05:52 +01:00
|
|
|
if (typeof lang_arr[_apps[i].app] === "undefined")
|
|
|
|
{
|
2014-01-09 16:32:07 +01:00
|
|
|
jss.push(this.webserverUrl +
|
2016-04-06 21:57:40 +02:00
|
|
|
'/api/lang.php?app=' + _apps[i].app +
|
2014-01-09 16:32:07 +01:00
|
|
|
'&lang=' + _apps[i].lang +
|
2014-02-21 12:10:11 +01:00
|
|
|
'&etag=' + (_apps[i].etag || this.config('max_lang_time')));
|
2012-03-13 17:05:52 +01:00
|
|
|
}
|
2013-10-07 19:00:03 +02:00
|
|
|
apps.push(_apps[i].app);
|
|
|
|
}
|
2014-04-28 18:17:41 +02:00
|
|
|
if (this !== egw && apps.length > 0)
|
2013-10-07 19:00:03 +02:00
|
|
|
{
|
|
|
|
this.lang_order = apps.reverse();
|
2012-03-13 17:05:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only continue if we need to include a language
|
|
|
|
if (jss.length > 0)
|
|
|
|
{
|
2013-10-08 10:55:15 +02:00
|
|
|
if (typeof _callback == 'function')
|
|
|
|
{
|
|
|
|
files.includeJS(jss, _callback, _context || null);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Require a "ready postpone token"
|
|
|
|
var token = ready.readyWaitFor();
|
2014-01-09 16:32:07 +01:00
|
|
|
|
2013-10-08 10:55:15 +02:00
|
|
|
// Call "readyDone" once all js files have been included.
|
|
|
|
files.includeJS(jss, function () {
|
|
|
|
ready.readyDone(token);
|
|
|
|
}, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (typeof _callback == 'function')
|
|
|
|
{
|
|
|
|
_callback.call(_context || null);
|
2012-03-13 17:05:52 +01:00
|
|
|
}
|
2012-03-01 17:24:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|