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

Commit

Permalink
fix(refresh): it has to clear all tables if no language key is specified
Browse files Browse the repository at this point in the history
The `$translate.refresh()` method has to clear all tables if no language key is specified.
In other case it may cause inconsistent state of different translation tables inside the module.

Fixes the following case:
// The 'A' language is not a fallback language
* $translate.use('A');
* $translate.use('B');
* $translate.refresh(); // only 'B' language is refreshed
* $translate.use('A'); // old translation table is used instead of fetching a new one

Related to #988.
  • Loading branch information
DWand authored and knalli committed Apr 22, 2015
1 parent a298ed8 commit 3cce795
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1714,10 +1714,8 @@ function $translate($STORAGE_KEY, $windowProvider) {
}

var allTranslationsLoaded = function (tableData) {
$translationTable = {};
angular.forEach(tableData, function (data) {
if ($translationTable[data.key]) {
delete $translationTable[data.key];
}
translations(data.key, data.table);
});
if ($uses) {
Expand Down
35 changes: 35 additions & 0 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,7 @@ describe('pascalprecht.translate', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$translateProvider
.translations('de_DE', translationMock)
.translations('en_EN', translationMock)
.preferredLanguage('en_EN')
.useLoader('customLoader');
Expand Down Expand Up @@ -1579,6 +1580,40 @@ describe('pascalprecht.translate', function () {
expect(newValue).toEqual('bar');
});

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

function setValue(answer) {
value = answer;
}

function fetchTranslation() {
$translate('EXISTING_TRANSLATION_ID').then(setValue, setValue);
$rootScope.$digest();
}

function changeLanguage(lang) {
$translate.use(lang);
$rootScope.$digest();
}

changeLanguage('de_DE');
fetchTranslation();
expect(value).toEqual('foo'); // found

changeLanguage('en_EN');
$translate.refresh();
$timeout.flush();
$rootScope.$digest();

changeLanguage('de_DE');
$timeout.flush(); // Expect the `de_DE` language to be loaded
$rootScope.$digest();

fetchTranslation();
expect(value).toEqual('EXISTING_TRANSLATION_ID'); // not found
});

it('should emit $translateRefreshStart event', function () {
spyOn($rootScope, '$emit');
$translate.refresh();
Expand Down

0 comments on commit 3cce795

Please sign in to comment.