[Mapbender-commits] r5001 - trunk/mapbender/http/plugins

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Tue Nov 17 10:40:18 EST 2009


Author: christoph
Date: 2009-11-17 10:40:16 -0500 (Tue, 17 Nov 2009)
New Revision: 5001

Added:
   trunk/mapbender/http/plugins/mb_i18n.js
   trunk/mapbender/http/plugins/mb_i18n_server.php
Log:


Added: trunk/mapbender/http/plugins/mb_i18n.js
===================================================================
--- trunk/mapbender/http/plugins/mb_i18n.js	                        (rev 0)
+++ trunk/mapbender/http/plugins/mb_i18n.js	2009-11-17 15:40:16 UTC (rev 5001)
@@ -0,0 +1,87 @@
+/**
+ * Package: i18n
+ *
+ * Description:
+ * Internationalization module, collects data from all elements
+ * and sends them to the server in a single POST request.
+ * The strings are translated via gettext only.
+ * 
+ * Files:
+ *  - http/plugins/mb_i18n.js
+ *  - http/plugins/mb_i18n_server.php
+ *
+ * SQL:
+ * > INSERT INTO gui_element (fkey_gui_id, e_id, e_pos, e_public, e_comment, 
+ * > e_title, e_element, e_src, e_attributes, e_left, e_top, e_width, 
+ * > e_height, e_z_index, e_more_styles, e_content, e_closetag, e_js_file, 
+ * > e_mb_mod, e_target, e_requires, e_url) VALUES('gui','i18n',1,1,
+ * > 'Internationalization module, collects data from all elements and sends them to the server in a single POST request. The strings are translated via gettext only.',
+ * > 'Internationalization','div','','',NULL ,NULL ,NULL ,NULL ,NULL ,'','',
+ * > 'div','../plugins/mb_i18n.js','','','',
+ * > 'http://www.mapbender.org/Gettext');
+ *
+ * Help:
+ * http://www.mapbender.org/Gettext
+ *
+ * Maintainer:
+ * http://www.mapbender.org/User:Christoph_Baudson
+ * 
+ * License:
+ * Copyright (c) 2009, Open Source Geospatial Foundation
+ * This program is dual licensed under the GNU General Public License 
+ * and Simplified BSD license.  
+ * http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+ */
+
+
+var I18n = function () {
+	var translationObj = {};
+	
+	this.queue = function (elementId, obj, callback) {
+		var t = translationObj[elementId] = {};
+		t.data = obj;
+		t.callback = callback;
+	};
+	this.localize = function (locale) {
+		Mapbender.events.localize.trigger();
+		
+		var req = new Mapbender.Ajax.Request({
+			url: "../plugins/mb_i18n_server.php",
+			method: "translate",
+			parameters: {
+				locale: locale,
+				data: translationObj
+			},
+			callback: function (obj, result, message) {
+				if (!result) {
+					new Mb_exception(message);
+					return;
+				}
+				Mapbender.locale = obj.locale;
+				for (var serverId in obj.data) {
+					// Processing translation for each element
+					new Mb_notice(
+						"Processing translation of " + serverId + "..."
+					);
+					for (var clientId in translationObj) {
+						if (clientId !== serverId) {
+							continue;
+						}
+						var t = translationObj[clientId];
+						if (typeof t.callback === "function") {
+							t.callback(obj.data[serverId]);
+							new Mb_notice(
+								"Processing translation of " + serverId + "...done"
+							);
+							break;
+						}
+					}
+				}
+				translationObj = {};
+			}
+		});
+		req.send();
+	};
+};
+
+Mapbender.modules[options.id] = $.extend(new I18n(options), Mapbender.modules[options.id]);

Added: trunk/mapbender/http/plugins/mb_i18n_server.php
===================================================================
--- trunk/mapbender/http/plugins/mb_i18n_server.php	                        (rev 0)
+++ trunk/mapbender/http/plugins/mb_i18n_server.php	2009-11-17 15:40:16 UTC (rev 5001)
@@ -0,0 +1,54 @@
+<?php
+/*
+ * License:
+ * Copyright (c) 2009, Open Source Geospatial Foundation
+ * This program is dual licensed under the GNU General Public License 
+ * and Simplified BSD license.  
+ * http://svn.osgeo.org/mapbender/trunk/mapbender/license/license.txt
+ */
+ 
+require_once(dirname(__FILE__)."/../../core/globalSettings.php");
+require_once(dirname(__FILE__) . "/../classes/class_locale.php");
+
+// translates all string values in a tree It can find
+function translateTree($tree) {
+
+	if (is_object($tree)) {
+    	foreach ($tree as $key => $value) {
+			$tree->$key = translateTree($value);
+		}
+	}
+	else if (is_array($tree)) {
+		// strings with arguments, like 'Found %d results'
+		$tree = call_user_func_array("_mb",	$tree);
+	}
+	else if (is_string($tree)) {
+		$tree = _mb($tree);
+	}
+ 	return $tree;
+}
+
+$ajaxResponse = new AjaxResponse($_POST);
+
+switch ($ajaxResponse->getMethod()) {
+	case "translate" :
+		Mapbender::session()->set("mb_lang", $ajaxResponse->getParameter("lang"));
+		
+		$localeObj = new Mb_locale(Mapbender::session()->get("mb_lang"));
+		$msg_obj = $ajaxResponse->getParameter("data");
+        $translated_obj = translateTree($msg_obj);
+		
+        $ajaxResponse->setSuccess(true);
+		$ajaxResponse->setResult(array(
+			"data" => $translated_obj,
+			"locale" => $localeObj->name
+		));
+		break;
+	default :
+		$ajaxResponse->setSuccess(false);
+		$ajaxResponse->setMessage(_mb("An unknown error occured."));
+		break;
+}
+
+$ajaxResponse->send();
+?>
\ No newline at end of file



More information about the Mapbender_commits mailing list