Created
February 26, 2022 16:01
-
-
Save DerGoogler/3f90466ba3f75447f153259eea87f593 to your computer and use it in GitHub Desktop.
Translation with HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <dg-lang de="" en=""></dg-lang> | |
class TTSLANG extends HTMLElement { | |
connectedCallback() { | |
if (getUrlParam("lang") == "en") { | |
this.innerHTML = this.getAttribute('en'); | |
} else if (getUrlParam("lang") == "de") { | |
this.innerHTML = this.getAttribute('de'); | |
} | |
} | |
} | |
customElements.define("dg-lang", TTSLANG); | |
/* | |
DE: | |
https://example.com/?lang=de --> <dg-lang de="Zugriff" en="Access">Zugriff</dg-lang> | |
EN: | |
https://example.com/?lang=en --> <dg-lang de="Zugriff" en="Access">Access</dg-lang> | |
*/ | |
function getUrlParam(param) { | |
param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1"); | |
var regex = new RegExp("[?]" + param + "=([^&#]*)"); | |
var url = decodeURIComponent(window.location.href); | |
var match = regex.exec(url); | |
return match ? match[1] : ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment