Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
fix(service): reject promise if handler returns undefined
Browse files Browse the repository at this point in the history
Service rejects the promise if translation doesn't exist and handler returns undefined

Closes #1600
  • Loading branch information
JaZo authored and knalli committed Oct 30, 2016
1 parent 40f9e35 commit 8fe6f23
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
29 changes: 17 additions & 12 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1429,12 +1429,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
// 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, interpolateParams, defaultTranslationText, sanitizeStrategy);
if (resultString !== undefined) {
return resultString;
} else {
return translationId;
}
return $injector.get($missingTranslationHandlerFactory)(translationId, $uses, interpolateParams, defaultTranslationText, sanitizeStrategy);
} else {
return translationId;
}
Expand Down Expand Up @@ -1474,14 +1469,15 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
if (defaultTranslationText) {
deferred.resolve(defaultTranslationText);
} else {
var missingTranslationHandlerTranslation = translateByHandler(translationId, interpolateParams, defaultTranslationText);

// if no default translation is set and an error handler is defined, send it to the handler
// and then return the result
if ($missingTranslationHandlerFactory) {
deferred.resolve(translateByHandler(translationId, interpolateParams));
// and then return the result if it isn't undefined
if ($missingTranslationHandlerFactory && missingTranslationHandlerTranslation) {
deferred.resolve(missingTranslationHandlerTranslation);
} else {
deferred.reject(translateByHandler(translationId, interpolateParams));
deferred.reject(applyNotFoundIndicators(translationId));
}

}
}
return deferred.promise;
Expand Down Expand Up @@ -2179,8 +2175,17 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
} else {
// Return translation of default interpolator if not found anything.
result = defaultInterpolator.interpolate(translationId, interpolateParams, 'filter', sanitizeStrategy);

// 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;
if ($missingTranslationHandlerFactory && !pendingLoader) {
result = translateByHandler(translationId, interpolateParams, sanitizeStrategy);
missingTranslationHandlerTranslation = translateByHandler(translationId, interpolateParams, sanitizeStrategy);
}

if ($missingTranslationHandlerFactory && !pendingLoader && missingTranslationHandlerTranslation) {
result = missingTranslationHandlerTranslation;
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2018,10 +2018,12 @@ describe('pascalprecht.translate', function () {

}));

var $translate;
var $translate,
$rootScope;

beforeEach(inject(function (_$translate_) {
beforeEach(inject(function (_$translate_, _$rootScope_) {
$translate = _$translate_;
$rootScope = _$rootScope_;
}));

it('should not invoke missingTranslationHandler if translation id exists', function () {
Expand Down Expand Up @@ -2065,6 +2067,25 @@ describe('pascalprecht.translate', function () {
}
});
});

it('should reject promise if translation doesn\'t exist and handler returns undefined', function () {
var wasResolved = false,
wasRejected = false,
promise = $translate('NOT_EXISTING_TRANSLATION_ID');
promise.then(resolved, rejected);

$rootScope.$digest();
expect(wasResolved).toEqual(false);
expect(wasRejected).toEqual(true);

function resolved() {
wasResolved = true;
}

function rejected() {
wasRejected = true;
}
});
});

describe('$translate#refresh', function () {
Expand Down

0 comments on commit 8fe6f23

Please sign in to comment.