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

Commit

Permalink
fix($translate): Ensuring that languages will be set based on the ord…
Browse files Browse the repository at this point in the history
…er they are requested, not the order the responses come in.
  • Loading branch information
0x-r4bbit committed Feb 19, 2014
2 parents 95c43b4 + 32e1851 commit ebd62af
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,13 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if (!$translationTable[key] && $loaderFactory) {
$nextLang = key;
langPromises[key] = loadAsync(key).then(function (translation) {
$nextLang = undefined;
translations(translation.key, translation.table);
deferred.resolve(translation.key);
useLanguage(translation.key);

if ($nextLang === key) {
useLanguage(translation.key);
$nextLang = undefined;
}
}, function (key) {
$nextLang = undefined;
$rootScope.$emit('$translateChangeError');
Expand Down
68 changes: 68 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,74 @@ describe('pascalprecht.translate', function () {
});
});

describe('$translate#use() with async loading', function () {

var fastButRequestedSecond = 'en_US',
slowButRequestedFirst = 'ab_CD',
expectedTranslation = 'Hello World',
fastRequestTime = 1000;

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 () {
deferred.resolve({
greeting: expectedTranslation
});
}, fastRequestTime);
}

if (locale === slowButRequestedFirst) {

$timeout(function() {
deferred.resolve({
greeting: 'foo bar bork bork bork'
});
}, fastRequestTime * 2);
}

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

var $translate;

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

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

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

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

var value;

$translate('greeting').then(function (translation) {
value = translation;
});

$rootScope.$digest();
expect(value).toEqual(expectedTranslation);
}));
});

describe('$translate#storageKey()', function () {

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

0 comments on commit ebd62af

Please sign in to comment.