Within Google, closure compiler is used for dealing with translations. We generate a closure-compatible locale file that allows for registration within Angular, so that Closure i18n works well together with Angular applications. Closure compiler does not limit its locales to BCP47-canonical locale identifiers. This commit updates the generation logic so that we also support deprecated (but aliased) locale identifiers, or other aliases which are likely used within Closure. We use CLDR's alias supplemental data for this. It instructs us to alias `iw` to `he` for example. `iw` is still supported in Closure. Note that we do not manually extract all locales supported in Closure; instead we only support the CLDR canonical locales (as done before) + common aliases that CLDR provides data for. We are not aware of other locale aliases within Closure that wouldn't be part of the CLDR aliases. If there would be, then Angular/Closure would fail accordingly. PR Close #42230
43 lines
1.9 KiB
TypeScript
43 lines
1.9 KiB
TypeScript
/**
|
|
* @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.io/license
|
|
*/
|
|
import {CldrLocaleData} from './cldr-data';
|
|
|
|
// There are no types available for `cldr`.
|
|
const cldr = require('cldr');
|
|
|
|
/**
|
|
* Returns the plural function for a locale
|
|
* TODO(ocombe): replace "cldr" extractPluralRuleFunction with our own extraction using "CldrJS"
|
|
* because the 2 libs can become out of sync if they use different versions of the cldr database
|
|
*/
|
|
export function getPluralFunction(localeData: CldrLocaleData, withTypes = true) {
|
|
// We use the resolved bundle for extracting the plural function. This matches with the
|
|
// lookup logic used by other extractions in the tool (using `cldrjs`), and also ensures
|
|
// we follow the CLDR-specified bundle lookup algorithm. A language does not necessarily
|
|
// resolve directly to a bundle CLDR provides data for.
|
|
const bundleName = localeData.attributes.bundle;
|
|
let fn = cldr.extractPluralRuleFunction(bundleName).toString();
|
|
|
|
const numberType = withTypes ? ': number' : '';
|
|
fn =
|
|
fn.replace(/function anonymous\(n[^}]+{/g, `function plural(n${numberType})${numberType} {`)
|
|
// Since our generated plural functions only take numbers, we can eliminate some of
|
|
// the logic generated by the `cldr` package (to reduce payload size).
|
|
.replace(/var/g, /let/)
|
|
.replace(/if\s+\(typeof\s+n\s+===\s+["']string["']\)\s+n\s+=\s+parseInt\(n,\s+10\);/, '');
|
|
|
|
// The replacement values must match the `Plural` enum from common.
|
|
// We do not use the enum directly to avoid depending on that package.
|
|
return fn.replace(/["']zero["']/, '0')
|
|
.replace(/["']one["']/, '1')
|
|
.replace(/["']two["']/, '2')
|
|
.replace(/["']few["']/, '3')
|
|
.replace(/["']many["']/, '4')
|
|
.replace(/["']other["']/, '5');
|
|
}
|