Last active
July 30, 2018 12:17
-
-
Save Molnfront/da1f0bd7a0264fa8f477 to your computer and use it in GitHub Desktop.
URL pathname replace for conditional redirects to support multi-language sites
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
/* pathnamereplace.js v0.1.2, URL pathname replace for conditional redirects to support multi-language sites, now with cookies and passtrue list. | |
* Author & copyright (c) 2014: Göran Svensson, goran@molnsys.com. MIT license. | |
* Tested with Chrome 36, Firefox 29 and Safari 7.0.5 on OSX. | |
* Tested with Chrome 36 on Xperia V (Android 4.3) and Internet 2.3.6 on Samsung GT-S5360 (Android 2.3.6). | |
* Works with any number of path name items and language acronymes. Does not break the back button. | |
* How to use it; add this in your nav menu: javascript:changeLanguage('_se') to switch to swedish version. | |
* REQUIRES: CatalystScripts/Java_Cookies.js and country = '{module_visitorcountrycode}'; added | |
* above this script (does not work inside a js file). | |
* No bugs have been harmed. | |
* To avoid unnecesary reload, build a nav menu for each language with the right path. | |
*/ | |
var defaultLang = '_en', // has no folder (eg: //features/faqs) | |
pathArray = window.location.pathname.split( '/' ), // get the path and split the string on "/" | |
langArray = ["_se","_es"], // Do not include the default lang here! You need to have a matching folder under root with the same structure as for the default language (eg: /_se/features/faqs). | |
passtrueArray = ["CampaignProcess.aspx","FormProcessv2.aspx"], // sometimes it is not possible to add language support to a module. Then we do not check language. | |
constructPath = window.location.protocol + "//" + window.location.host; | |
pathArray.splice(0,1); // remove the first item which is empty | |
var firstPartofPathname = pathArray[0], | |
cookieValue = readCookie('selectedLanguage'), | |
thepathname = window.location.pathname, | |
visitorLanguage = ""; | |
switch (country) { | |
case 'US': | |
visitorLanguage = "_en"; | |
break; | |
case 'SE': | |
visitorLanguage = "_se"; | |
break; | |
case 'UK': | |
visitorLanguage = "_en"; | |
break; | |
case 'ES': | |
visitorLanguage = "_es"; | |
break; | |
default: | |
visitorLanguage = defaultLang; | |
break; | |
} | |
// run it on every request | |
if (!checkPassTrue()){ | |
if (cookieValue!=null){ | |
changeLanguage(cookieValue);} | |
else { | |
changeLanguage(visitorLanguage); | |
} | |
} | |
// helper function: check if the first path item contains any of the known language acronymes | |
function checkLanguage(){ | |
var itemFoundFlag = false; | |
if (langArray.indexOf(firstPartofPathname) > -1) itemFoundFlag = true; | |
return itemFoundFlag; | |
} | |
// helper function: don´t redirect if the first path contains anything from the passtrue array. | |
function checkPassTrue(){ | |
var itemFoundFlag = false; | |
if (passtrueArray.indexOf(firstPartofPathname) > -1) itemFoundFlag = true; | |
return itemFoundFlag; | |
} | |
// helper function: put the pathname back together with slashes | |
function buildPath(theArray){ | |
var newPathname = ""; | |
for (i = 0; i < theArray.length; i++) { | |
newPathname += "/"; | |
newPathname += theArray[i]; | |
} | |
return newPathname; | |
} | |
// main function: changeLanguage takes a language acronyme as an argument and replaces the path name in the URL. | |
function changeLanguage(theLanguage){ | |
if (theLanguage == defaultLang){ | |
if (checkLanguage()){ | |
// remove the first item | |
pathArray.splice(0,1); | |
} | |
// avoid infinite loop | |
if (thepathname != buildPath(pathArray)){ | |
// create cookie | |
createCookie('selectedLanguage',defaultLang,100); | |
// replace the url | |
window.location.replace(constructPath + buildPath(pathArray)); | |
} | |
} else if (theLanguage != firstPartofPathname) { | |
if (checkLanguage()) { | |
// replace the lang in the URL | |
pathArray[0] = theLanguage; | |
} else if (checkLanguage() == 0) { | |
// add the Language | |
pathArray.splice(0, 0, theLanguage); | |
} | |
// avoid infinite loop | |
if (thepathname != buildPath(pathArray)){ | |
// create cookie | |
createCookie('selectedLanguage',theLanguage,100); | |
// replace the url | |
if (thepathname == "/"){ | |
window.location.replace(constructPath + '/' + theLanguage); | |
} else{ | |
window.location.replace(constructPath + buildPath(pathArray)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment