1 | /**
|
---|
2 | * @license
|
---|
3 | * Copyright Google LLC All Rights Reserved.
|
---|
4 | *
|
---|
5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
6 | * found in the LICENSE file at https://angular.io/license
|
---|
7 | */
|
---|
8 | import { Pipe } from '@angular/core';
|
---|
9 | import { getPluralCategory, NgLocalization } from '../i18n/localization';
|
---|
10 | import { invalidPipeArgumentError } from './invalid_pipe_argument_error';
|
---|
11 | const _INTERPOLATION_REGEXP = /#/g;
|
---|
12 | /**
|
---|
13 | * @ngModule CommonModule
|
---|
14 | * @description
|
---|
15 | *
|
---|
16 | * Maps a value to a string that pluralizes the value according to locale rules.
|
---|
17 | *
|
---|
18 | * @usageNotes
|
---|
19 | *
|
---|
20 | * ### Example
|
---|
21 | *
|
---|
22 | * {@example common/pipes/ts/i18n_pipe.ts region='I18nPluralPipeComponent'}
|
---|
23 | *
|
---|
24 | * @publicApi
|
---|
25 | */
|
---|
26 | export class I18nPluralPipe {
|
---|
27 | constructor(_localization) {
|
---|
28 | this._localization = _localization;
|
---|
29 | }
|
---|
30 | /**
|
---|
31 | * @param value the number to be formatted
|
---|
32 | * @param pluralMap an object that mimics the ICU format, see
|
---|
33 | * http://userguide.icu-project.org/formatparse/messages.
|
---|
34 | * @param locale a `string` defining the locale to use (uses the current {@link LOCALE_ID} by
|
---|
35 | * default).
|
---|
36 | */
|
---|
37 | transform(value, pluralMap, locale) {
|
---|
38 | if (value == null)
|
---|
39 | return '';
|
---|
40 | if (typeof pluralMap !== 'object' || pluralMap === null) {
|
---|
41 | throw invalidPipeArgumentError(I18nPluralPipe, pluralMap);
|
---|
42 | }
|
---|
43 | const key = getPluralCategory(value, Object.keys(pluralMap), this._localization, locale);
|
---|
44 | return pluralMap[key].replace(_INTERPOLATION_REGEXP, value.toString());
|
---|
45 | }
|
---|
46 | }
|
---|
47 | I18nPluralPipe.decorators = [
|
---|
48 | { type: Pipe, args: [{ name: 'i18nPlural', pure: true },] }
|
---|
49 | ];
|
---|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