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