Skip to content

Instantly share code, notes, and snippets.

@steida
Created May 6, 2009 11:31
Show Gist options
  • Save steida/107490 to your computer and use it in GitHub Desktop.
Save steida/107490 to your computer and use it in GitHub Desktop.
/*
Resource Manager
- change app language just in time -> AUI.ResourceManager.setLocales(swahilstina)
- give localized text to element - el.set('locale', 'fileOpen')
- locale value could be: 'fileOpen' or ['fileOpen', 'title(anyProperty)'] or custom localize function
- if custom localization function returns value, that value will be used for localization
- .. but first of all you have to create locale storage (exported .net or own localization to JSON { dictionaryKey: term})
Example: viz. demo
*/
AUI.ResourceManager = new new Class({
Implements: [Events],
initialize: function() {
this.els = [];
this.untranslated = [];
this.extendElement();
},
extendElement: function() {
var els = this.els,
localize = this.localize.bind(this);
Element.Properties.locale = {
set: function(value, prop) {
var item = { el: this, value: Array.flatten(arguments) };
var oldItem = els.find(function(it){
return it.el == this;
}, this);
if (oldItem) els.erase(oldItem);
els.push(item);
localize(item);
}
}
},
localize: function(item) {
var value = item.value[0],
prop = item.value[1] || (item.el.get('tag') == 'input' ? 'value' : 'html');
switch ($type(value)) {
case 'string': item.el.set(prop, this.getTerm(value)); break;
case 'function': (value = value.call(item.el, this)) && item.el.set(prop, value);
}
},
getTerm: function(key) {
var term = this.storage[key];
if (!term) {
this.untranslated.push(key);
term = 'untranslated term: ' + key;
}
else {
if (Browser.Platform['mac']) {
// every 'Ctrl +' means control shorthand, so change it to mac incarnation
term = term.replace(/Ctrl \+/g, 'Cmd +');
}
}
return term;
},
getUntranslated: function() {
return this.untranslated;
},
setLocales: function(locales) {
this.storage = locales;
for (var i = this.els.length; i--; )
this.localize(this.els[i]);
this.fireEvent('localize');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment