Last active
June 19, 2019 06:29
-
-
Save jsakhil/f7f70e9b13ec7a986333729df6933aa6 to your computer and use it in GitHub Desktop.
qTranslate WordPress Plugin Custom Language Switch
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
<div class="language_switch" tabindex="0"> | |
<span class="current" data-value="en">EN</span> | |
<ul class="list"> | |
<li data-value="en" class="option selected">EN</li> | |
<li data-value="fr" class="option">FR</li> | |
<li data-value="de" class="option">DE</li> | |
</ul> | |
</div> | |
<script> | |
jQuery('.language_switch li').click(function(){ | |
var date = new Date(); | |
date.setTime(date.getTime() + ( 30 * 24 * 60 * 60 * 1000)); | |
var expires = date.toUTCString(); | |
var lang = jQuery(this).data('value'); | |
var url_host = window.location.host; | |
var url_path = window.location.pathname; | |
var path_arr = url_path.split( '/' ); | |
if(path_arr[1].length == 2){ | |
path_arr = jQuery.grep(path_arr, function(value) { | |
return value.length != 2; | |
}); | |
url_path = path_arr.join("/"); | |
var new_url = 'http://' + url_host + "/" + lang + url_path; | |
document.cookie = "qtrans_front_language=" + lang + "; expires= " + expires + "; path=/;"; | |
if(document.cookie.indexOf('qtrans_front_language=' + lang ) != -1){ | |
window.location.href = new_url; | |
} | |
}else{ | |
url_path = path_arr.join("/"); | |
var new_url = 'http://' + url_host + "/" + lang + url_path; | |
document.cookie = "qtrans_front_language=" + lang + "; expires= " + expires + "; path=/;"; | |
if(document.cookie.indexOf('qtrans_front_language=' + lang ) != -1){ | |
window.location.href = new_url; | |
} | |
} | |
}) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment