浏览代码

Moves translation to react and use i18next language detectors.

master
damencho 8 年前
父节点
当前提交
d861ba1876

+ 0
- 1
modules/settings/Settings.js 查看文件

118
     },
118
     },
119
     setLanguage: function (lang) {
119
     setLanguage: function (lang) {
120
         language = lang;
120
         language = lang;
121
-        jitsiLocalStorage.setItem("language", lang);
122
     },
121
     },
123
 
122
 
124
     /**
123
     /**

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

1
-/* global $, require, config, interfaceConfig */
2
-import i18n from 'i18next';
3
-import XHR from 'i18next-xhr-backend';
1
+/* global $ */
2
+import { i18n, DEFAULT_LANG } from '../../react/features/base/translation';
4
 import jqueryI18next from 'jquery-i18next';
3
 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";
8
-
9
-const DEFAULT_LANG = languages.EN;
10
-
11
-const defaultOptions = {
12
-    compatibilityAPI: 'v1',
13
-    compatibilityJSON: 'v1',
14
-    fallbackLng: DEFAULT_LANG,
15
-    load: "unspecific",
16
-    resGetPath: 'lang/__ns__-__lng__.json',
17
-    ns: {
18
-        namespaces: ['main', 'languages'],
19
-        defaultNs: 'main'
20
-    },
21
-    lngWhitelist : languages.getLanguages(),
22
-    fallbackOnNull: true,
23
-    fallbackOnEmpty: true,
24
-    useDataAttrOptions: true,
25
-    app: interfaceConfig.APP_NAME
26
-};
27
 
4
 
28
 function initCompleted() {
5
 function initCompleted() {
29
     $("[data-i18n]").localize();
6
     $("[data-i18n]").localize();
30
 }
7
 }
31
 
8
 
32
-function getLangFromQuery() {
33
-    var query = window.location.search.substring(1);
34
-    var vars = query.split("&");
35
-    for (var i=0;i<vars.length;i++) {
36
-        var pair = vars[i].split("=");
37
-        if(pair[0] == "lang")
38
-        {
39
-            return pair[1];
40
-        }
41
-    }
42
-    return null;
43
-}
44
-
45
 class Translation {
9
 class Translation {
46
-    init (settingsLang) {
47
-        let options = defaultOptions;
48
-
49
-        let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
50
-        // XXX If none of the above has been set then the 'lang' will be
51
-        // 'undefined' and the i18n lib will try to auto detect user's
52
-        // preferred language based on browser's locale.
53
-        // The interface config option allows to disable this auto detection
54
-        // by specifying the fallback language in that case.
55
-        let langDetection = interfaceConfig.LANG_DETECTION;
56
-
57
-        if (!langDetection && !lang) {
58
-            lang = DEFAULT_LANG;
59
-        }
60
-
61
-        if (lang) {
62
-            options.lng = lang;
63
-        }
10
+    init () {
11
+        if (i18n.isInitialized)
12
+            initCompleted();
13
+        else
14
+            i18n.on('initialized', initCompleted);
64
 
15
 
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});
16
         jqueryI18next.init(i18n, $, {useOptionsAttr: true});
79
     }
17
     }
80
 
18
 
81
     setLanguage (lang) {
19
     setLanguage (lang) {
82
         if(!lang)
20
         if(!lang)
83
             lang = DEFAULT_LANG;
21
             lang = DEFAULT_LANG;
84
-        i18n.setLng(lang, defaultOptions, initCompleted);
22
+        i18n.setLng(lang, {}, initCompleted);
85
     }
23
     }
86
 
24
 
