瀏覽代碼

Merge pull request #1069 from jitsi/update-i18next

Updates i18next and bundles default translations.
j8
hristoterezov 8 年之前
父節點
當前提交
f08e9afc19

+ 2
- 1
app.js 查看文件

@@ -29,6 +29,7 @@ import API from './modules/API/API';
29 29
 
30 30
 import UIEvents from './service/UI/UIEvents';
31 31
 import getTokenData from "./modules/TokenData/TokenData";
32
+import translation from "./modules/translation/translation";
32 33
 
33 34
 /**
34 35
  * Tries to push history state with the following parameters:
@@ -95,6 +96,7 @@ const APP = {
95 96
     UI,
96 97
     settings,
97 98
     conference,
99
+    translation,
98 100
     /**
99 101
      * After the APP has been initialized provides utility methods for dealing
100 102
      * with the conference room URL(address).
@@ -106,7 +108,6 @@ const APP = {
106 108
     init () {
107 109
         this.keyboardshortcut =
108 110
             require("./modules/keyboardshortcut/keyboardshortcut");
109
-        this.translation = require("./modules/translation/translation");
110 111
         this.configFetch = require("./modules/config/HttpConfigFetch");
111 112
         this.tokenData = getTokenData();
112 113
     }

+ 0
- 1
lang/languages.json 查看文件

@@ -1,6 +1,5 @@
1 1
 {
2 2
     "en": "English",
3
-
4 3
     "bg": "Bulgarian",
5 4
     "de": "German",
6 5
     "es": "Spanish",

+ 3
- 0
modules/UI/side_pannels/settings/SettingsMenu.js 查看文件

@@ -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

+ 51
- 70
modules/translation/translation.js 查看文件

@@ -1,15 +1,16 @@
1 1
 /* global $, require, config, interfaceConfig */
2
-var i18n = require("i18next-client");
3
-var languages = require("../../service/translation/languages");
4
-var DEFAULT_LANG = languages.EN;
2
+import i18n from 'i18next';
3
+import XHR from 'i18next-xhr-backend';
4
+import jqueryI18next from 'jquery-i18next';
5
+import languagesR from "../../lang/languages.json";
6
+import mainR from "../../lang/main.json";
7
+import languages from "../../service/translation/languages";
5 8
 
6
-i18n.addPostProcessor(
7
-    "resolveAppName",
8
-    value => value.replace("__app__", interfaceConfig.APP_NAME));
9
+const DEFAULT_LANG = languages.EN;
9 10
 
10
-var defaultOptions = {
11
-    detectLngQS: "lang",
12
-    useCookie: false,
11
+const defaultOptions = {
12
+    compatibilityAPI: 'v1',
13
+    compatibilityJSON: 'v1',
13 14
     fallbackLng: DEFAULT_LANG,
14 15
     load: "unspecific",
15 16
     resGetPath: 'lang/__ns__-__lng__.json',
@@ -21,48 +22,11 @@ var defaultOptions = {
21 22
     fallbackOnNull: true,
22 23
     fallbackOnEmpty: true,
23 24
     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
25
+    app: interfaceConfig.APP_NAME
62 26
 };
63 27
 
64 28
 function initCompleted() {
65
-    $("[data-i18n]").i18n();
29
+    $("[data-i18n]").localize();
66 30
 }
67 31
 
68 32
 function getLangFromQuery() {
@@ -78,8 +42,8 @@ function getLangFromQuery() {
78 42
     return null;
79 43
 }
80 44
 
81
-module.exports = {
82
-    init: function (settingsLang) {
45
+class Translation {
46
+    init (settingsLang) {
83 47
         let options = defaultOptions;
84 48
 
85 49
         let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
@@ -98,31 +62,48 @@ module.exports = {
98 62
             options.lng = lang;
99 63
         }
100 64
 
101
-        i18n.init(options, initCompleted);
102
-    },
103
-    setLanguage: function (lang) {
65
+        i18n.use(XHR)
66
+            .use({
67
+                type: 'postProcessor',
68
+                name: "resolveAppName",
69
+                process: (res, key) => {
70
+                    return i18n.t(key, {app: options.app});
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});
79
+    }
80
+
81
+    setLanguage (lang) {
104 82
         if(!lang)
105 83
             lang = DEFAULT_LANG;
106 84
         i18n.setLng(lang, defaultOptions, initCompleted);
107
-    },
108
-    getCurrentLanguage: function () {
85
+    }
86
+
87
+    getCurrentLanguage () {
109 88
         return i18n.lng();
110
-    },
111
-    translateElement: function (selector, options) {
89
+    }
90
+
91
+    translateElement (selector, options) {
112 92
         // i18next expects undefined if options are missing, check if its null
113
-        selector.i18n(
93
+        selector.localize(
114 94
             options === null ? undefined : options);
115
-    },
116
-    generateTranslationHTML: function (key, options) {
117
-        var str = "<span data-i18n=\"" + key + "\"";
118
-        if (options) {
119
-            str += " data-i18n-options='" + JSON.stringify(options) + "'";
120
-        }
121
-        str += ">";
122
-        // i18next expects undefined if options ARE missing, check if its null
123
-        str += i18n.t(key, options === null ? undefined : options);
124
-        str += "</span>";
125
-        return str;
95
+    }
126 96
 
97
+    generateTranslationHTML (key, options) {
98
+        let optAttr = options
99
+            ? ` data-i18n-options='${JSON.stringify(options)}'` : "";
100
+        let text = i18n.t(key, options === null ? undefined : options);
101
+        return `<span data-i18n="${key}"${optAttr}>${text}</span>`;
127 102
     }
128
-};
103
+
104
+    addLanguageChangedListener(listener) {
105
+        i18n.on('languageChanged', listener);
106
+    }
107
+}
108
+
109
+export default new Translation();

+ 4
- 1
package.json 查看文件

@@ -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": "*",

+ 1
- 1
service/translation/languages.js 查看文件

@@ -1,4 +1,4 @@
1
-module.exports = {
1
+export default {
2 2
     getLanguages : function () {
3 3
         var languages = [];
4 4
         for (var lang in this)

+ 5
- 0
webpack.config.js 查看文件

@@ -58,6 +58,11 @@ var config = {
58 58
                 name: '[path][name].[ext]'
59 59
             },
60 60
             test: /\.(gif|png|svg)$/
61
+        },{
62
+            //Adds the ability to import json files.
63
+            loader: 'json',
64
+            exclude: __dirname + '/node_modules/',
65
+            test: /\.json$/
61 66
         }],
62 67
         noParse: [
63 68
             // Do not parse the files of the Strophe.js library or at least

Loading…
取消
儲存