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

Commit

Permalink
fix($translate): apply notFoundIndicators only when all configured la…
Browse files Browse the repository at this point in the history
…nguage checked in $translate.instant method

Fixes #1314
  • Loading branch information
shkrobova authored and knalli committed Dec 13, 2015
1 parent 23d5ce1 commit 25b13c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1969,8 +1969,6 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
if ($translationTable[possibleLangKey]) {
if (typeof $translationTable[possibleLangKey][translationId] !== 'undefined') {
result = determineTranslationInstant(translationId, interpolateParams, interpolationId);
} else if ($notFoundIndicatorLeft || $notFoundIndicatorRight) {
result = applyNotFoundIndicators(translationId);
}
}
if (typeof result !== 'undefined') {
Expand All @@ -1979,10 +1977,14 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
}

if (!result && result !== '') {
// Return translation of default interpolator if not found anything.
result = defaultInterpolator.interpolate(translationId, interpolateParams);
if ($missingTranslationHandlerFactory && !pendingLoader) {
result = translateByHandler(translationId, interpolateParams);
if ($notFoundIndicatorLeft || $notFoundIndicatorRight) {
result = applyNotFoundIndicators(translationId);
} else {
// Return translation of default interpolator if not found anything.
result = defaultInterpolator.interpolate(translationId, interpolateParams);
if ($missingTranslationHandlerFactory && !pendingLoader) {
result = translateByHandler(translationId, interpolateParams);
}
}
}

Expand Down
44 changes: 44 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2175,6 +2175,50 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translate.instant (with fallback and not found indicators)', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$translateProvider
.useLoader('customLoader')
.translations('en', {
'FOO': 'bar'
})
.translations('de', {
'FOO2': 'bar2'
})
.preferredLanguage('de')
.fallbackLanguage('en')
.translationNotFoundIndicator('-+-+');

$provide.factory('customLoader', function ($q, $timeout) {
return function (options) {
var deferred = $q.defer();

$timeout(function () {
deferred.resolve({});
}, 1000);

return deferred.promise;
};
});
}));

var $translate;

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

it('should return translation if translation id exist', function () {
expect($translate.instant('FOO')).toEqual('bar');
expect($translate.instant('FOO2')).toEqual('bar2');
});

it('should return translation id wrapped into not found indicators if translation id does not exist', function () {
expect($translate.instant('FOO3')).toEqual('-+-+ FOO3 -+-+');
});
});

describe('$translate#determineTranslation with fallback for shortcuts', function () {

var missingTranslations = {};
Expand Down

0 comments on commit 25b13c4

Please sign in to comment.