Skip to content
Snippets Groups Projects
Commit a494d5e0 authored by Kocher, Manuel's avatar Kocher, Manuel
Browse files

Add innerText to dbp-translation (cypress support), rework showcase example

parent 40c678c2
No related branches found
No related tags found
No related merge requests found
Pipeline #221168 passed
...@@ -301,6 +301,7 @@ html { ...@@ -301,6 +301,7 @@ html {
<div class="control" id="dbp-translated-demo"> <div class="control" id="dbp-translated-demo">
<dbp-translated subscribe="lang"> <dbp-translated subscribe="lang">
<div slot="de"> <div slot="de">
<!---->
Dieser Text ist Deutsch und wird Englisch werden wenn man die Dieser Text ist Deutsch und wird Englisch werden wenn man die
Sprache auf Englisch stellt. Sprache auf Englisch stellt.
</div> </div>
...@@ -314,13 +315,12 @@ html { ...@@ -314,13 +315,12 @@ html {
<div class="content"> <div class="content">
<h2>Translation Demo</h2> <h2>Translation Demo</h2>
<div class="control" id="dbp-translation-demo"> <div class="control" id="dbp-translation-demo">
<dbp-translation subscribe="lang, lang-dir" key="link" var='{"linkDE": "https://www.tugraz.at/home/", "linkEN": "https://www.tugraz.at/en/home/"}' unsafe></dbp-translation><br/> <dbp-translation id="dbp-translation-link" subscribe="lang, lang-dir" key="link" var='{"linkDE": "https://www.tugraz.at/home/", "linkEN": "https://www.tugraz.at/en/home/"}' unsafe></dbp-translation><br/>
<dbp-translation subscribe="lang, lang-dir" key="theme-switcher-demo"></dbp-translation> <dbp-translation id="dbp-translation-showcase" subscribe="lang, lang-dir" key="toolkit-showcase"></dbp-translation><br/>
<dbp-translation id="dbp-translation-theme-switcher" subscribe="lang, lang-dir" key="theme-switcher-demo"></dbp-translation><br/>
<dbp-theme-switcher subscribe="lang, lang-dir" <dbp-theme-switcher subscribe="lang, lang-dir"
themes='[{"class": "light-theme", "icon": "sun", "name": "${this._i18n.t('dbp.themes.light-mode')}"}, {"class": "dark-theme", "icon": "night", "name": "${this._i18n.t('dbp.themes.dark-mode')}"}]'></dbp-theme-switcher> themes='[{"class": "light-theme", "icon": "sun", "name": "Light Mode"}, {"class": "dark-theme", "icon": "night", "name": "Dark Mode"}]'></dbp-theme-switcher>
<dbp-translation key="toolkit-showcase" subscribe="lang, lang-dir"></dbp-translation> <dbp-translation key="abc" subscribe="lang, lang-dir"></dbp-translation> <!-- shouldnt be displayed -->
<dbp-translation key="toolkit-showcase-link" var='{"link1": "https://www.i18next.com/translation-function/interpolation"}' subscribe="lang, lang-dir" unsafe></dbp-translation>
<dbp-translation key="abc" subscribe="lang, lang-dir"></dbp-translation>
</div> </div>
</div> </div>
</section> </section>
......
...@@ -15,6 +15,7 @@ export class Translation extends DBPLitElement { ...@@ -15,6 +15,7 @@ export class Translation extends DBPLitElement {
// dir and i18next instance of translation overrides // dir and i18next instance of translation overrides
this._i18n = createInstance(); this._i18n = createInstance();
this.langDir = ''; this.langDir = '';
this.innerText = '';
} }
...@@ -72,17 +73,25 @@ export class Translation extends DBPLitElement { ...@@ -72,17 +73,25 @@ export class Translation extends DBPLitElement {
this._i18n.options.fallbackNS : this._i18n.options.fallbackNS :
getOverrideNamespace(this._i18n.options.fallbackNS); getOverrideNamespace(this._i18n.options.fallbackNS);
let translation = "";
// request to i18n translation // request to i18n translation
let translation = (() => { if (this.interpolation && this.unsafe) {
if (this.interpolation && this.unsafe) this.innerText = this._i18n.t(this.key, this.interpolation);
return unsafeHTML(this._i18n.t(this.key, this.interpolation)); translation = unsafeHTML(this._i18n.t(this.key, this.interpolation));
else if (this.interpolation) }
return this._i18n.t(this.key, this.interpolation); else if (this.interpolation) {
else if (this.unsafe) this.innerText = this._i18n.t(this.key, this.interpolation);
return unsafeHTML(this._i18n.t(this.key)); translation = this._i18n.t(this.key, this.interpolation);
else }
return this._i18n.t(this.key); else if (this.unsafe) {
})(); this.innerText = this._i18n.t(this.key);
translation = unsafeHTML(this._i18n.t(this.key));
}
else {
this.innerText = this._i18n.t(this.key);
translation = this._i18n.t(this.key);
}
...@@ -92,16 +101,18 @@ export class Translation extends DBPLitElement { ...@@ -92,16 +101,18 @@ export class Translation extends DBPLitElement {
let keyComment = ""; let keyComment = "";
if ((this._i18n.exists(overrideNamespace + ":" + this.key) && this._i18n.hasResourceBundle(this.lang, overrideNamespace)) if ((this._i18n.exists(overrideNamespace + ":" + this.key) && this._i18n.hasResourceBundle(this.lang, overrideNamespace))
|| window.location.hash.includes('debug')) { || window.location.hash.includes('debug')) {
keyComment = unsafeHTML("<!-- key: " + this.key + "-->"); keyComment = unsafeHTML("<!-- key: " + this.key + "-->");
} else if (this._i18n.hasResourceBundle(this.lang, overrideNamespace)){ } else if (this._i18n.hasResourceBundle(this.lang, overrideNamespace)){
keyComment = unsafeHTML("<!-- key '" + this.key + "' not found! -->"); this.innerText = "";
translation = ""; translation = "";
console.error("Key '" + this.key + "' not found!"); keyComment = unsafeHTML("<!-- key '" + this.key + "' not found! -->");
console.error("Key '" + this.key + "' not found!");
} else { } else {
keyComment = unsafeHTML("<!-- key '" + this.key + "' and namespace '" + overrideNamespace + "' not found! -->"); this.innerText = "";
translation = ""; translation = "";
keyComment = unsafeHTML("<!-- key '" + this.key + "' and namespace '" + overrideNamespace + "' not found! -->");
console.error("Key '" + this.key + "' and namespace '" + overrideNamespace + "' not found!");
} }
// load translation text // load translation text
return html` return html`
${keyComment} ${keyComment}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment