Skip to content

Commit 5504018

Browse files
committed
fix (ngPluralize): generate a warning when being asked to use a rule that is not defined
Fix angular#10207
1 parent 30694c8 commit 5504018

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/ng/directive/ngPluralize.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
* <span ng-non-bindable>`{{personCount}}`</span>. The closed braces `{}` is a placeholder
5555
* for <span ng-non-bindable>{{numberExpression}}</span>.
5656
*
57+
* If no rule is defined for a category, then an empty string is displayed and a warning is generated.
58+
* Note that some locales define more categories than `one` and `other`. For example, fr-fr defines `few` and `many`.
59+
*
5760
* # Configuring ngPluralize with offset
5861
* The `offset` attribute allows further customization of pluralized text, which can result in
5962
* a better user experience. For example, instead of the message "4 people are viewing this document",
@@ -172,7 +175,7 @@
172175
</file>
173176
</example>
174177
*/
175-
var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interpolate) {
178+
var ngPluralizeDirective = ['$locale', '$interpolate', '$log', function($locale, $interpolate, $log) {
176179
var BRACE = /{}/g,
177180
IS_WHEN = /^when(Minus)?(.+)$/;
178181

@@ -216,7 +219,11 @@ var ngPluralizeDirective = ['$locale', '$interpolate', function($locale, $interp
216219
// In JS `NaN !== NaN`, so we have to exlicitly check.
217220
if ((count !== lastCount) && !(countIsNaN && isNaN(lastCount))) {
218221
watchRemover();
219-
watchRemover = scope.$watch(whensExpFns[count], updateElementText);
222+
var whenExpFn = whensExpFns[count];
223+
if (typeof whenExpFn === "undefined") {
224+
$log.debug("ngPluralize: no rule defined for '" + count + "' in " + whenExp);
225+
}
226+
watchRemover = scope.$watch(whenExpFn, updateElementText);
220227
lastCount = count;
221228
}
222229
});

test/ng/directive/ngPluralizeSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,28 @@ describe('ngPluralize', function() {
146146
}));
147147
});
148148

149+
describe('error cases', function() {
150+
var $locale, $log;
151+
beforeEach(inject(function (_$locale_, _$log_) {
152+
$locale = _$locale_;
153+
$log = _$log_;
154+
}));
155+
156+
it('should generate a warning when being asked to use a rule that is not defined', inject(function($rootScope, $compile) {
157+
element = $compile(
158+
'<ng:pluralize count="email"' +
159+
"when=\"{'0': 'Zero'," +
160+
"'one': 'Some text'," +
161+
"'other': 'Some text'}\">" +
162+
'</ng:pluralize>')($rootScope);
163+
$locale.pluralCat = function() {return "few";};
164+
$rootScope.email = '3';
165+
expect($log.debug.logs).toEqual([]);
166+
$rootScope.$digest();
167+
expect(element.text()).toBe('');
168+
expect($log.debug.logs.shift()).toEqual(["ngPluralize: no rule defined for 'few' in {'0': 'Zero','one': 'Some text','other': 'Some text'}"]);
169+
}));
170+
});
149171

150172
describe('deal with pluralized strings with offset', function() {
151173
it('should show single/plural strings with offset', inject(function($rootScope, $compile) {

0 commit comments

Comments
 (0)