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

Commit

Permalink
fix(translateService): fixes missingTranslationHandler-invokation bug
Browse files Browse the repository at this point in the history
this fix makes sure that missingTranslationHandlerFactory only gets
called when there is no pending loader to laod translations asynchronously.

Closes #74
  • Loading branch information
0x-r4bbit committed Jun 21, 2013
1 parent 4d066db commit 525b353
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
'$q',
function ($interpolate, $log, $injector, $rootScope, $q) {

var Storage;
var Storage,
pendingLoader = false;

if ($storageFactory) {
Storage = $injector.get($storageFactory);
Expand All @@ -427,7 +428,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
return $interpolate(table[translationId])(interpolateParams);
}

if ($missingTranslationHandlerFactory) {
if ($missingTranslationHandlerFactory && !pendingLoader) {
$injector.get($missingTranslationHandlerFactory)(translationId);
}

Expand Down Expand Up @@ -509,6 +510,8 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',

if (!$translationTable[key]) {

pendingLoader = true;

$injector.get($loaderFactory)(angular.extend($loaderOptions, {
key: key
})).then(function (data) {
Expand All @@ -530,6 +533,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
Storage.set($translate.storageKey(), $uses);
}

pendingLoader = false;
$rootScope.$broadcast('translationChangeSuccess');
deferred.resolve($uses);
}, function (key) {
Expand Down
54 changes: 53 additions & 1 deletion test/unit/translateServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ describe('pascalprecht.translate', function () {
});

describe('loader returning multiple promises', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {

$translateProvider.useLoader('customLoader', {});
Expand Down Expand Up @@ -488,5 +488,57 @@ describe('pascalprecht.translate', function () {
});
});
});

});

describe('missing translation handling', function () {

var hasBeenCalled = false;

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {

// consider we have a custom loader, which needs a bit 'til it returns translation
// data
$provide.factory('customLoader', function ($timeout, $q) {
return function (options) {
var deferred = $q.defer(),
translations = {
'FOO': 'BAR'
};

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

return deferred.promise;
};
});

// we also have a custom missing translation handler which gets called, when
// trying to translate translation ids that aren't existing.
$provide.factory('customMissingTranslationHandler', function () {
return function (translationId) {
hasBeenCalled = true;
};
});

// finally, we make use of both
$translateProvider.useLoader('customLoader', {});
$translateProvider.useMissingTranslationHandler('customMissingTranslationHandler');
$translateProvider.preferredLanguage('en_US');
}));

it('shouldn\'t call when there is a pending loader', function () {
inject(function ($translate, $timeout) {
expect($translate('FOO')).toEqual('FOO');
// missingTranslationHandler should not has been called, because i18n data
// is loaded asynchronously it should wait, once its loaded.
expect(hasBeenCalled).toBe(false);
$timeout.flush();
expect($translate('FOO')).toEqual('BAR');
expect($translate('MISSING_ONE')).toEqual('MISSING_ONE');
expect(hasBeenCalled).toBe(true);
});
});
});
});

0 comments on commit 525b353

Please sign in to comment.