87
     getCurrentLanguage () {
25
     getCurrentLanguage () {

+ 3
- 2
package.json 查看文件

20
     "async": "0.9.0",
20
     "async": "0.9.0",
21
     "autosize": "^1.18.13",
21
     "autosize": "^1.18.13",
22
     "bootstrap": "3.1.1",
22
     "bootstrap": "3.1.1",
23
-    "i18next": "3.4.4",
24
-    "i18next-xhr-backend": "1.1.0",
23
+    "i18next": "7.0.0",
24
+    "i18next-browser-languagedetector": "*",
25
+    "i18next-xhr-backend": "1.3.0",
25
     "jitsi-meet-logger": "jitsi/jitsi-meet-logger",
26
     "jitsi-meet-logger": "jitsi/jitsi-meet-logger",
26
     "jquery": "~2.1.1",
27
     "jquery": "~2.1.1",
27
     "jquery-contextmenu": "*",
28
     "jquery-contextmenu": "*",

+ 25
- 0
react/features/base/translation/ConfigLanguageDetector.js 查看文件

1
+/* global config */
2
+
3
+/**
4
+ * Custom language detection, just returns the config property if any.
5
+ */
6
+export default {
7
+    /**
8
+     * Name of the language detector.
9
+     */
10
+    name: 'configLanguageDetector',
11
+
12
+    /**
13
+     * The actual lookup.
14
+     *
15
+     * @returns {string} the default language if any.
16
+     */
17
+    lookup() {
18
+        return config.defaultLanguage;
19
+    },
20
+
21
+    /**
22
+     * Doesn't support caching.
23
+     */
24
+    cacheUserLanguage: Function.prototype
25
+};

+ 72
- 0
react/features/base/translation/Translation.js 查看文件

1
+/* global interfaceConfig */
2
+import i18n from 'i18next';
3
+import XHR from 'i18next-xhr-backend';
4
+import { DEFAULT_LANG, languages } from './constants';
5
+import languagesR from '../../../../lang/languages.json';
6
+import mainR from '../../../../lang/main.json';
7
+import Browser from 'i18next-browser-languagedetector';
8
+import ConfigLanguageDetector from './ConfigLanguageDetector';
9
+
10
+/**
11
+ * Default options to initialize i18next.
12
+ *
13
+ * @enum {string}
14
+ */
15
+const defaultOptions = {
16
+    compatibilityAPI: 'v1',
17
+    compatibilityJSON: 'v1',
18
+    fallbackLng: DEFAULT_LANG,
19
+    load: 'unspecific',
20
+    resGetPath: 'lang/__ns__-__lng__.json',
21
+    ns: {
22
+        namespaces: [ 'main', 'languages' ],
23
+        defaultNs: 'main'
24
+    },
25
+    lngWhitelist: languages.getLanguages(),
26
+    fallbackOnNull: true,
27
+    fallbackOnEmpty: true,
28
+    useDataAttrOptions: true,
29
+    app: interfaceConfig.APP_NAME
30
+};
31
+
32
+/**
33
+ * List of detectors to use in their order.
34
+ *
35
+ * @type {[*]}
36
+ */
37
+const detectors = [ 'querystring', 'localStorage', 'configLanguageDetector' ];
38
+
39
+/**
40
+ * Allow i18n to detect the system language from the browser.
41
+ */
42
+if (interfaceConfig.LANG_DETECTION) {
43
+    detectors.push('navigator');
44
+}
45
+
46
+/**
47
+ * The language detectors.
48
+ */
49
+const browser = new Browser(null, {
50
+    order: detectors,
51
+    lookupQuerystring: 'lang',
52
+    lookupLocalStorage: 'language',
53
+    caches: [ 'localStorage' ]
54
+});
55
+
56
+// adds a language detector that just checks the config
57
+browser.addDetector(ConfigLanguageDetector);
58
+
59
+i18n.use(XHR)
60
+    .use(browser)
61
+    .use({
62
+        type: 'postProcessor',
63
+        name: 'resolveAppName',
64
+        process: (res, key) => i18n.t(key, { app: defaultOptions.app })
65
+    })
66
+    .init(defaultOptions);
67
+
68
+// adds default language which is preloaded from code
69
+i18n.addResourceBundle(DEFAULT_LANG, 'main', mainR, true, true);
70
+i18n.addResourceBundle(DEFAULT_LANG, 'languages', languagesR, true, true);
71
+
72
+export default i18n;

+ 13
- 0
react/features/base/translation/constants.js 查看文件

1
+import languages from '../../../../service/translation/languages';
2
+
3
+/**
4
+ * The default language globally for the project.
5
+ *
6
+ * @type {string} the default language globally for the project.
7
+ */
8
+export const DEFAULT_LANG = languages.EN;
9
+
10
+/**
11
+ * Exports the list of languages currently supported.
12
+ */
13
+export { languages };

+ 2
- 0
react/features/base/translation/index.js 查看文件

1
+export { default as i18n } from './Translation';
2
+export * from './constants';

正在加载...
取消
保存