1
|
|
|
define(['polyglot', 'moment', 'helper'], function (Polyglot, moment, helper) { |
2
|
|
|
'use strict'; |
3
|
|
|
return function (config) { |
4
|
|
|
function languageSelect(el) { |
5
|
|
|
var select = document.createElement('select'); |
6
|
|
|
select.className = 'language-switch'; |
7
|
|
|
select.addEventListener('change', setLocale); |
8
|
|
|
el.appendChild(select); |
9
|
|
|
|
10
|
|
|
// Keep english |
11
|
|
|
select.innerHTML = '<option>Language</option>'; |
12
|
|
|
for (var i = 0; i < config.supportedLocale.length; i++) { |
13
|
|
|
select.innerHTML += '<option value="' + config.supportedLocale[i] + '">' + config.supportedLocale[i] + '</option>'; |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function setLocale(event) { |
18
|
|
|
localStorage.setItem('language', getLocale(event.target.value)); |
19
|
|
|
location.reload(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
function getLocale(input) { |
23
|
|
|
var language = input || localStorage.getItem('language') || navigator.languages && navigator.languages[0] || navigator.language || navigator.userLanguage; |
24
|
|
|
var locale = config.supportedLocale[0]; |
25
|
|
|
config.supportedLocale.some(function (item) { |
26
|
|
|
if (language.indexOf(item) !== -1) { |
27
|
|
|
locale = item; |
28
|
|
|
return true; |
29
|
|
|
} |
30
|
|
|
return false; |
31
|
|
|
}); |
32
|
|
|
return locale; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function setTranslation(json) { |
36
|
|
|
_.extend(json); |
37
|
|
|
|
38
|
|
|
moment.locale(_.locale(), { |
39
|
|
|
longDateFormat: { |
40
|
|
|
LT: 'HH:mm', |
41
|
|
|
LTS: 'HH:mm:ss', |
42
|
|
|
L: 'DD.MM.YYYY', |
43
|
|
|
LL: 'D. MMMM YYYY', |
44
|
|
|
LLL: 'D. MMMM YYYY HH:mm', |
45
|
|
|
LLLL: 'dddd, D. MMMM YYYY HH:mm' |
46
|
|
|
}, |
47
|
|
|
calendar: json.momentjs.calendar, |
48
|
|
|
relativeTime: json.momentjs.relativeTime |
49
|
|
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
window._ = new Polyglot({ locale: getLocale(), allowMissing: true }); |
53
|
|
|
helper.getJSON('locale/' + _.locale() + '.json?' + config.cacheBreaker).then(setTranslation); |
54
|
|
|
|
55
|
|
|
return { |
56
|
|
|
languageSelect: languageSelect |
57
|
|
|
}; |
58
|
|
|
}; |
59
|
|
|
}); |
60
|
|
|
|