-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlocalisation.js
93 lines (93 loc) · 2.4 KB
/
localisation.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
include = function (url, fn) {
var e = document.createElement("script");
e.onload = fn;
e.src = url;
e.async=true;
document.getElementsByTagName("head")[0].appendChild(e);
};
//entry point
//document.onload = loadStartLocale();
//now we need some language for start
function loadStartLocale(){
//check whether browser support localStorage
isLocalStorageSupport = checkLocalStorageSupport();
if (isLocalStorageSupport===true) { //for all modern browsers, including IE8 and newer
language = localStorage.getItem("areso-lang"); //try load
if (language === null) {
language = getBrowserLanguage();
}
} else {
//read cookies and find language
language = document.cookie.replace(/(?:(?:^|.*;\s*)areso-lang\s*\=\s*([^;]*).*$)|^.*$/, "$1");
if (language === null) {
language = getBrowserLanguage();
}
}
if (language.indexOf('en')!==-1) {
language = 'en-US';
}
if (language.indexOf('ru')!==-1) {
language = 'ru-RU';
}
if (language.indexOf('de')!==-1) {
language = 'de-DE';
}
if (language.indexOf('fr')!==-1) {
language = 'fr-FR';
}
if (language.indexOf('fi')!==-1) {
language = 'fi-FI';
}
locales = ['en-US','ru-RU','de-DE','eo','fr-FR','fi-FI'];
default_locale = 'en-US';
if (checkValue(language, locales)===1) {
loadLocale(language);
} else {
loadLocale(default_locale);
}
}
function checkValue(value,arr){
var status = -1;
for(var i=0; i<arr.length; i++){
var name = arr[i];
if(name == value){
status = 1;
break;
}
}
return status;
}
function getBrowserLanguage() {
//if not set, then
try { //try to get the browser language
language = navigator.language;
} catch(e) { //in case we use IE9, IE8
language = navigator.userLanguage;
}
if (language===null) {//just to be sure about IE9, IE8
language = navigator.userLanguage;
}
return language;
}
function checkLocalStorageSupport() {
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}
function loadLocale(language){
file = 'langs/'+language+'.js';
include(file,function(){
localeCallback(language);
});
isLocalStorageSupport = checkLocalStorageSupport();
if (isLocalStorageSupport===true) { //for all modern browsers, including IE8 and newer
localStorage.setItem("areso-lang", language); //try save
} else {
document.cookie = "areso-lang="+language;
}
};