Ver código fonte

Updates i18next and bundles default translations.

master
damencho 8 anos atrás
pai
commit
1f200b63a7

+ 3
- 0
modules/UI/side_pannels/settings/SettingsMenu.js Ver arquivo

@@ -105,6 +105,9 @@ export default {
105 105
 
106 106
             APP.translation.translateElement(selectEl);
107 107
 
108
+            APP.translation.addLanguageChangedListener(
109
+                lng => selectInput[0].dataset.i18n = `languages:${lng}`);
110
+
108 111
             UIUtil.showElement(wrapperId);
109 112
         }
110 113
         // DEVICES LIST

+ 28
- 48
modules/translation/translation.js Ver arquivo

@@ -1,15 +1,15 @@
1 1
 /* global $, require, config, interfaceConfig */
2
-var i18n = require("i18next-client");
2
+import i18n from 'i18next';
3
+import XHR from 'i18next-xhr-backend';
4
+import jqueryI18next from 'jquery-i18next';
3 5
 var languages = require("../../service/translation/languages");
6
+var languagesR = require("json!../../lang/languages.json");
7
+var mainR = require("json!../../lang/main.json");
4 8
 var DEFAULT_LANG = languages.EN;
5 9
 
6
-i18n.addPostProcessor(
7
-    "resolveAppName",
8
-    value => value.replace("__app__", interfaceConfig.APP_NAME));
9
-
10 10
 var defaultOptions = {
11
-    detectLngQS: "lang",
12
-    useCookie: false,
11
+    compatibilityAPI: 'v1',
12
+    compatibilityJSON: 'v1',
13 13
     fallbackLng: DEFAULT_LANG,
14 14
     load: "unspecific",
15 15
     resGetPath: 'lang/__ns__-__lng__.json',
@@ -21,48 +21,11 @@ var defaultOptions = {
21 21
     fallbackOnNull: true,
22 22
     fallbackOnEmpty: true,
23 23
     useDataAttrOptions: true,
24
-    app: interfaceConfig.APP_NAME,
25
-    getAsync: true,
26
-    defaultValueFromContent: false,
27
-    customLoad: function(lng, ns, options, done) {
28
-        var resPath = "lang/__ns__-__lng__.json";
29
-        if(lng === languages.EN)
30
-            resPath = "lang/__ns__.json";
31
-        var url = i18n.functions.applyReplacement(resPath,
32
-                                                 { lng: lng, ns: ns });
33
-        i18n.functions.ajax({
34
-            url: url,
35
-            success: function(data) {
36
-                i18n.functions.log('loaded: ' + url);
37
-                done(null, data);
38
-            },
39
-            error : function(xhr, status, error) {
40
-                if ((status && status == 200) ||
41
-                    (xhr && xhr.status && xhr.status == 200)) {
42
-                    // file loaded but invalid json, stop waste time !
43
-                    i18n.functions.error('There is a typo in: ' + url);
44
-                } else if ((status && status == 404) ||
45
-                    (xhr && xhr.status && xhr.status == 404)) {
46
-                    i18n.functions.log('Does not exist: ' + url);
47
-                } else {
48
-                    var theStatus = status ? status :
49
-                        ((xhr && xhr.status) ? xhr.status : null);
50
-                    i18n.functions.log(theStatus + ' when loading ' + url);
51
-                }
52
-
53
-                done(error, {});
54
-            },
55
-            dataType: "json",
56
-            async : options.getAsync
57
-        });
58
-    }
59
-    //              options for caching
60
-//                useLocalStorage: true,
61
-//                localStorageExpirationTime: 86400000 // in ms, default 1 week
24
+    app: interfaceConfig.APP_NAME
62 25
 };
63 26
 
64 27
 function initCompleted() {
65
-    $("[data-i18n]").i18n();
28
+    $("[data-i18n]").localize();
66 29
 }
67 30
 
68 31
 function getLangFromQuery() {
@@ -98,7 +61,21 @@ module.exports = {
98 61
             options.lng = lang;
99 62
         }
100 63
 
101
-        i18n.init(options, initCompleted);
64
+        i18n.use(XHR)
65
+            .use({
66
+                type: 'postProcessor',
67
+                name: "resolveAppName",
68
+                process:
69
+                    function (res, key) {
70
+                        return i18n.t(key, {app: interfaceConfig.APP_NAME});
71
+                    }
72
+            })
73
+            .init(options, initCompleted);
74
+        // adds default language which is preloaded from code
75
+        i18n.addResourceBundle(DEFAULT_LANG, 'main', mainR, true, true);
76
+        i18n.addResourceBundle(
77
+            DEFAULT_LANG, 'languages', languagesR, true, true);
78
+        jqueryI18next.init(i18n, $, {useOptionsAttr: true});
102 79
     },
103 80
     setLanguage: function (lang) {
104 81
         if(!lang)
@@ -110,7 +87,7 @@ module.exports = {
110 87
     },
111 88
     translateElement: function (selector, options) {
112 89
         // i18next expects undefined if options are missing, check if its null
113
-        selector.i18n(
90
+        selector.localize(
114 91
             options === null ? undefined : options);
115 92
     },
116 93
     generateTranslationHTML: function (key, options) {
@@ -124,5 +101,8 @@ module.exports = {
124 101
         str += "</span>";
125 102
         return str;
126 103
 
104
+    },
105
+    addLanguageChangedListener: function(listener) {
106
+        i18n.on('languageChanged', listener);
127 107
     }
128 108
 };

+ 4
- 1
package.json Ver arquivo

@@ -21,7 +21,10 @@
21 21
     "autosize": "^1.18.13",
22 22
     "bootstrap": "3.1.1",
23 23
     "events": "*",
24
-    "i18next-client": "1.7.7",
24
+    "i18next": "3.4.4",
25
+    "i18next-xhr-backend": "1.1.0",
26
+    "jquery-i18next": "1.1.0",
27
+    "json-loader": "0.5.4",
25 28
     "jQuery-Impromptu": "trentrichardson/jQuery-Impromptu#v6.0.0",
26 29
     "jquery": "~2.1.1",
27 30
     "jquery-contextmenu": "*",

Carregando…
Cancelar
Salvar