From 35a828bfb6290e19ec07fd1064f198cff8a85df8 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 12 Oct 2015 17:11:46 -0700 Subject: [PATCH] GUAC-1345: Do not attempt to retrieve translation files for languages which are not explicitly listed as available by the REST API. --- .../app/locale/services/translationLoader.js | 63 +++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/guacamole/src/main/webapp/app/locale/services/translationLoader.js b/guacamole/src/main/webapp/app/locale/services/translationLoader.js index ec05ef540..a95f85f1b 100644 --- a/guacamole/src/main/webapp/app/locale/services/translationLoader.js +++ b/guacamole/src/main/webapp/app/locale/services/translationLoader.js @@ -29,9 +29,10 @@ angular.module('locale').factory('translationLoader', ['$injector', function translationLoader($injector) { // Required services - var $http = $injector.get('$http'); - var $q = $injector.get('$q'); - var cacheService = $injector.get('cacheService'); + var $http = $injector.get('$http'); + var $q = $injector.get('$q'); + var cacheService = $injector.get('cacheService'); + var languageService = $injector.get('languageService'); /** * Satisfies a translation request for the given key by searching for the @@ -62,22 +63,48 @@ angular.module('locale').factory('translationLoader', ['$injector', function tra return; } - // Attempt to retrieve language - $http({ - cache : cacheService.languages, - method : 'GET', - url : 'translations/' + encodeURIComponent(currentKey) + '.json' - }) - - // Resolve promise if translation retrieved successfully - .success(function translationFileRetrieved(translation) { - deferred.resolve(translation); - }) - - // Retry with remaining languages if translation file could not be retrieved - .error(function translationFileUnretrievable() { + /** + * Continues trying possible translation files until no possibilities + * exist. + * + * @private + */ + var tryNextTranslation = function tryNextTranslation() { satisfyTranslation(deferred, requestedKey, remainingKeys); - }); + }; + + // Retrieve list of supported languages + languageService.getLanguages() + + // Attempt to retrieve translation if language is supported + .success(function retrievedLanguages(languages) { + + // Skip retrieval if language is not supported + if (!(currentKey in languages)) { + tryNextTranslation(); + return; + } + + // Attempt to retrieve language + $http({ + cache : cacheService.languages, + method : 'GET', + url : 'translations/' + encodeURIComponent(currentKey) + '.json' + }) + + // Resolve promise if translation retrieved successfully + .success(function translationFileRetrieved(translation) { + deferred.resolve(translation); + }) + + // Retry with remaining languages if translation file could not be + // retrieved + .error(tryNextTranslation); + + }) + + // Retry with remaining languages if translation does not exist + .error(tryNextTranslation); };