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

Commit

Permalink
fix(translate.js): Allow blank translation values
Browse files Browse the repository at this point in the history
Check for translationTable presence in place of boolean eval of values.
  • Loading branch information
Heath Morrison committed May 25, 2013
1 parent c18dfaf commit 97591a8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,9 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
}

var $translate = function (translationId, interpolateParams) {
var translation = ($uses) ?
($translationTable[$uses] ? $translationTable[$uses][translationId] : translationId) :
$translationTable[translationId];
if (translation) {
return $interpolate(translation)(interpolateParams);
var table = $uses ? $translationTable[$uses] : $translationTable;
if (table && table.hasOwnProperty(translationId)) {
return $interpolate(table[translationId])(interpolateParams);
}

if ($missingTranslationHandlerFactory) {
Expand Down
22 changes: 20 additions & 2 deletions test/unit/translateDirectiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe('pascalprecht.translate', function () {
'YET_ANOTHER': 'Hallo da!',
'TEXT_WITH_VALUE': 'This is a text with given value: {{value}}',
'HOW_ABOUT_THIS': '{{value}} + {{value}}',
'AND_THIS': '{{value + value}}'
'AND_THIS': '{{value + value}}',
'BLANK_VALUE': ''
});
}));

Expand All @@ -39,6 +40,10 @@ describe('pascalprecht.translate', function () {
element = $compile('<div translate="TRANSLATION_ID"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('foo');

element = $compile('<div translate="BLANK_VALUE"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
});
});

Expand Down Expand Up @@ -75,6 +80,10 @@ describe('pascalprecht.translate', function () {
element = $compile('<div translate>TRANSLATION_ID</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('foo');

element = $compile('<div translate>BLANK_VALUE</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
});
});

Expand Down Expand Up @@ -238,7 +247,8 @@ describe('pascalprecht.translate', function () {
'YET_ANOTHER': 'Hallo da!',
'TEXT_WITH_VALUE': 'This is a text with given value: {{value}}',
'HOW_ABOUT_THIS': '{{value}} + {{value}}',
'AND_THIS': '{{value + value}}'
'AND_THIS': '{{value + value}}',
'BLANK_VALUE': ''
});
$translateProvider.uses('en_EN');
}));
Expand All @@ -265,6 +275,10 @@ describe('pascalprecht.translate', function () {
element = $compile('<div translate="EXISTING_TRANSLATION_ID"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('foo');

element = $compile('<div translate="BLANK_VALUE"></div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
});
});

Expand Down Expand Up @@ -301,6 +315,10 @@ describe('pascalprecht.translate', function () {
element = $compile('<div translate>EXISTING_TRANSLATION_ID</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('foo');

element = $compile('<div translate>BLANK_VALUE</div>')($rootScope);
$rootScope.$digest();
expect(element.text()).toBe('');
});
});

Expand Down
8 changes: 6 additions & 2 deletions test/unit/translateFilterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe('pascalprecht.translate', function () {
'TEXT': 'this is a text',
'TEXT_WITH_VALUE': 'This is a text with given value: {{value}}',
'HOW_ABOUT_THIS': '{{value}} + {{value}}',
'AND_THIS': '{{value + value}}'
'AND_THIS': '{{value + value}}',
'BLANK_VALUE': ''
});
}));

Expand All @@ -32,6 +33,7 @@ describe('pascalprecht.translate', function () {
it('should return translation if translation id exist', function () {
inject(function ($filter) {
expect($filter('translate')('TRANSLATION_ID')).toEqual('Lorem Ipsum ');
expect($filter('translate')('BLANK_VALUE')).toEqual('');
});
});

Expand Down Expand Up @@ -77,7 +79,8 @@ describe('pascalprecht.translate', function () {
'TEXT': 'this is a text',
'TEXT_WITH_VALUE': 'This is a text with given value: {{value}}',
'HOW_ABOUT_THIS': '{{value}} + {{value}}',
'AND_THIS': '{{value + value}}'
'AND_THIS': '{{value + value}}',
'BLANK_VALUE': ''
});

$translateProvider.uses('en_EN');
Expand All @@ -92,6 +95,7 @@ describe('pascalprecht.translate', function () {
it('should return translation if translation id exist and language is given', function () {
inject(function ($filter) {
expect($filter('translate')('EXISTING_TRANSLATION_ID')).toEqual('foo');
expect($filter('translate')('BLANK_VALUE')).toEqual('');
});
});

Expand Down
7 changes: 5 additions & 2 deletions test/unit/translateServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('pascalprecht.translate', function () {
beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider.translations({
'EXISTING_TRANSLATION_ID': 'foo',
'BLANK_VALUE': '',
'TRANSLATION_ID': 'Lorem Ipsum {{value}}',
'TRANSLATION_ID_2': 'Lorem Ipsum {{value}} + {{value}}',
'TRANSLATION_ID_3': 'Lorem Ipsum {{value + value}}'
Expand All @@ -126,9 +127,9 @@ describe('pascalprecht.translate', function () {
});

it('should return translation if translation id if exists', function () {
var translationId = "EXISTING_TRANSLATION_ID";
inject(function ($translate) {
expect($translate(translationId)).toEqual('foo');
expect($translate("EXISTING_TRANSLATION_ID")).toEqual('foo');
expect($translate("BLANK_VALUE")).toEqual('');
});
});

Expand Down Expand Up @@ -162,6 +163,7 @@ describe('pascalprecht.translate', function () {
$translateProvider.translations('de_DE', {
'EXISTING_TRANSLATION_ID': 'foo',
'ANOTHER_ONE': 'bar',
'BLANK_VALUE': '',
'TRANSLATION_ID': 'Lorem Ipsum {{value}}',
'TRANSLATION_ID_2': 'Lorem Ipsum {{value}} + {{value}}',
'TRANSLATION_ID_3': 'Lorem Ipsum {{value + value}}',
Expand Down Expand Up @@ -200,6 +202,7 @@ describe('pascalprecht.translate', function () {
inject(function ($translate) {
expect($translate('EXISTING_TRANSLATION_ID')).toEqual('foo');
expect($translate('ANOTHER_ONE')).toEqual('bar');
expect($translate('BLANK_VALUE')).toEqual('');
});
});

Expand Down

0 comments on commit 97591a8

Please sign in to comment.