Last active
November 16, 2022 23:26
-
-
Save bvandenbon/dae62a7008d34ab0895e42d9dc5131fa to your computer and use it in GitHub Desktop.
a landing page in html+js, which detects language (possibly cached) and redirects.
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
<!DOCTYPE html> | |
<html> | |
<body> | |
<script type="text/javascript"> | |
////////////// | |
/// CONFIG /// | |
////////////// | |
var defaultLocale = 'en'; | |
var routing = { | |
"en": 'https://mydomain.com/en', | |
"fr": 'https://mydomain.com/fr' | |
}; | |
var enableLog = true; | |
///////////////// | |
/// FUNCTIONS /// | |
///////////////// | |
function getBrowserLanguage() { | |
if (!navigator) return null; | |
if (navigator.languages && navigator.languages.length > 0) return navigator.languages[0]; | |
if (navigator.userLanguage) return navigator.userLanguage; | |
if (navigator.browserLanguage) return navigator.browserLanguage; | |
return navigator.language; | |
} | |
function getSubPath() { | |
var url = window.location.href; | |
var domainNameIndex = url.indexOf("//"); | |
var firstSlashIndex = url.indexOf("/", domainNameIndex + 2); | |
if (firstSlashIndex === -1) return null; | |
var subPath = url.substring(firstSlashIndex + 1); | |
return subPath; | |
} | |
function getQueryParam(name) { | |
var url = window.location.href; | |
name = name.replace(/[\[\]]/g, '\\$&'); | |
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'), | |
results = regex.exec(url); | |
if (!results) return null; | |
if (!results[2]) return ''; | |
return decodeURIComponent(results[2].replace(/\+/g, ' ')); | |
} | |
function redirectToRoute(locale, subPath) { | |
if (enableLog) console.log('matching locale ' + locale + ' ...'); | |
var route = routing[locale]; | |
if (route != null) { | |
if (enableLog) console.log('will redirect to ' + route); | |
if (subPath) { | |
if (!route.endsWith('/')) route += '/'; | |
route += subPath; | |
if (enableLog) console.log('full url will be ' + route); | |
} | |
window.location.replace(route); | |
return; | |
} | |
var parts = locale.split('-'); | |
if (parts == null || parts.length <= 1) return; | |
if (enableLog) console.log("will try to use just the language"); | |
redirectToRoute(parts[0], subPath); | |
} | |
//////////////// | |
/// REDIRECT /// | |
//////////////// | |
var locale = getQueryParam('locale'); | |
var saveLocale = true; | |
if (enableLog) console.log('url locale: ' + locale); | |
if (locale == null) { | |
locale = localStorage.getItem('locale'); | |
if (enableLog) console.log('stored locale: ' + locale); | |
if (locale != null) saveLocale = false; | |
} | |
if (locale == null) { | |
locale = getBrowserLanguage(); | |
if (enableLog) console.log('browser locale: ' + locale); | |
} | |
if (saveLocale === true) { | |
if (enableLog) console.log('storing locale: ' + locale); | |
localStorage.setItem('locale', locale); | |
} | |
if (locale == null) { | |
if (enableLog) console.log('will use default ' + defaultLocale); | |
locale = defaultLocale; | |
} | |
locale = locale.toLowerCase(); | |
var subPath = getSubPath(); | |
redirectToRoute(locale, subPath); | |
</script> | |
</body> | |
</html> | |
Looks good! Did you put your code under an open source license so that it can be used without hesitation?
you can without hesitation. I'm not sure which license I put on it. But you
really don't have to worry about anything.
I hereby state that it is free for commercial use, non-commercial use,
open-source, not-open source, with or without source code. Free for
everything you like. Except for the creation of nuclear weapons and for
rocket launchers. :-)
Op vr 7 mei 2021 om 16:08 schreef ph0llux ***@***.***>:
… ***@***.**** commented on this gist.
------------------------------
Looks good! Did you put your code under an open source license so that it
can be used without hesitation?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://gist.github.com/dae62a7008d34ab0895e42d9dc5131fa#gistcomment-3734874>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AE4IVSD2PDZ4Z3YVSHS2IRLTMPX6JANCNFSM4V2PET6Q>
.
Thanks for your response.
Ok, if you really want it that badly, I just won't use it for noclear weapons. 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was useful to my use case and saved me valuable time! Thanks @bvandenbon