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

Commit

Permalink
fix(service): fallback languages follow shortcuts (fixes #758)
Browse files Browse the repository at this point in the history
  • Loading branch information
tspaeth authored and knalli committed Jan 10, 2015
1 parent 5a9f436 commit cce897a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,13 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
getTranslationTable(langKey).then(function (translationTable) {
if (Object.prototype.hasOwnProperty.call(translationTable, translationId)) {
Interpolator.setLocale(langKey);
deferred.resolve(Interpolator.interpolate(translationTable[translationId], interpolateParams));
var translation = translationTable[translationId];
if (translation.substr(0, 2) === '@:') {
getFallbackTranslation(langKey, translation.substr(2), interpolateParams, Interpolator)
.then(deferred.resolve, deferred.reject);
} else {
deferred.resolve(Interpolator.interpolate(translationTable[translationId], interpolateParams));
}
Interpolator.setLocale($uses);
} else {
deferred.reject();
Expand Down Expand Up @@ -1104,6 +1110,9 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
if (translationTable && Object.prototype.hasOwnProperty.call(translationTable, translationId)) {
Interpolator.setLocale(langKey);
result = Interpolator.interpolate(translationTable[translationId], interpolateParams);
if (result.substr(0, 2) === '@:') {
return getFallbackTranslationInstant(langKey, result.substr(2), interpolateParams, Interpolator);
}
Interpolator.setLocale($uses);
}

Expand Down
87 changes: 86 additions & 1 deletion test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ describe('pascalprecht.translate', function () {
expect($translate.instant('FOO')).toEqual('bar');
});

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

Expand Down Expand Up @@ -1644,4 +1644,89 @@ describe('pascalprecht.translate', function () {
expect($translate.instant('FOO4 {{value}}', {'value': 'PARAM'})).toEqual('FOO4 PARAM');
});
});

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

var missingTranslations = {};

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {
$translateProvider
.translations('en', {
'NAMESPACE1':
{
'SUBSPACE1':
{
'FOO': 'Translation1',
'BAR': 'Translation2',
'SUBSPACE1': 'Subtrans'
}
}
})
.translations('de', {
'FOO': 'Ich bin ein Foo'
})
.translations('fr', {
'NAMESPACE1': {
'SUBSPACE1': {
'FOO': 'Translation1 Francais',
'BAR': 'Translation2 Francais',
'SUBSPACE1': 'Subtrans',
'SUBSPACE2': 'Subtrans un a deux'
},
'SUBSPACE_FR': {
'SUBSPACE_FR': 'Francais angular'
},
'FALLBACK': '@:NAMESPACE1.SUBSPACE1.FOO'
}
})
.fallbackLanguage(['de', 'fr'])
.preferredLanguage('en');


}));

var $translate, $q, $rootScope;

beforeEach(inject(function (_$translate_, _$q_, _$rootScope_) {
$translate = _$translate_;
$q = _$q_;
$rootScope = _$rootScope_;
}));

it('should invoke the translation method and return the sh1ortcut translation value', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

promise.then(function (foo) {
value = foo;
});
$translate('NAMESPACE1.SUBSPACE1').then(function (translation) {
deferred.resolve(translation);
});
$rootScope.$digest();
expect(value).toEqual('Subtrans');
});

it('should invoke the translation fallback stack and return the resolved french shortcut', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

promise.then(function (foo) {
value = foo;
});
$translate('NAMESPACE1.SUBSPACE_FR').then(function (translation) {
deferred.resolve(translation);
});
$rootScope.$digest();
expect(value).toEqual('Francais angular');
});

it('should invoke the translation fallback stack and return the resolved at syntax based shortcut', function () {
expect($translate.instant('NAMESPACE1.FALLBACK')).toBe('Translation1 Francais');
});

});

});

0 comments on commit cce897a

Please sign in to comment.