瀏覽代碼

feat(translation): ES6 support

j8
hristoterezov 8 年之前
父節點
當前提交
e731f6c3f3
共有 5 個文件被更改,包括 40 次插入34 次删除
  1. 2
    1
      app.js
  2. 0
    1
      lang/languages.json
  3. 32
    31
      modules/translation/translation.js
  4. 1
    1
      service/translation/languages.js
  5. 5
    0
      webpack.config.js

+ 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",

+ 32
- 31
modules/translation/translation.js 查看文件

@@ -2,12 +2,13 @@
2 2
 import i18n from 'i18next';
3 3
 import XHR from 'i18next-xhr-backend';
4 4
 import jqueryI18next from 'jquery-i18next';
5
-var languages = require("../../service/translation/languages");
6
-var languagesR = require("json!../../lang/languages.json");
7
-var mainR = require("json!../../lang/main.json");
8
-var DEFAULT_LANG = languages.EN;
5
+import languagesR from "../../lang/languages.json";
6
+import mainR from "../../lang/main.json";
7
+import languages from "../../service/translation/languages";
9 8
 
10
-var defaultOptions = {
9
+const DEFAULT_LANG = languages.EN;
10
+
11
+const defaultOptions = {
11 12
     compatibilityAPI: 'v1',
12 13
     compatibilityJSON: 'v1',
13 14
     fallbackLng: DEFAULT_LANG,
@@ -41,8 +42,8 @@ function getLangFromQuery() {
41 42
     return null;
42 43
 }
43 44
 
44
-module.exports = {
45
-    init: function (settingsLang) {
45
+class Translation {
46
+    init (settingsLang) {
46 47
         let options = defaultOptions;
47 48
 
48 49
         let lang = getLangFromQuery() || settingsLang || config.defaultLanguage;
@@ -65,10 +66,9 @@ module.exports = {
65 66
             .use({
66 67
                 type: 'postProcessor',
67 68
                 name: "resolveAppName",
68
-                process:
69
-                    function (res, key) {
70
-                        return i18n.t(key, {app: interfaceConfig.APP_NAME});
71
-                    }
69
+                process: (res, key) => {
70
+                    return i18n.t(key, {app: options.app});
71
+                }
72 72
             })
73 73
             .init(options, initCompleted);
74 74
         // adds default language which is preloaded from code
@@ -76,33 +76,34 @@ module.exports = {
76 76
         i18n.addResourceBundle(
77 77
             DEFAULT_LANG, 'languages', languagesR, true, true);
78 78
         jqueryI18next.init(i18n, $, {useOptionsAttr: true});
79
-    },
80
-    setLanguage: function (lang) {
79
+    }
80
+
81
+    setLanguage (lang) {
81 82
         if(!lang)
82 83
             lang = DEFAULT_LANG;
83 84
         i18n.setLng(lang, defaultOptions, initCompleted);
84
-    },
85
-    getCurrentLanguage: function () {
85
+    }
86
+
87
+    getCurrentLanguage () {
86 88
         return i18n.lng();
87
-    },
88
-    translateElement: function (selector, options) {
89
+    }
90
+
91
+    translateElement (selector, options) {
89 92
         // i18next expects undefined if options are missing, check if its null
90 93
         selector.localize(
91 94
             options === null ? undefined : options);
92
-    },
93
-    generateTranslationHTML: function (key, options) {
94
-        var str = "<span data-i18n=\"" + key + "\"";
95
-        if (options) {
96
-            str += " data-i18n-options='" + JSON.stringify(options) + "'";
97
-        }
98
-        str += ">";
99
-        // i18next expects undefined if options ARE missing, check if its null
100
-        str += i18n.t(key, options === null ? undefined : options);
101
-        str += "</span>";
102
-        return str;
95
+    }
103 96
 
104
-    },
105
-    addLanguageChangedListener: function(listener) {
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>`;
102
+    }
103
+
104
+    addLanguageChangedListener(listener) {
106 105
         i18n.on('languageChanged', listener);
107 106
     }
108
-};
107
+}
108
+
109
+export default new Translation();

+ 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…
取消
儲存