-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Expand file tree
/
Copy pathlocalization.ts
More file actions
91 lines (79 loc) 路 1.93 KB
/
Copy pathlocalization.ts
File metadata and controls
91 lines (79 loc) 路 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
inject,
Inject,
Injectable,
LOCALE_ID,
傻RuntimeError as RuntimeError,
Service,
} from '@angular/core';
import {RuntimeErrorCode} from '../errors';
import {getLocalePluralCase, Plural} from './locale_data_api';
/**
* @publicApi
*/
@Service({factory: () => new NgLocaleLocalization(inject(LOCALE_ID))})
export abstract class NgLocalization {
abstract getPluralCategory(value: number, locale?: string): string;
}
/**
* Returns the plural category for a given value.
* - "=value" when the case exists,
* - the plural category otherwise
*/
export function getPluralCategory(
value: number,
cases: string[],
ngLocalization: NgLocalization,
locale?: string,
): string {
let key = `=${value}`;
if (cases.indexOf(key) > -1) {
return key;
}
key = ngLocalization.getPluralCategory(value, locale);
if (cases.indexOf(key) > -1) {
return key;
}
if (cases.indexOf('other') > -1) {
return 'other';
}
throw new RuntimeError(
RuntimeErrorCode.NO_PLURAL_MESSAGE_FOUND,
ngDevMode && `No plural message found for value "${value}"`,
);
}
/**
* Returns the plural case based on the locale
*
* @publicApi
*/
@Injectable()
export class NgLocaleLocalization extends NgLocalization {
constructor(@Inject(LOCALE_ID) protected locale: string) {
super();
}
override getPluralCategory(value: number, locale?: string): string {
const plural = getLocalePluralCase(locale || this.locale)(value);
switch (plural) {
case Plural.Zero:
return 'zero';
case Plural.One:
return 'one';
case Plural.Two:
return 'two';
case Plural.Few:
return 'few';
case Plural.Many:
return 'many';
default:
return 'other';
}
}
}