/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/common/src/i18n/localization.ts
91 строка
2 KB
Matthieu Riegler
refactor(core): use the `@Service` decorator where possible.
08 май 2026, 02:03
08 май 2026, 02:03
a7dab60
Код
Авторство
О чём код?
/** * @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'; } } }