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

Commit

Permalink
fix(service): make the fallback $uses / $translate.use work in a corr…
Browse files Browse the repository at this point in the history
…ect manner

test(fallback): add a test case for the scenario described in #1415
  • Loading branch information
tspaeth authored and knalli committed Feb 28, 2016
1 parent d94ecb0 commit 7e71a5a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1758,8 +1758,8 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide

// if there isn't a translation table for the language we've requested,
// we load it asynchronously
$nextLang = key;
if (($forceAsyncReloadEnabled || !$translationTable[key]) && $loaderFactory && !langPromises[key]) {
$nextLang = key;
langPromises[key] = loadAsync(key).then(function (translation) {
translations(translation.key, translation.table);
deferred.resolve(translation.key);
Expand All @@ -1780,7 +1780,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
// we are already loading this asynchronously
// resolve our new deferred when the old langPromise is resolved
langPromises[key].then(function (translation) {
if (!$uses) {
if ($nextLang === translation.key) {
useLanguage(translation.key);
}
deferred.resolve(translation.key);
Expand Down
104 changes: 103 additions & 1 deletion test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ describe('pascalprecht.translate', function () {
expect(newValue).toEqual('bar');
});

it('should clear completelly inactive translation tables', function () {
it('should clear completely inactive translation tables', function () {
var value;

function setValue(answer) {
Expand Down Expand Up @@ -2542,4 +2542,106 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translate#use() being changed to a fallback while having a fallback stack also async loaded', function () {

var fastButRequestedSecond = 'en_US',
slowButRequestedFirst = 'ab_CD',
expectedTranslation = 'Hello World',
notExpectedTranslation = 'foo bar bork bork bork',
onlyFirstTranslation = 'Only In First',
onlySecondTranslation = 'Only In Second',
fastRequestTime = 1000,
firstLanguageResponded = false,
secondLanguageResponded = false;

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

$translateProvider.useLoader('customLoader');

$translateProvider.preferredLanguage(slowButRequestedFirst);
$translateProvider.fallbackLanguage(fastButRequestedSecond);

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

if (locale === fastButRequestedSecond) {
$timeout(function () {
secondLanguageResponded = true;
deferred.resolve({
greeting: expectedTranslation,
onlySecond: onlySecondTranslation
});
}, fastRequestTime);
}

if (locale === slowButRequestedFirst) {
$timeout(function() {
firstLanguageResponded = true;
deferred.resolve({
greeting: notExpectedTranslation,
onlyFirst: onlyFirstTranslation
});
}, fastRequestTime * 4);
}

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

var $translate;

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

$timeout(function () {
$translate.use(slowButRequestedFirst);
}, fastRequestTime / 2);

$timeout.flush();
$rootScope.$digest();
}));

it('should be requested the first language', function () {
expect(firstLanguageResponded).toEqual(true);
});

it('should be requested the second language', function () {
expect(secondLanguageResponded).toEqual(true);
});

it('should set the language to be the most recently requested one, not the most recently responded one', inject(function($rootScope, $q, $timeout) {

var value, firstValue, secondValue;

$timeout(function () {
$translate.use(fastButRequestedSecond).then(function (result) {
$translate('greeting').then(function (translation) {
value = translation;
});

$translate('onlyFirst').then(function (translation) {
firstValue = translation;
});

$translate('onlySecond').then(function (translation) {
secondValue = translation;
});
});



}, fastRequestTime / 2);
$timeout.flush();
$rootScope.$digest();

$rootScope.$digest();
expect(value).toEqual(expectedTranslation);
expect(firstValue).toEqual(onlyFirstTranslation);
expect(secondValue).toEqual(onlySecondTranslation);
}));
});

});

0 comments on commit 7e71a5a

Please sign in to comment.