Skip to content

Instantly share code, notes, and snippets.

@Sammiess
Created November 18, 2024 19:53
Show Gist options
  • Save Sammiess/00f6a34c852a5365db9679e5d1c56e0e to your computer and use it in GitHub Desktop.
Save Sammiess/00f6a34c852a5365db9679e5d1c56e0e to your computer and use it in GitHub Desktop.
Language Switcher
<script>
document.addEventListener("DOMContentLoaded", function() {
const userLang = navigator.language || navigator.userLanguage;
const languageSwitchDiv = document.createElement("div");
if (userLang.startsWith("de")) {
languageSwitchDiv.innerHTML = 'You are visiting the English website. <a href="https://yourwebsite.de" hreflang="de">Click here to visit the German website</a>.';
} else if (userLang.startsWith("en")) {
languageSwitchDiv.innerHTML = 'Sie besuchen die deutsche Website. <a href="https://yourwebsite.com" hreflang="en">Klicken Sie hier, um zur englischen Website zu wechseln</a>.';
}
languageSwitchDiv.style.position = "fixed";
languageSwitchDiv.style.bottom = "10px";
languageSwitchDiv.style.right = "10px";
languageSwitchDiv.style.backgroundColor = "#f9f9f9";
languageSwitchDiv.style.padding = "10px";
languageSwitchDiv.style.border = "1px solid #ccc";
languageSwitchDiv.style.zIndex = "1000";
document.body.appendChild(languageSwitchDiv);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment