strolch-wc-localize-behavior/strolch-wc-localize-behavio...

52 lines
1.9 KiB
HTML
Raw Normal View History

<link rel="import" href="../app-localize-behavior/app-localize-behavior.html">
<script>
// custom behaviour accessing the configuration above
StrolchLocalizeBehaviorImpl = {
2021-09-10 08:03:01 +02:00
properties: {
fallbackLanguage: {
type: String,
value: "en"
}
},
created: function () {
this.__computeLocalizeOld = this.__computeLocalize;
this.__computeLocalize = function (language, resources, formats) {
var localizeFunc;
2021-09-10 08:03:01 +02:00
if (!resources[language])
localizeFunc = this.__computeLocalizeOld(this.fallbackLanguage, resources, formats).bind(this);
localizeFunc = this.__computeLocalizeOld(language, resources, formats).bind(this);
return function () {
var key = arguments[0];
2023-07-04 12:35:05 +02:00
if(!key)
2023-07-07 08:06:06 +02:00
return;
2023-07-04 12:35:05 +02:00
if (!resources || !language || !resources[language])
return key;
try {
return localizeFunc.apply(this, arguments);
} catch (e) {
console.log("Failed to translate key " + key);
console.log(e);
return key;
}
}.bind(this);
2021-09-10 08:03:01 +02:00
}.bind(this);
},
attached: function () {
// load the language resource of the configured language
2021-09-10 08:03:01 +02:00
this.language = Strolch.getUserLocale();
2017-03-08 16:34:42 +01:00
this.useKeyIfMissing = true;
if (Strolch.isNotEmptyString(this.localesPath)) {
this.loadResources(this.resolveUrl(this.localesPath));
}
2021-09-10 08:03:01 +02:00
},
};
// inherit the behavior of the standard polymer localization
StrolchLocalizeBehavior = [Polymer.AppLocalizeBehavior, StrolchLocalizeBehaviorImpl];
</script>