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

Commit

Permalink
fix(translate): handle null translation
Browse files Browse the repository at this point in the history
null translation treated as if does not exist

Fixes #665
  • Loading branch information
GuyMograbi authored and knalli committed Jan 1, 2017
1 parent 93f37eb commit 1e57b4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/service/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
var deferred = $q.defer();

var onResolve = function (translationTable) {
if (Object.prototype.hasOwnProperty.call(translationTable, translationId)) {
if (Object.prototype.hasOwnProperty.call(translationTable, translationId) && translationTable[translationId] !== null) {
Interpolator.setLocale(langKey);
var translation = translationTable[translationId];
if (translation.substr(0, 2) === '@:') {
Expand Down Expand Up @@ -1397,7 +1397,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
var getFallbackTranslationInstant = function (langKey, translationId, interpolateParams, Interpolator, sanitizeStrategy) {
var result, translationTable = $translationTable[langKey];

if (translationTable && Object.prototype.hasOwnProperty.call(translationTable, translationId)) {
if (translationTable && Object.prototype.hasOwnProperty.call(translationTable, translationId) && translationTable[translationId] !== null) {
Interpolator.setLocale(langKey);
result = Interpolator.interpolate(translationTable[translationId], interpolateParams, 'filter', sanitizeStrategy);
result = applyPostProcessing(translationId, translationTable[translationId], result, interpolateParams, langKey, sanitizeStrategy);
Expand Down Expand Up @@ -1550,7 +1550,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
Interpolator = (interpolationId) ? interpolatorHashMap[interpolationId] : defaultInterpolator;

// if the translation id exists, we can just interpolate it
if (table && Object.prototype.hasOwnProperty.call(table, translationId)) {
if (table && Object.prototype.hasOwnProperty.call(table, translationId) && table[translationId] !== null ) {
var translation = table[translationId];

// If using link, rerun $translate with linked translationId and return it
Expand Down Expand Up @@ -1612,7 +1612,7 @@ function $translate($STORAGE_KEY, $windowProvider, $translateSanitizationProvide
}

// if the translation id exists, we can just interpolate it
if (table && Object.prototype.hasOwnProperty.call(table, translationId)) {
if (table && Object.prototype.hasOwnProperty.call(table, translationId) && table[translationId] !== null) {
var translation = table[translationId];

// If using link, rerun $translate with linked translationId and return it
Expand Down
36 changes: 32 additions & 4 deletions test/unit/service/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('pascalprecht.translate', function () {
var translationMock = {
'EXISTING_TRANSLATION_ID': 'foo',
'BLANK_VALUE': '',
'NULL': null,
'TRANSLATION_ID': 'Lorem Ipsum {{value}}',
'TRANSLATION_ID_2': 'Lorem Ipsum {{value}} + {{value}}',
'TRANSLATION_ID_3': 'Lorem Ipsum {{value + value}}',
Expand Down Expand Up @@ -98,7 +99,8 @@ describe('pascalprecht.translate', function () {
.translations('en', translationMock)
.translations('en', {
'FOO': 'bar',
'BAR': 'foo'
'BAR': 'foo',
'NULL': null,
})
.translations('de', {
'FOO': 'faa'
Expand Down Expand Up @@ -285,6 +287,25 @@ describe('pascalprecht.translate', function () {
expect(value).toEqual(translationId);
});

it('should return translation id if translation is null', function () {
var deferred = $q.defer(),
promise = deferred.promise,
value;

promise.then(function (translation) {
value = translation;
});


$translate('NULL').then(null, function (translations) {
deferred.resolve(translations);
});

$rootScope.$digest();
expect(value).toEqual('NULL');
});


it('should return translation if translation id if exists', function () {
var deferred = $q.defer(),
promise = deferred.promise,
Expand Down Expand Up @@ -1252,7 +1273,7 @@ describe('pascalprecht.translate', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider
.translations('de_DE', translationMock)
.translations('en_EN', { 'TRANSLATION__ID': 'bazinga' })
.translations('en_EN', { 'TRANSLATION__ID': 'bazinga', 'NULL': 'yowza' })
.preferredLanguage('de_DE')
.fallbackLanguage('en_EN');
}));
Expand Down Expand Up @@ -1281,12 +1302,13 @@ describe('pascalprecht.translate', function () {
promise.then(function (translation) {
value = translation;
});
$translate('TRANSLATION__ID').then(function (translation) {
$translate(['TRANSLATION__ID','NULL']).then(function (translation) {
deferred.resolve(translation);
});

$rootScope.$digest();
expect(value).toEqual('bazinga');
expect(value.TRANSLATION__ID).toEqual('bazinga');
expect(value.NULL).toEqual('yowza');
});

it('should use fallback language when forceLanguage if translation id doesn\'t exist', function () {
Expand Down Expand Up @@ -2290,6 +2312,7 @@ describe('pascalprecht.translate', function () {
.translations('en', {
'FOO': 'bar',
'BAR': 'foo',
'NULL': null,
'FOOBAR': 'Foo bar {{value}}',
})
.translations('de', {
Expand Down Expand Up @@ -2320,6 +2343,10 @@ describe('pascalprecht.translate', function () {
expect($translate.instant('BLANK_VALUE')).toEqual('');
});

it('should handle null values as if not exists', function () {
expect($translate.instant('NULL')).toEqual('NULL');
});

it('should return translations of multiple translation ids', function () {
var result = $translate.instant(['FOO', 'FOO2', 'BLANK_VALUE']);
expect(result.FOO).toEqual('bar');
Expand Down Expand Up @@ -2350,6 +2377,7 @@ describe('pascalprecht.translate', function () {
'FOO': 'bar',
'BAR': 'foo',
'BARE': '',
'NULL': null,
'FOOBAR': 'Foo bar {{value}}',
})
.translations('de', {
Expand Down

0 comments on commit 1e57b4f

Please sign in to comment.