-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlanguage-detect.js
65 lines (61 loc) · 1.31 KB
/
language-detect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Detects the browser language
*/
var userLang = navigator.language || navigator.userLanguage;
/*
test if ou cookie is set
*/
if(getCookie("lang")){
// it is set, so no redirect
}
else{
// The cookie is not set, so set it, and redirect
langstring=userLang.split('-')[0];
lang = "lang="+langstring;
document.cookie=lang;
redirect(langstring);
}
/*
Returns the given cookie or false if not existing
*/
function getCookie(name){
var re = new RegExp(name + "=([^;]+)");
var value = re.exec(document.cookie);
return (value != null) ? unescape(value[1]) : null;
}
/*
Function used to clean our language cookie
*/
function clearCookie(){
var mydate = new Date();
mydate.setTime(mydate.getTime() - 1);
document.cookie = "lang=; expires=" + mydate.toGMTString();
}
/*
redirects to the desired URLs
Only the first two letters of the ISO country code are used
*/
function redirect(actuallang){
var url ='/';
switch(actuallang){
case 'en':
url ='http://link-en';
break;
case 'fr':
url ='http://link-fr';
break;
case 'ru':
url ='http://link-ru';
break;
case 'es':
url ='http://link-es';
break;
case 'de':
url ='http://link-de';
break;
default:
url='http://link-default';
break;
}
window.location.replace(url);
}