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

Commit

Permalink
fix(service): fix interpolation issue with non-string as input
Browse files Browse the repository at this point in the history
Since AngularJS 1.5, the input values (i.e. of a filter) will used 'as is' which means numbers (as translation ids) will not be transformed to strings anymore. This generates unexpected errors.

Because this fix, the result is that explicit typed number values will be return (transformed as a string).

```
// before and < AJS 1.5
$filter('translate)(0) === 0
// after and < AJS 1.5
$filter('translate)(0) === '0'

// before and AJS 1.5+
$filter('translate)(0) exception
// after and AJS 1.5+
$filter('translate)(0) === '0'

```

Fixes #1511
  • Loading branch information
knalli committed Jul 17, 2016
1 parent 04e11c9 commit fa4a80e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/service/default-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,29 @@ function $translateDefaultInterpolation ($interpolate, $translateSanitization) {
* @methodOf pascalprecht.translate.$translateDefaultInterpolation
*
* @description
* Interpolates given string agains given interpolate params using angulars
* Interpolates given value agains given interpolate params using angulars
* `$interpolate` service.
*
* Since AngularJS 1.5, `value` must not be a string but can be anything input.
*
* @returns {string} interpolated string.
*/
$translateInterpolator.interpolate = function (string, interpolationParams) {
$translateInterpolator.interpolate = function (value, interpolationParams) {
interpolationParams = interpolationParams || {};
interpolationParams = $translateSanitization.sanitize(interpolationParams, 'params');

var interpolatedText = $interpolate(string)(interpolationParams);
interpolatedText = $translateSanitization.sanitize(interpolatedText, 'text');
var interpolatedText;
if (angular.isNumber(value)) {
// numbers are safe
interpolatedText = '' + value;
} else if (angular.isString(value)) {
// strings must be interpolated (that's the job here)
interpolatedText = $interpolate(value)(interpolationParams);
interpolatedText = $translateSanitization.sanitize(interpolatedText, 'text');
} else {
// neither a number or a string, cant interpolate => empty string
interpolatedText = '';
}

return interpolatedText;
};
Expand Down
60 changes: 60 additions & 0 deletions test/unit/filter/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,64 @@ describe('pascalprecht.translate', function () {
expect(element.html()).toBe('Hallo');
});
});

describe('filter handling a falsy value', function () {

beforeEach(module('pascalprecht.translate', function ($translateProvider) {
$translateProvider
.translations('en', {
'HELLO': 'Hello'
})
.translations('de', {
'HELLO': 'Hallo'
})
.preferredLanguage('en');
}));

var $translate, $rootScope, $compile;
beforeEach(inject(function (_$rootScope_, _$compile_, _$translate_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$translate = _$translate_;
}));

it('should work with "0"', function () {
var element = $compile('<div>{{0 | translate}}</div>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('0');
});

it('should work with "false"', function () {
var element = $compile('<div>{{false | translate}}</div>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('');
});

it('should work with "null"', function () {
var element = $compile('<div>{{null | translate}}</div>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('');
});

it('should work with "0" (by scope)', function () {
$rootScope.x = 0;
var element = $compile('<div>{{x | translate}}</div>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('0');
});

it('should work with "false" (by scope)', function () {
$rootScope.x = false;
var element = $compile('<div>{{x | translate}}</div>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('');
});

it('should work with "null" (by scope)', function () {
$rootScope.x = null;
var element = $compile('<div>{{x | translate}}</div>')($rootScope);
$rootScope.$digest();
expect(element.html()).toBe('');
});
});
});

0 comments on commit fa4a80e

Please sign in to comment.