Skip to content

Instantly share code, notes, and snippets.

@DerGoogler
Created February 26, 2022 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DerGoogler/3f90466ba3f75447f153259eea87f593 to your computer and use it in GitHub Desktop.
Save DerGoogler/3f90466ba3f75447f153259eea87f593 to your computer and use it in GitHub Desktop.
Translation with HTML
// <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