File: src/elements/oreesh-i18n/oreesh-i18n.html
<script>
/**
Internationalization support for Oreesh
// Can be created dynamically
var i18n = new OreeshI18n();
<!-- Or via HTML -->
<oreesh-i18n key="name">My string</oreesh-i18n>
Set the locale at ````window['oreesh-i18n']```` like this:
window['oreesh-i18n'] = {
locale: 'en-us',
'en-us': {
name: 'My string'
},
'ko-kr': {
name: '나의 문자열'
}
};
@class oreesh-i18n
**/
</script>
<dom-module id="oreesh-i18n"></dom-module>
<script>
if (!window.oreeshI18N) window.oreeshI18N = {};
if (!window.oreeshI18N.locale) window.oreeshI18N.locale = 'en-us';
var objectPropertyChangeChecker = (function() {
var _obj = {};
return function(obj, cb) {
obj.handler = obj.handler || [];
obj.handler.push(cb);
/* dynamic not supported */
Object.keys(obj).forEach(function(key) {
_obj[key] = obj[key];
Object.defineProperty(obj, key, {
get: function() {
return _obj[key];
},
set: function(value) {
_obj[key] = value;
obj.handler.forEach(function(f) {
f();
});
}
});
});
};
})();
window.OreeshI18n = Polymer({
is: 'oreesh-i18n',
properties: {
key: {
type: String,
observer: '_update'
}
},
ready: function() {
var that = this;
objectPropertyChangeChecker(window.oreeshI18N, function() {
that._update();
});
this._update();
},
_update: function() {
if (!window.oreeshI18N.hasOwnProperty(this.key)) {
this.innerHTML = '';
return;
}
if (!window.oreeshI18N[this.key].hasOwnProperty(window.oreeshI18N.locale)) {
this.innerHTML = window.oreeshI18N[this.key]['en-us'];
return;
}
this.innerHTML = window.oreeshI18N[this.key][window.oreeshI18N.locale];
}
});
</script>