Skip to content

Instantly share code, notes, and snippets.

@honzabilek4
Created February 3, 2021 09:55
Show Gist options
  • Save honzabilek4/d816177d14324ad8b3e63dc73a34bfa9 to your computer and use it in GitHub Desktop.
Save honzabilek4/d816177d14324ad8b3e63dc73a34bfa9 to your computer and use it in GitHub Desktop.
import { i18n, initI18n, getLanguages, getCurrentLanguage } from "./i18n";
const createLanguageSelector = () => {
let template = '<select id="selector">';
getLanguages().forEach((l) => {
template += `
 <option ${l.language === getCurrentLanguage() ? "selected" : ""} value="${
l.language
}">
 ${l.localizedName}
 </option>`;
});
template += "</select>";
return template;
};
const getTranslatedContent = () => {
return i18n.t("hello_localazy");
};
const updateTranslatedContent = () => {
document.querySelector("#content").innerHTML = getTranslatedContent();
};
const initPageContent = () => {
document.querySelector("#app").innerHTML = ` 
 ${createLanguageSelector()} 
 <div id="content"> 
 ${getTranslatedContent()}
 </div>`;
document.querySelector("#selector").addEventListener("change", (e) => {
i18n.changeLanguage(e.target.value);
updateTranslatedContent();
});
};
initI18n(initPageContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment