-
Notifications
You must be signed in to change notification settings - Fork 801
/
i18next.js
89 lines (76 loc) · 2.24 KB
/
i18next.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
/**
* Copyright (C) 2015 Laverna project Authors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* global define */
define([
'underscore',
'jquery',
'q',
'backbone.radio',
'i18next',
'i18nextXHRBackend',
'text!locales/locales.json'
], function(_, $, Q, Radio, i18n, XHR, locales) {
'use strict';
var __ = {
/**
* Initialize i18next
*/
init: function() {
var defer = Q.defer();
$.t = i18n.t.bind(i18n);
i18n
.use(XHR)
.init(
{
lng : __.getLang(),
fallbackLng : ['en'],
ns : [''],
defaultNS : '',
backend : {
loadPath : 'locales/{{lng}}/translation.json'
},
},
function() {
defer.resolve();
}
);
return defer.promise;
},
/**
* Get language either from configs or
* autodetect it from browser settings.
*/
getLang: function() {
var lng = Radio.request('configs', 'get:config', 'appLang');
if (lng || typeof window.navigator === 'undefined') {
return lng;
}
// Language keys in navigator
lng = ['languages', 'language', 'userLanguage', 'browserLanguage'];
// Available locales
locales = _.keys(JSON.parse(locales));
return _.chain(window.navigator)
.pick(lng)
.values()
.flatten()
.compact()
.map(function(key) {
return key.replace('-', '_').toLowerCase();
})
.find(function(key) {
return _.contains(locales, key);
})
.value();
},
};
/**
* Init i18next on `app:before` initialize request.
*/
Radio.request('init', 'add', 'app:before', __.init);
return __;
});