From 7625951de2474049be78294fa129c32ea46df6a9 Mon Sep 17 00:00:00 2001 From: marek Date: Thu, 12 Jun 2014 12:20:19 +0200 Subject: [PATCH] fix($translate): return $missingTranslationHandler result when no translation was found Instant translations do not return result of $missingTranslationHandler invocation. --- src/service/translate.js | 77 ++++++++++++++++++----------- test/unit/service/translate.spec.js | 30 +++++++++++ 2 files changed, 79 insertions(+), 28 deletions(-) diff --git a/src/service/translate.js b/src/service/translate.js index 95170f9e1..621fd9453 100644 --- a/src/service/translate.js +++ b/src/service/translate.js @@ -1019,6 +1019,32 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', return result; }; + + /** + * @name translateByHandler + * @private + * + * Translate by missing translation handler. + * + * @param translationId + * @returns translation created by $missingTranslationHandler or translationId is $missingTranslationHandler is + * absent + */ + var translateByHandler = function (translationId) { + // If we have a handler factory - we might also call it here to determine if it provides + // a default text for a translationid that can't be found anywhere in our tables + if ($missingTranslationHandlerFactory) { + var resultString = $injector.get($missingTranslationHandlerFactory)(translationId, $uses); + if (resultString !== undefined) { + return resultString; + } else { + return translationId; + } + } else { + return translationId; + } + }; + /** * @name resolveForFallbackLanguage * @private @@ -1050,19 +1076,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', ); } else { // No translation found in any fallback language - - // If we have a handler factory - we might also call it here to determine if it provides - // a default text for a translationid that can't be found anywhere in our tables - if ($missingTranslationHandlerFactory) { - var resultString = $injector.get($missingTranslationHandlerFactory)(translationId, $uses); - if (resultString !== undefined) { - deferred.resolve(resultString); - } else { - deferred.resolve(translationId); - } - } else { - deferred.resolve(translationId); - } + deferred.resolve(translateByHandler(translationId)); } return deferred.promise; }; @@ -1139,11 +1153,10 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', deferred.resolve(Interpolator.interpolate(translation, interpolateParams)); } } else { - // looks like the requested translation id doesn't exists. - // Now, if there is a registered handler for missing translations and no - // asyncLoader is pending, we execute the handler + var missingTranslationHandlerTranslation; + // for logging purposes only (as in $translateMissingTranslationHandlerLog), value is not returned to promise if ($missingTranslationHandlerFactory && !pendingLoader) { - $injector.get($missingTranslationHandlerFactory)(translationId, $uses); + missingTranslationHandlerTranslation = translateByHandler(translationId); } // since we couldn't translate the inital requested translation id, @@ -1151,12 +1164,16 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', // configured. if ($uses && $fallbackLanguage && $fallbackLanguage.length) { fallbackTranslation(translationId, interpolateParams, Interpolator) - .then(function (translation) { - deferred.resolve(translation); - }, function (_translationId) { - deferred.reject(applyNotFoundIndicators(_translationId)); - }); - + .then(function (translation) { + deferred.resolve(translation); + }, function (_translationId) { + deferred.reject(applyNotFoundIndicators(_translationId)); + }); + } else if ($missingTranslationHandlerFactory && !pendingLoader && missingTranslationHandlerTranslation) { + // looks like the requested translation id doesn't exists. + // Now, if there is a registered handler for missing translations and no + // asyncLoader is pending, we execute the handler + deferred.resolve(missingTranslationHandlerTranslation); } else { deferred.reject(applyNotFoundIndicators(translationId)); } @@ -1180,11 +1197,10 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', result = Interpolator.interpolate(translation, interpolateParams); } } else { - // looks like the requested translation id doesn't exists. - // Now, if there is a registered handler for missing translations and no - // asyncLoader is pending, we execute the handler + var missingTranslationHandlerTranslation; + // for logging purposes only (as in $translateMissingTranslationHandlerLog), value is not returned to promise if ($missingTranslationHandlerFactory && !pendingLoader) { - $injector.get($missingTranslationHandlerFactory)(translationId, $uses); + missingTranslationHandlerTranslation = translateByHandler(translationId); } // since we couldn't translate the inital requested translation id, @@ -1193,6 +1209,11 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', if ($uses && $fallbackLanguage && $fallbackLanguage.length) { fallbackIndex = 0; result = fallbackTranslationInstant(translationId, interpolateParams, Interpolator); + } else if ($missingTranslationHandlerFactory && !pendingLoader && missingTranslationHandlerTranslation) { + // looks like the requested translation id doesn't exists. + // Now, if there is a registered handler for missing translations and no + // asyncLoader is pending, we execute the handler + result = missingTranslationHandlerTranslation; } else { result = applyNotFoundIndicators(translationId); } @@ -1577,7 +1598,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY', // Return translation if not found anything. result = translationId; if ($missingTranslationHandlerFactory && !pendingLoader) { - $injector.get($missingTranslationHandlerFactory)(translationId, $uses); + result = translateByHandler(translationId); } } diff --git a/test/unit/service/translate.spec.js b/test/unit/service/translate.spec.js index 635520aa4..d1d59ad86 100644 --- a/test/unit/service/translate.spec.js +++ b/test/unit/service/translate.spec.js @@ -557,6 +557,36 @@ describe('pascalprecht.translate', function () { }); }); + describe('translate returns handler result', function () { + + beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) { + $translateProvider + .translations('de_DE', translationMock) + .preferredLanguage('de_DE'); + + // factory provides a default fallback text being defined in the factory + // gives a maximum of flexibility + $provide.factory('elementReturningTranslationHandler', function () { + return function (translationID, uses) { + return '' + translationID + ''; + }; + }); + $translateProvider.useMissingTranslationHandler('elementReturningTranslationHandler'); + })); + + var $translate, $rootScope, $q; + + beforeEach(inject(function (_$translate_, _$rootScope_, _$q_) { + $translate = _$translate_; + $rootScope = _$rootScope_; + $q = _$q_; + })); + + it('when instant translate', function () { + expect($translate.instant('false.key')).toBe('false.key'); + }); + }); + describe('multi fallback language', function () { beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {