Browse Source

Merge pull request #1069 from jitsi/update-i18next

Updates i18next and bundles default translations.
j8
hristoterezov 8 years ago
parent
commit
f08e9afc19

+ 2
- 1
app.js View File

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

+ 0
- 1
lang/languages.json View File

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

+ 3
- 0
modules/UI/side_pannels/settings/SettingsMenu.js View File

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

+ 51
- 70
modules/translation/translation.js View File

1
 /* global $, require, config, interfaceConfig */
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
     fallbackLng: DEFAULT_LANG,
14
     fallbackLng: DEFAULT_LANG,
14
     load: "unspecific",
15
     load: "unspecific",
15
     resGetPath: 'lang/__ns__-__lng__.json',
16
     resGetPath: 'lang/__ns__-__lng__.json',
21
     fallbackOnNull: true,
22
     fallbackOnNull: true,
22
     fallbackOnEmpty: true,
23
     fallbackOnEmpty: true,
23
     useDataAttrOptions: true,
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
 function initCompleted() {
28
 function initCompleted() {
65
-    $("[data-i18n]").i18n();
29
+    $("[data-i18n]").localize();
66
 }
30
 }
67
 
31
 
68
 function getLangFromQuery() {
32
 function getLangFromQuery() {
78
     return null;
42
     return null;
79
 }
43
 }
80
 
44
 
81
-module.exports = {
82
-    init: function (settingsLang) {
45
+class Translation {
46
+    init (settingsLang) {
83
         let options = defaultOptions;
47
         let options = defaultOptions;
84
 
48
 
85
         let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
49
         let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
98
             options.lng = lang;
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
         if(!lang)
82
         if(!lang)
105
             lang = DEFAULT_LANG;
83
             lang = DEFAULT_LANG;
106
         i18n.setLng(lang, defaultOptions, initCompleted);
84
         i18n.setLng(lang, defaultOptions, initCompleted);
107
-    },
108
-    getCurrentLanguage: function () {
85
+    }
86
+
87
+    getCurrentLanguage () {
109
         return i18n.lng();
88
         return i18n.lng();
110
-    },
111
-    translateElement: function (selector, options) {
89
+    }
90
+
91
+    translateElement (selector, options) {
112
         // i18next expects undefined if options are missing, check if its null
92
         // i18next expects undefined if options are missing, check if its null
113
-        selector.i18n(
93
+        selector.localize(
114
             options === null ? undefined : options);
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 View File

21
     "autosize": "^1.18.13",
21
     "autosize": "^1.18.13",
22
     "bootstrap": "3.1.1",
22
     "bootstrap": "3.1.1",
23
     "events": "*",
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
     "jQuery-Impromptu": "trentrichardson/jQuery-Impromptu#v6.0.0",
28
     "jQuery-Impromptu": "trentrichardson/jQuery-Impromptu#v6.0.0",
26
     "jquery": "~2.1.1",
29
     "jquery": "~2.1.1",
27
     "jquery-contextmenu": "*",
30
     "jquery-contextmenu": "*",

+ 1
- 1
service/translation/languages.js View File

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

+ 5
- 0
webpack.config.js View File

58
                 name: '[path][name].[ext]'
58
                 name: '[path][name].[ext]'
59
             },
59
             },
60
             test: /\.(gif|png|svg)$/
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
         noParse: [
67
         noParse: [
63
             // Do not parse the files of the Strophe.js library or at least
68
             // Do not parse the files of the Strophe.js library or at least

Loading…
Cancel
Save