-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.l10n.js
executable file
·66 lines (56 loc) · 1.82 KB
/
jquery.l10n.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
'use strict';
$.fn.l10n = function(options) {
const that = this;
let dict = {};
let params = $.extend({
lang: 'en',
dictList: []
}, options);
// Defining Language
let language = params.lang;
if (localStorage.getItem('lang')) {
language = (localStorage.getItem('lang')) ? localStorage.getItem('lang') : navigator.language.substr(0, 2);
} else {
localStorage.setItem('lang', language);
}
if (params.dictList.hasOwnProperty(language)) {
dict = params.dictList[language];
} else {
console.log(`You don't has the variable '${language}' in the 'dictionary.js', you may create it first\nYou can check our documetation to get more informations here https://github.com/karim88/jquery.l10n`);
}
/**
* get Translated text
* @param text
* @returns text
*/
this.getText = (text) => {
return dict[text] || text;
};
/**
* Change lang html attribute & local storage one
* @param lang
*/
this.setLanguage = (lang) => {
document.querySelector('html').setAttribute('lang', lang);
localStorage.setItem('lang', lang);
location.reload();
};
/**
* Translate the whole website
*/
this.translate = () => {
that.each((index, element) => {
// Check Html element
if (dict[element.innerHTML.trim()]) {
element.innerHTML = dict[element.innerHTML.trim()];
}
// Check Placeholder attribute
if (element.getAttribute('placeholder')) {
element.setAttribute('placeholder', dict[element.getAttribute('placeholder').trim()]);
}
});
};
document.querySelector('html').setAttribute('lang', language);
that.translate();
return that;
}