thingsboard-aplcache

Detect the list of supported languages on a build stage. Use

7/14/2018 1:19:23 PM

Details

ui/package.json 3(+2 -1)

diff --git a/ui/package.json b/ui/package.json
index 22999e8..284f904 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -127,7 +127,8 @@
     "webpack-dev-middleware": "^1.6.1",
     "webpack-dev-server": "^1.15.1",
     "webpack-hot-middleware": "^2.12.2",
-    "webpack-material-design-icons": "^0.1.0"
+    "webpack-material-design-icons": "^0.1.0",
+    "directory-tree": "^2.1.0"
   },
   "engine": "node >= 5.9.0",
   "nyc": {
diff --git a/ui/src/app/app.config.js b/ui/src/app/app.config.js
index 982e9a9..ebd2e0a 100644
--- a/ui/src/app/app.config.js
+++ b/ui/src/app/app.config.js
@@ -43,20 +43,12 @@ export default function AppConfig($provide,
     
     $translateProvider.useSanitizeValueStrategy(null)
                       .useMissingTranslationHandler('tbMissingTranslationHandler')
-                     /* .useMissingTranslationHandlerLog() */
                       .addInterpolation('$translateMessageFormatInterpolation')
                       .useStaticFilesLoader({
                           prefix: PUBLIC_PATH + 'locale/locale.constant-', //eslint-disable-line
                           suffix: '.json'
                       })
-                      .registerAvailableLanguageKeys(['en', 'es', 'it', 'ko', 'ru', 'zh'], {
-                        'en_*': 'en',
-                        'es_*': 'es',
-                        'it_*': 'it',
-                        'ko_*': 'ko',
-                        'ru_*': 'ru',
-                        'zh_*': 'zh'
-                      })
+                      .registerAvailableLanguageKeys(SUPPORTED_LANGS, getLanguageAliases(SUPPORTED_LANGS)) //eslint-disable-line
                       .fallbackLanguage('en') // must be before determinePreferredLanguage   
                       .uniformLanguageTag('java')  // must be before determinePreferredLanguage
                       .determinePreferredLanguage();                
@@ -150,4 +142,22 @@ export default function AppConfig($provide,
         //$mdThemingProvider.alwaysWatchTheme(true);
     }
 
+    function getLanguageAliases(supportedLangs) {
+        var aliases = {};
+
+        supportedLangs.sort().forEach(function(item, index, array) {
+            if (item.length === 2) { 
+                aliases[item + '_*'] = item;
+            } else {
+                var key = item.slice(0, 2);
+                if (index === 0 || key !== array[index - 1].slice(0, 2)) {
+                    aliases[key + '_*'] = item;
+                } else {
+                    aliases[item] = item;
+                }
+            }
+        });
+        
+        return aliases;
+    }
 }
\ No newline at end of file
diff --git a/ui/src/app/profile/profile.controller.js b/ui/src/app/profile/profile.controller.js
index d17a65a..b947bde 100644
--- a/ui/src/app/profile/profile.controller.js
+++ b/ui/src/app/profile/profile.controller.js
@@ -24,16 +24,9 @@ export default function ProfileController(userService, $scope, $document, $mdDia
     var vm = this;
 
     vm.profileUser = {};
-
     vm.save = save;
     vm.changePassword = changePassword;
-    vm.languageList = {
-        en_US: {value : "en_US", name: "language.en_US"}, 
-        ko_KR: {value : "ko_KR", name: "language.ko_KR"},
-        zh_CN: {value : "zh_CN", name: "language.zh_CN"},
-        ru_RU: {value : "ru_RU", name: "language.ru_RU"},
-        es_ES: {value : "es_ES", name: "language.es_ES"},
-    };
+    vm.languageList = SUPPORTED_LANGS; //eslint-disable-line
 
     loadProfile();
 
diff --git a/ui/src/app/profile/profile.tpl.html b/ui/src/app/profile/profile.tpl.html
index a0358c1..58e4aac 100644
--- a/ui/src/app/profile/profile.tpl.html
+++ b/ui/src/app/profile/profile.tpl.html
@@ -43,8 +43,8 @@
                     <md-input-container class="md-block">
                         <label translate>language.language</label>
                         <md-select name="language" ng-model="vm.profileUser.additionalInfo.lang">
-                            <md-option ng-repeat="lang in vm.languageList" ng-value="lang.value">
-                                {{lang.name | translate}}
+                            <md-option ng-repeat="lang in vm.languageList" ng-value="lang">
+                                {{ 'language.locales.' + lang | translate}}
                             </md-option>
                         </md-select>
                     </md-input-container>
diff --git a/ui/webpack.config.dev.js b/ui/webpack.config.dev.js
index e0cc91e..1aa09ff 100644
--- a/ui/webpack.config.dev.js
+++ b/ui/webpack.config.dev.js
@@ -20,9 +20,17 @@ const ExtractTextPlugin = require('extract-text-webpack-plugin');
 const CopyWebpackPlugin = require('copy-webpack-plugin');
 const webpack = require('webpack');
 const path = require('path');
+const dirTree = require('directory-tree');
 
 const PUBLIC_RESOURCE_PATH = '/';
 
+var langs = [];
+dirTree('./src/app/locale/', {extensions:/\.json$/}, (item) => {
+    /* It is expected what the name of a locale file has the following format: */
+    /* 'locale.constant-LANG_CODE[_REGION_CODE].json', e.g. locale.constant-es.json or locale.constant-zh_CN.json*/
+    langs.push(item.name.slice(item.name.lastIndexOf('-') + 1, -5));
+});
+
 /* devtool: 'cheap-module-eval-source-map', */
 
 module.exports = {
@@ -68,7 +76,8 @@ module.exports = {
             'process.env': {
                 NODE_ENV: JSON.stringify('development'),
             },
-            PUBLIC_PATH: PUBLIC_RESOURCE_PATH
+            PUBLIC_PATH: PUBLIC_RESOURCE_PATH,
+            SUPPORTED_LANGS: JSON.stringify(langs)
         }),
     ],
     node: {
@@ -121,10 +130,6 @@ module.exports = {
                     'url?limit=8192',
                     'img?minimize'
                 ]
-            },
-            {
-                test: /\.json$/,
-                loader: 'json-loader'
             }
         ],
     },
