add @mozilla.org/l20n;1
authorZbigniew Braniecki <gandalf@mozilla.com>
Thu, 05 Aug 2010 22:23:30 +0200
changeset 48086 c1a63dc4a3f7565fd2f37c4809ff158db15587bc
parent 48085 99efff8e39ea46f974f3e21a0f34f74f41bf64f5
child 48087 3225534777334be85ca0d6c347c307fab1bee573
push id7
push userzbraniecki@mozilla.com
push dateThu, 05 Aug 2010 20:23:43 +0000
milestone2.0b3pre
add @mozilla.org/l20n;1
intl/Makefile.in
intl/l20n/Makefile.in
intl/l20n/public/Makefile.in
intl/l20n/public/nsIL20n.idl
intl/l20n/src/Makefile.in
intl/l20n/src/nsL20n.js
intl/l20n/src/nsL20n.manifest
--- a/intl/Makefile.in
+++ b/intl/Makefile.in
@@ -41,16 +41,17 @@ srcdir    = @srcdir@
 VPATH     = @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 PARALLEL_DIRS = \
   locale \
   lwbrk \
   strres \
+  l20n \
   unicharutil \
   $(NULL)
 
 DIRS = \
   uconv \
   build \
   $(NULL)
 
new file mode 100644
--- /dev/null
+++ b/intl/l20n/Makefile.in
@@ -0,0 +1,52 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Netscape Communications Corporation.
+# Portions created by the Initial Developer are Copyright (C) 1998
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH   = ../..
+topsrcdir = @top_srcdir@
+srcdir    = @srcdir@
+VPATH   = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+DIRS = public src
+
+#ifdef ENABLE_TESTS
+#DIRS += test
+#endif
+
+include $(topsrcdir)/config/rules.mk
+
new file mode 100644
--- /dev/null
+++ b/intl/l20n/public/Makefile.in
@@ -0,0 +1,51 @@
+#
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Netscape Communications Corporation.
+# Portions created by the Initial Developer are Copyright (C) 1998
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH   = ../../..
+topsrcdir = @top_srcdir@
+srcdir    = @srcdir@
+VPATH   = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+MODULE    = l20n
+
+XPIDLSRCS = nsIL20n.idl \
+            $(NULL)
+
+include $(topsrcdir)/config/rules.mk
+
new file mode 100644
--- /dev/null
+++ b/intl/l20n/public/nsIL20n.idl
@@ -0,0 +1,9 @@
+#include "nsISupports.idl"
+
+[scriptable, uuid(3DF49EF4-CA04-41CF-8892-1CB9C8D524C7)]
+interface nsIL20n : nsISupports
+{
+  void addReference(in AString url);
+  
+  AString getValue(in AString id, in AString n);
+};  
new file mode 100644
--- /dev/null
+++ b/intl/l20n/src/Makefile.in
@@ -0,0 +1,58 @@
+#! gmake
+# 
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is mozilla.org code.
+#
+# The Initial Developer of the Original Code is
+# Netscape Communications Corporation.
+# Portions created by the Initial Developer are Copyright (C) 2000
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#   Stuart Parmenter <pavlov@netscape.com>
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+
+DEPTH   = ../../..
+topsrcdir = @top_srcdir@
+srcdir    = @srcdir@
+VPATH   = @srcdir@
+
+include $(DEPTH)/config/autoconf.mk
+
+MODULE    = l20n
+LIBRARY_NAME  = l20n_s
+
+
+
+EXTRA_COMPONENTS = nsL20n.manifest
+EXTRA_PP_COMPONENTS = nsL20n.js
+
+include $(topsrcdir)/config/rules.mk
+
+EXTRA_DSO_LDOPTS += $(MOZ_COMPONENT_LIBS)
+
new file mode 100644
--- /dev/null
+++ b/intl/l20n/src/nsL20n.js
@@ -0,0 +1,180 @@
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+
+/***********************************************************
+class definition
+***********************************************************/
+
+//class constructor
+function nsL20n() {
+  this.ctx = null;
+}
+
+// class definition
+nsL20n.prototype = {
+
+  // properties required for XPCOM registration:
+  classDescription: "L20n XPCOM Component",
+  classID:          Components.ID("3DF49EF4-CA04-41CF-8892-1CB9C8D524C7"),
+  contractID:       "@mozilla.org/l20n;1",
+
+  // QueryInterface implementation, e.g. using the generateQI helper (remove argument if skipped steps above)
+  QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIL20n]),
+
+  // ...component implementation...
+  // define the function we want to expose in our interface
+  addReference: function(url) {
+	if (!this.ctx)
+		this.ctx = new l20n.service.Context();
+    this.ctx.addReference(url);
+  },
+  getValue: function(id, n) {
+    return this.ctx.getValue(id, n);
+  }
+};
+
+var l20n = {};
+
+l20n.service = {
+  Context: function _contextConstructor() {
+    this._lols = [];
+    this._locale = null;
+    this._refs = [];
+    this._bindings = [];
+  },
+  LOL: function _lolConstructor() {
+    this._map = {};
+  },
+  getLOL: function _getLOL(aRef, aLocale, aContext, cb) {
+    var cachekey = [aLocale, aRef].toSource();
+    if (cachekey in this._cache) {
+      aContext.addLOL(this._cache[cachekey]);
+      return;
+    }
+
+	var read = function(data) {
+	  eval(data);
+	}
+
+    var callback = function(data, success) {
+      var lol = new l20n.service.LOL();
+      read.apply(lol._map, Array(data))
+      l20n.service._cache[cachekey] = lol;
+      aContext.addLOL(lol);
+      if (cb)
+        cb()
+    }
+    l20n.service.loader.start(aRef, callback);
+  },
+  _cache: {},
+}
+
+l20n.service.LOL.prototype = {
+  __exposedProps__: {addEntity: 'rw'},
+  addEntity: function _addEntity(entity) {
+    this._map[entity.ID] = entity;
+  },
+  get entities() {
+    return this._map;
+  }
+};
+
+function overwrite(b,a,t,s,n) {
+	  if (typeof b[a] == 'function') {
+	    b[a](t.resolveEntity(s).toString(n));
+	  } else {
+	    b[a] = t.resolveEntity(s).toString(n);
+	  }
+}
+
+l20n.service.Context.prototype = {
+ getValue: function _gv(string,n) {
+	try {
+	  return this.resolveEntity(string).toString(n);
+	} catch (e) {LOG('Missing entity:'+string)}
+  },
+ getAttributes: function _gv(string) {
+	try {
+      return this.resolveEntity(string);
+	} catch (e) {LOG('Missing entity:'+string)}
+  },
+ bindEntity: function _gv(string, i, b,a) {
+	var self=this;
+	function redraw(i,o,n) {
+      overwrite(b,a,self,string,n)
+      self._bindings[string][2] = {'tabs':n}
+	}
+	for (var el in i) i.watch(el, redraw)
+	try {
+	  var el=getKey(i,0)
+	  overwrite(b,a,self,string,el)
+	} catch (e) {LOG('Missing entity:'+string)}
+
+	this._bindings[string] = Array(b,a,i)
+  },
+ set locale(code) {
+   if (code == this.locale) return;
+   this._locale = code;
+   this._lols = [];
+   for (var i in this._refs) {
+	 if (this._refs[i]['locale'] == code)
+       l20n.service.getLOL(this._refs[i]['ref'], code, this);
+   }
+   for (i in this._bindings) {
+	   var bind = this._bindings[i];
+	   try {
+	     overwrite(bind[0],bind[1],this,i,getKey(bind[2],0))
+	   } catch (e){}
+   }
+  }, 
+ get locale() {
+   return this._locale;
+ },
+ addReference : function(aRef, locale, cb) {
+    this._refs.push({'ref':aRef,'locale':locale});
+    if (!locale && this.locale)
+    	locale = this.locale
+    l20n.service.getLOL(aRef,locale, this, cb);
+ },
+ resolveEntity: function _resolveEntity(key) {
+   return this._lols[0].entities[key];
+ },
+ // Internal API
+ addLOL: function _addLOL(lol) {
+   this._lols.push(lol);
+ }
+};
+
+function LOG(arg) {
+  if (typeof console != "undefined" && console.info) {console.info(arg);}
+  else
+    throw arg;
+};
+
+
+l20n.service.loader = {
+  start : function (url, callback) {
+    var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
+                    .createInstance(Components.interfaces.nsIXMLHttpRequest);
+    req.addEventListener("load", function() {
+      callback(req.responseText)
+    }, false);
+    req.open("GET", url, true);
+    req.overrideMimeType('text/plain; charset=x-user-defined')
+    req.send(null);
+  }
+}
+
+function getKey(arr) {
+  for (var u in arr)
+    return arr[u]
+}
+
+
+
+////////////////////////////////////////////////////////////////////////////////
+////Module
+
+const NSGetFactory = XPCOMUtils.generateNSGetFactory([nsL20n]);
+
+
new file mode 100644
--- /dev/null
+++ b/intl/l20n/src/nsL20n.manifest
@@ -0,0 +1,3 @@
+component {3DF49EF4-CA04-41CF-8892-1CB9C8D524C7} nsL20n.js
+contract @mozilla/l20n;1 {3DF49EF4-CA04-41CF-8892-1CB9C8D524C7}
+category profile-after-change nsL20n @mozilla/l20n;1