From d9ca568b9f5297163a280845e7538e759c4d848e Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 17 Jul 2016 01:16:17 +0800 Subject: [PATCH] optimize language resource loading --- bower.json | 1 - src/index.html | 2 +- src/langs/zh_CN.json | 1 - src/scripts/config/configuration.js | 10 +++--- src/scripts/config/constants.js | 4 +++ src/scripts/config/defaultLanguage.js | 11 +++--- src/scripts/config/languages.js | 6 ++-- src/scripts/services/ariaNgLanguageLoader.js | 35 ++++++++++++++++++++ src/views/settings-ariang.html | 2 +- 9 files changed, 55 insertions(+), 17 deletions(-) create mode 100644 src/scripts/services/ariaNgLanguageLoader.js diff --git a/bower.json b/bower.json index ccd2da6..53b6c7d 100644 --- a/bower.json +++ b/bower.json @@ -39,7 +39,6 @@ "angular-cookies": "1.4.10", "angular-animate": "1.4.10", "angular-translate": "^2.11.0", - "angular-translate-loader-static-files": "^2.11.0", "angular-moment": "1.0.0-beta.6", "angular-websocket": "^1.1.0", "angular-utf8-base64": "^0.0.5", diff --git a/src/index.html b/src/index.html index 23d5372..c1b733e 100644 --- a/src/index.html +++ b/src/index.html @@ -271,7 +271,6 @@ - @@ -322,6 +321,7 @@ + diff --git a/src/langs/zh_CN.json b/src/langs/zh_CN.json index 489eda3..02e64eb 100644 --- a/src/langs/zh_CN.json +++ b/src/langs/zh_CN.json @@ -1,5 +1,4 @@ { - "Simplified Chinese": "简体中文", "Operation Succeeded": "操作成功", "Connection Succeeded": "连接成功", "Error": "错误", diff --git a/src/scripts/config/configuration.js b/src/scripts/config/configuration.js index 6dd7a36..093357c 100644 --- a/src/scripts/config/configuration.js +++ b/src/scripts/config/configuration.js @@ -7,12 +7,10 @@ .setStorageType('localStorage') .setStorageCookie(365, '/'); - $translateProvider.useStaticFilesLoader({ - prefix: 'langs/', - suffix: '.json' - }).useLoaderCache(true) - .preferredLanguage('en') - .fallbackLanguage('en') + $translateProvider.useLoader('ariaNgLanguageLoader') + .useLoaderCache(true) + .preferredLanguage(ariaNgConstants.defaultLanguage) + .fallbackLanguage(ariaNgConstants.defaultLanguage) .useSanitizeValueStrategy('escapeParameters'); NotificationProvider.setOptions({ diff --git a/src/scripts/config/constants.js b/src/scripts/config/constants.js index c6aeada..23b506e 100644 --- a/src/scripts/config/constants.js +++ b/src/scripts/config/constants.js @@ -5,6 +5,10 @@ title: 'AriaNg', appPrefix: 'AriaNg', optionStorageKey: 'Options', + languageStorageKeyPrefix: 'Language', + languagePath: '/langs', + languageFileExtension: '.json', + defaultLanguage: 'en', globalStatStorageCapacity: 120, taskStatStorageCapacity: 300, lazySaveTimeout: 500, diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index b7a921e..8824f1f 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -1,9 +1,8 @@ (function () { 'use strict'; - angular.module('ariaNg').config(['$translateProvider', function ($translateProvider) { - $translateProvider.translations('en', { - 'English': 'English', + angular.module('ariaNg').config(['$translateProvider', 'ariaNgConstants', function ($translateProvider, ariaNgConstants) { + var defaultLanguageResource = { 'Operation Succeeded': 'Operation Succeeded', 'Connection Succeeded': 'Connection Succeeded', 'Error': 'Error', @@ -106,7 +105,7 @@ 'Manual Download': 'Manual Download', 'Support multiple URLs, one URL per line.': 'Support multiple URLs, one URL per line.', 'Your browser does not support loading file!': 'Your browser does not support loading file!', - 'The selected file type is invalid': 'The selected file type is invalid', + 'The selected file type is invalid!': 'The selected file type is invalid!', 'Failed to load file!': 'Failed to load file!', 'Download Completed': 'Download Completed', 'BT Download Completed': 'BT Download Completed', @@ -520,6 +519,8 @@ 'truncate-console-readout.name': 'Truncate Console Output', 'truncate-console-readout.description': 'Truncate console readout to fit in a single line.' } - }); + }; + + $translateProvider.translations(ariaNgConstants.defaultLanguage, defaultLanguageResource); }]) })(); diff --git a/src/scripts/config/languages.js b/src/scripts/config/languages.js index cff2f85..b5a69e4 100644 --- a/src/scripts/config/languages.js +++ b/src/scripts/config/languages.js @@ -3,10 +3,12 @@ angular.module('ariaNg').constant('ariaNgLanguages', { 'en': { - name: 'English' + name: 'English', + displayName: 'English' }, 'zh_CN': { - name: 'Simplified Chinese' + name: 'Simplified Chinese', + displayName: '简体中文' } }); })(); diff --git a/src/scripts/services/ariaNgLanguageLoader.js b/src/scripts/services/ariaNgLanguageLoader.js new file mode 100644 index 0000000..3879899 --- /dev/null +++ b/src/scripts/services/ariaNgLanguageLoader.js @@ -0,0 +1,35 @@ +(function () { + 'use strict'; + + angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'localStorageService', 'ariaNgConstants', 'ariaNgLanguages', function ($http, $q, localStorageService, ariaNgConstants, ariaNgLanguages) { + return function (options) { + var deferred = $q.defer(); + + if (!ariaNgLanguages[options.key]) { + deferred.reject(options.key); + return deferred.promise; + } + + var languageKey = ariaNgConstants.languageStorageKeyPrefix + '.' + options.key; + var languageResource = localStorageService.get(languageKey); + + if (languageResource) { + deferred.resolve(languageResource); + } + + var languagePath = ariaNgConstants.languagePath + '/' + options.key + ariaNgConstants.languageFileExtension; + + $http({ + url: languagePath, + method: 'GET' + }).success(function (data) { + localStorageService.set(languageKey, data); + return deferred.resolve(data); + }).error(function (data) { + return deferred.reject(options.key); + }); + + return deferred.promise; + }; + }]); +})(); diff --git a/src/views/settings-ariang.html b/src/views/settings-ariang.html index f63a2e3..833d89a 100644 --- a/src/views/settings-ariang.html +++ b/src/views/settings-ariang.html @@ -6,7 +6,7 @@