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 { Inject, Injectable, LOCALE_ID } from '@angular/core';
|
---|
9 | import { getLocalePluralCase, Plural } from './locale_data_api';
|
---|
10 | /**
|
---|
11 | * @publicApi
|
---|
12 | */
|
---|
13 | export class NgLocalization {
|
---|
14 | }
|
---|
15 | /**
|
---|
16 | * Returns the plural category for a given value.
|
---|
17 | * - "=value" when the case exists,
|
---|
18 | * - the plural category otherwise
|
---|
19 | */
|
---|
20 | export function getPluralCategory(value, cases, ngLocalization, locale) {
|
---|
21 | let key = `=${value}`;
|
---|
22 | if (cases.indexOf(key) > -1) {
|
---|
23 | return key;
|
---|
24 | }
|
---|
25 | key = ngLocalization.getPluralCategory(value, locale);
|
---|
26 | if (cases.indexOf(key) > -1) {
|
---|
27 | return key;
|
---|
28 | }
|
---|
29 | if (cases.indexOf('other') > -1) {
|
---|
30 | return 'other';
|
---|
31 | }
|
---|
32 | throw new Error(`No plural message found for value "${value}"`);
|
---|
33 | }
|
---|
34 | /**
|
---|
35 | * Returns the plural case based on the locale
|
---|
36 | *
|
---|
37 | * @publicApi
|
---|
38 | */
|
---|
39 | export class NgLocaleLocalization extends NgLocalization {
|
---|
40 | constructor(locale) {
|
---|
41 | super();
|
---|
42 | this.locale = locale;
|
---|
43 | }
|
---|
44 | getPluralCategory(value, locale) {
|
---|
45 | const plural = getLocalePluralCase(locale || this.locale)(value);
|
---|
46 | switch (plural) {
|
---|
47 | case Plural.Zero:
|
---|
48 | return 'zero';
|
---|
49 | case Plural.One:
|
---|
50 | return 'one';
|
---|
51 | case Plural.Two:
|
---|
52 | return 'two';
|
---|
53 | case Plural.Few:
|
---|
54 | return 'few';
|
---|
55 | case Plural.Many:
|
---|
56 | return 'many';
|
---|
57 | default:
|
---|
58 | return 'other';
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | NgLocaleLocalization.decorators = [
|
---|
63 | { type: Injectable }
|
---|
64 | ];
|
---|
65 | NgLocaleLocalization.ctorParameters = () => [
|
---|
66 | { type: String, decorators: [{ type: Inject, args: [LOCALE_ID,] }] }
|
---|
67 | ];
|
---|
68 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9jYWxpemF0aW9uLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vLi4vcGFja2FnZXMvY29tbW9uL3NyYy9pMThuL2xvY2FsaXphdGlvbi50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQUMsTUFBTSxFQUFFLFVBQVUsRUFBRSxTQUFTLEVBQUMsTUFBTSxlQUFlLENBQUM7QUFFNUQsT0FBTyxFQUFDLG1CQUFtQixFQUFFLE1BQU0sRUFBQyxNQUFNLG1CQUFtQixDQUFDO0FBRzlEOztHQUVHO0FBQ0gsTUFBTSxPQUFnQixjQUFjO0NBRW5DO0FBR0Q7Ozs7R0FJRztBQUNILE1BQU0sVUFBVSxpQkFBaUIsQ0FDN0IsS0FBYSxFQUFFLEtBQWUsRUFBRSxjQUE4QixFQUFFLE1BQWU7SUFDakYsSUFBSSxHQUFHLEdBQUcsSUFBSSxLQUFLLEVBQUUsQ0FBQztJQUV0QixJQUFJLEtBQUssQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLEdBQUcsQ0FBQyxDQUFDLEVBQUU7UUFDM0IsT0FBTyxHQUFHLENBQUM7S0FDWjtJQUVELEdBQUcsR0FBRyxjQUFjLENBQUMsaUJBQWlCLENBQUMsS0FBSyxFQUFFLE1BQU0sQ0FBQyxDQUFDO0lBRXRELElBQUksS0FBSyxDQUFDLE9BQU8sQ0FBQyxHQUFHLENBQUMsR0FBRyxDQUFDLENB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