diff --git a/ui/webpack.config.prod.js b/ui/webpack.config.prod.js
index f914f9c..bae09c2 100644
--- a/ui/webpack.config.prod.js
+++ b/ui/webpack.config.prod.js
@@ -21,9 +21,17 @@ const CopyWebpackPlugin = require('copy-webpack-plugin');
 const CompressionPlugin = require('compression-webpack-plugin');
 const webpack = require('webpack');
 const path = require('path');
+const dirTree = require('directory-tree');
 
 const PUBLIC_RESOURCE_PATH = '/static/';
 
+var langs = [];
+dirTree('./src/app/locale/', {extensions:/\.json$/}, (item) => {
+    /* It is expected what the name of a locale file has the following format: */
+    /* 'locale.constant-LANG_CODE[_REGION_CODE].json', e.g. locale.constant-es.json or locale.constant-zh_CN.json*/
+    langs.push(item.name.slice(item.name.lastIndexOf('-') + 1, -5));
+});
+
 module.exports = {
     devtool: 'source-map',
     entry: [
@@ -67,12 +75,13 @@ module.exports = {
             'process.env': {
                 NODE_ENV: JSON.stringify('production'),
             },
-            PUBLIC_PATH: PUBLIC_RESOURCE_PATH
+            PUBLIC_PATH: PUBLIC_RESOURCE_PATH,
+            SUPPORTED_LANGS: JSON.stringify(langs)
         }),
         new CompressionPlugin({
             asset: "[path].gz[query]",
             algorithm: "gzip",
-            test: /\.js$|\.css$|\.svg$|\.ttf$|\.woff$|\.woff2|\.eot$/,
+            test: /\.js$|\.css$|\.svg$|\.ttf$|\.woff$|\.woff2|\.eot$\.json$/,
             threshold: 10240,
             minRatio: 0.8
         })
@@ -127,10 +136,6 @@ module.exports = {
                     'url?limit=8192',
                     'img?minimize'
                 ]
-            },
-            {
-                test: /\.json$/,
-                loader: 'json-loader'
             }
         ],
     },