From 0aa128b53e3c9f6752daa4f4787193f9a56ff2ea Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Sun, 21 Aug 2011 20:09:14 +0000 Subject: [PATCH] Loading translations like other javascript files: setting headers to allow browser to cache the file until it's etag containing the creationdates of the used langfiles changes --> et2 can now use egw_lang object to translate labels, options, ... --- etemplate/inc/class.etemplate_new.inc.php | 9 ++++ phpgwapi/inc/class.egw.inc.php | 2 +- phpgwapi/lang.php | 53 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 phpgwapi/lang.php diff --git a/etemplate/inc/class.etemplate_new.inc.php b/etemplate/inc/class.etemplate_new.inc.php index f83a69a6fa..294043c1a0 100644 --- a/etemplate/inc/class.etemplate_new.inc.php +++ b/etemplate/inc/class.etemplate_new.inc.php @@ -103,6 +103,15 @@ class etemplate_new extends etemplate_widget_template { if (!$this->rel_path) throw new egw_exception_assertion_failed('No (valid) template read!'); + // load translations + translation::add_app('etemplate'); + foreach(translation::$loaded_apps as $app => $lang) + { + egw_framework::validate_file('/phpgwapi/lang.php', array( + 'app' => $app, + 'lang' => $lang, + )); + } // generate new etemplate request object self::$request = etemplate_request::read(); self::$request->output_mode = $output_mode; // let extensions "know" they are run eg. in a popup diff --git a/phpgwapi/inc/class.egw.inc.php b/phpgwapi/inc/class.egw.inc.php index 955efc528b..c51a4d6d75 100644 --- a/phpgwapi/inc/class.egw.inc.php +++ b/phpgwapi/inc/class.egw.inc.php @@ -234,7 +234,7 @@ class egw extends egw_minimal $this->template->set_root(EGW_APP_TPL); } // init the translation class, necessary as own wakeup would run before our's - translation::init(); + translation::init(isset($GLOBALS['egw_info']['flags']['load_translations']) ? $GLOBALS['egw_info']['flags']['load_translations'] : true); $this->unset_datetime(); diff --git a/phpgwapi/lang.php b/phpgwapi/lang.php new file mode 100644 index 0000000000..805bf9aa76 --- /dev/null +++ b/phpgwapi/lang.php @@ -0,0 +1,53 @@ + + * @package addressbook + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @version $Id$ + */ + +$GLOBALS['egw_info'] = array( + 'flags' => array( + 'currentapp' => in_array($_GET['app'],array('etemplate','common')) ? 'home' : $_GET['app'], + 'noheader' => true, + 'load_translations' => false, // do not automatically load translations + 'nocachecontrol' => true, + ) +); + +include '../header.inc.php'; + +// 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']).'"'; + +// headers to allow caching of one month +Header('Content-Type: text/javascript; charset=utf-8'); +Header('Cache-Control: public, no-transform'); +Header('Pragma: cache'); +Header('ETag: '.$etag); + +// if servers send a If-None-Match header, response with 304 Not Modified, if etag matches +if (isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] == $etag) +{ + header("HTTP/1.1 304 Not Modified"); + common::egw_exit(); +} + +translation::add_app($_GET['app'], $_GET['lang']); +if (!count(translation::$lang_arr)) +{ + translation::add_app($_GET['app'], 'en'); +} + + +echo ' +if (typeof egw_lang == "undefined") egw_lang = {}; +egw_lang.'.$_GET['app'].' = '.json_encode(translation::$lang_arr); + +// Content-Lenght header is important, otherwise browsers dont cache! +Header('Content-Length: '.ob_get_length()); \ No newline at end of file