Revision: 6815
Author:
jand...@gmail.com
Date: Sun May 19 14:35:01 2013
Log: MultiLangLabel is functional
http://code.google.com/p/ppwcode/source/detail?r=6815
Added:
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangLabelParent.js
Modified:
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/MultiLangLabel.js
=======================================
--- /dev/null
+++
/javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/_MultiLangLabelParent.js
Sun May 19 14:35:01 2013
@@ -0,0 +1,44 @@
+define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/_base/kernel", "dojo/i18n", "../xml"],
+ function(declare, _WidgetBase, kernel) {
+
+ var _MultiLangLabelParent = declare([_WidgetBase], {
+ // summary:
+ // Optional parent for MultiLangParent. We can set
+ // - nlsParentDirectory: the directory containing the used nls
directory
+ // - bundle: the name of the i18n file
+ // - lang; the locale, which can change
+ //
+ // All locales must be defined as extraLocale in dojoConfig.
+ // The actual i18n resource must be loaded using the i18n! plugin
syntax.
+ //
+ // If nlsParentDirectory is not set, we see if we have a `mid`
property on the Constructor.
+ // If so, we derive it from that, and cache it.
+
+ // nlsParentDirectory: String?
+ nlsParentDirectory: null,
+
+ // bundle: String?
+ bundle: null,
+
+ lang: kernel.locale, // default language is browser dependent
+
+ _getNlsParentDirectoryAttr: function() {
+ if ((!this.nlsParentDirectory) && this.constructor.mid) {
+ this.set("nlsParentDirectory",
_MultiLangLabelParent.dirFromMid(this.constructor.mid));
+ }
+ return this.nlsParentDirectory;
+ }
+
+ });
+
+ _MultiLangLabelParent.dirFromMid = function(mid) {
+ // summary:
+ // Helper function to get the directory from a MID
+ var parts = mid.split("/");
+ parts.pop();
+ return parts.join("/");
+ };
+
+ return _MultiLangLabelParent;
+ }
+);
=======================================
--- /javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/MultiLangLabel.js
Sun May 19 12:06:31 2013
+++ /javascript/util/oddsAndEnds/trunk/src/oddsAndEnds/ui/MultiLangLabel.js
Sun May 19 14:35:01 2013
@@ -1,7 +1,7 @@
-define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/_base/kernel", "dojo/i18n", "../xml"],
- function(declare, _WidgetBase, kernel, i18n, xml) {
+define(["dojo/_base/declare", "./_MultiLangLabelParent", "dojo/_base/kernel", "dojo/i18n", "../xml"],
+ function(declare, _MultiLangLabelParent, kernel, i18n, xml) {
- return declare([_WidgetBase], {
+ return declare([_MultiLangLabelParent], {
// summary:
// Widget that is specially made to represent a i18n (nls) label
in a template,
// when multiple languages must be shown, and the language can
change dynamically.
@@ -11,6 +11,11 @@
// - label: the name of the property in that file to show
// - lang; the locale, which can change
//
+ // If any of these are not a meaningful value, we look upwards in
the widget
+ // tree for a value _MultiLangLabelParent, and use its values.
+ //
+ // If bindLang is true (the default), we bind lang on startup to
the lang of a parent, if there is one.
+ //
// All locales must be defined as extraLocale in dojoConfig.
// The actual i18n resource must be loaded using the i18n! plugin
syntax.
//
@@ -27,21 +32,28 @@
// Default is true.
escapeXml: true,
- // nlsParentDirectory: String?
- nlsParentDirectory: null,
-
- // bundle: String?
- bundle: null,
-
// label: String?
label: null,
- lang: kernel.locale, // default language is browser dependent
+ bindLang: true,
+ _parentLangHandle: null,
- postCreate: function() {
+ startup: function() {
+ this.inherited(arguments);
+ if (this.bindLang) {
+ this._bindLang();
+ }
this._output();
},
+ destroy: function() {
+ this.inherited(arguments);
+ if (this._parentLangHandle) {
+ this._parentLangHandle.remove();
+ this._parentLangHandle = null;
+ }
+ },
+
set: function(name, value){
// summary:
// Override and refresh output on value change.
@@ -55,16 +67,66 @@
}
},
+ _bindLang: function() {
+ function parent(/*_WidgetBase*/ wb) {
+ if (!wb) {
+ return null
+ }
+ var parentWb = wb.getParent();
+ if (parentWb.isInstanceOf(_MultiLangLabelParent)) {
+ return parentWb;
+ }
+ return parent(parentWb);
+ }
+
+ var self = this;
+ var parentWb = parent(self);
+ // TODO do something with own
+ if (parentWb) {
+ self._parentLangHandle = parentWb.watch("lang",
function(propName, oldValue, newValue) {
+ if (oldValue !== newValue) {
+ self.set("lang", newValue);
+ }
+ });
+ }
+ },
+
+ _setBindLang: function(value) {
+ if (this.bindLang != value) {
+ if (this.bindLang && this._parentLangHandle) {
+ this._parentLangHandle.remove();
+ }
+ this._set("bindLang", value);
+ if (this.bindLang) {
+ this._bindLang();
+ }
+ }
+ },
+
_output: function(){
// summary:
// Produce the data-bound output, xml-escaped.
// tags:
// protected
+ function lookup(/*_WidgetBase*/ wb, /*String*/ propName) {
+ if (!wb) {
+ return null;
+ }
+ var result;
+ if (wb.isInstanceOf(_MultiLangLabelParent)) { // this is too
+ result = wb.get(propName);
+ }
+ return result || lookup(wb.getParent(), propName);
+ }
+
var render = this.missing;
- if (this.nlsParentDirectory && this.bundle && this.label) {
+ var nlsParentDir = lookup(this, "nlsParentDirectory");
+ var bundle = lookup(this, "bundle");
+ var lang = lookup(this, "lang") || kernel.locale;
+ if (nlsParentDir && bundle && this.label) {
try {
- var labels = i18n.getLocalization(this.nlsParentDirectory,
this.bundle, this.lang || kernel.locale);
+ var labels = i18n.getLocalization(nlsParentDir, bundle, lang);
render = labels[this.label];
}
catch (err) {