Files
Paul Gschwendtner 074abad64c build: speed up locale file generation to improve DX (#50426)
Before this commit, building everything to run `@angular/core` tests:
```
INFO: Elapsed time: 76.496s, Critical Path: 72.92s
INFO: 225 processes: 125 internal, 5 linux-sandbox, 2 local, 93 worker.
INFO: Build completed successfully, 225 total actions
```

After:
```
Use --sandbox_debug to see verbose messages from the sandbox
INFO: Elapsed time: 15.952s, Critical Path: 10.75s
INFO: 200 processes: 128 internal, 4 linux-sandbox, 2 local, 66 worker.
```

This being on a specialist Cloudtop.

PR Close #50426
2023-05-30 13:05:18 -07:00

46 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 {runfiles} from '@bazel/runfiles';
import {CldrLocaleData} from './cldr-data';
// There are no types available for `cldr`.
const {load: createCldr} = await import('cldr' as any);
// Load once to avoid re-parsing CLDR XML data on every invocation.
const cldr = createCldr(runfiles.resolve('cldr_xml_data'));
/**
* Returns the plural function for a locale.
*/
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\(val[^)]+\)/g, `function plural(val${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(/n\s+=\s+Number\(val\)/, 'n = val')
.replace(/if\s+\(isNaN\(n\)\)\s+throw Error\([^)]+\);/, '');
// 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');
}