angular-docs-cn/packages/common/locales/generate-locales-tool/locale-global-file.ts

43 lines
1.6 KiB
TypeScript
Raw Normal View History

build: convert CLDR locale extraction from Gulp to Bazel tool (#42230) Converts the CLDR locale extraction script to a Bazel tool. This allows us to generate locale files within Bazel, so that locales don't need to live as sources within the repo. Also it allows us to get rid of the legacy Gulp tooling. The migration of the Gulp script to a Bazel tool involved the following things: 1. Basic conversion of the `extract.js` script to TypeScript. This mostly was about adding explicit types. e.g. adding `locale: string` or `localeData: CldrStatic`. 2. Split-up into separate files. Instead of keeping the large `extract.js` file, the tool has been split into separate files. The logic remains the same, just that code is more readable and maintainable. 3. Introduction of a new `index.ts` file that is the entry-point for the Bazel tool. Previously the Gulp tool just generated all locale files, the default locale and base currency files at once. The new entry-point accepts a mode to be passed as first process argument. based on that argument, either locales are generated into a specified directory, or the default locale, base currencies or closure file is generated. This allows us to generate files with a Bazel genrule where we simply run the tool and specify the outputs. Note: It's necessary to have multiple modes because files live in separate locations. e.g. the default locale in `@angular/core`, but the rest in `@angular/common`. 4. Removal of the `cldr-data-downloader` and custom CLDR resolution logic. Within Bazel we cannot run a downloader using network. We switch this to something more Bazel idiomatic with better caching. For this a new repository rule is introduced that downloads the CLDR JSON repository and extracts it. Within that rule we determine the supported locales so that they can be used to pre-declare outputs (for the locales) within Bazel analysis phase. This allows us to add the generated locale files to a `ts_library` (which we want to have for better testing, and consistent JS transpilation). Note that the removal of `cldr-data-downloader` also requires us to add logic for detecting locales without data. The CLDR data downloader overwrote the `availableLocales.json` file with a file that only lists locales that CLDR provides data for. We use the official `availableLocales` file CLDR provides, but filter out locales for which no data is available. This is needed until we update to CLDR 39 where data is available for all such locales listed in `availableLocales.json`. PR Close #42230
2021-05-21 16:57:42 -04:00
/**
* @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';
import {fileHeader} from './file-header';
import {BaseCurrencies} from './locale-base-currencies';
import {generateDayPeriodsSupplementalString} from './locale-extra-file';
import {generateBasicLocaleString} from './locale-file';
import {getPluralFunction} from './plural-function';
/**
* Generated the contents for the global locale file
*/
export function generateLocaleGlobalFile(
locale: string, localeData: CldrLocaleData, baseCurrencies: BaseCurrencies) {
const basicLocaleData = generateBasicLocaleString(locale, localeData, baseCurrencies);
const extraLocaleData = generateDayPeriodsSupplementalString(locale, localeData);
const data = basicLocaleData.replace(/\]$/, `, ${extraLocaleData}]`);
return `${fileHeader}
(function(global) {
global.ng = global.ng || {};
global.ng.common = global.ng.common || {};
global.ng.common.locales = global.ng.common.locales || {};
const u = undefined;
${getPluralFunction(localeData, false)}
build: convert CLDR locale extraction from Gulp to Bazel tool (#42230) Converts the CLDR locale extraction script to a Bazel tool. This allows us to generate locale files within Bazel, so that locales don't need to live as sources within the repo. Also it allows us to get rid of the legacy Gulp tooling. The migration of the Gulp script to a Bazel tool involved the following things: 1. Basic conversion of the `extract.js` script to TypeScript. This mostly was about adding explicit types. e.g. adding `locale: string` or `localeData: CldrStatic`. 2. Split-up into separate files. Instead of keeping the large `extract.js` file, the tool has been split into separate files. The logic remains the same, just that code is more readable and maintainable. 3. Introduction of a new `index.ts` file that is the entry-point for the Bazel tool. Previously the Gulp tool just generated all locale files, the default locale and base currency files at once. The new entry-point accepts a mode to be passed as first process argument. based on that argument, either locales are generated into a specified directory, or the default locale, base currencies or closure file is generated. This allows us to generate files with a Bazel genrule where we simply run the tool and specify the outputs. Note: It's necessary to have multiple modes because files live in separate locations. e.g. the default locale in `@angular/core`, but the rest in `@angular/common`. 4. Removal of the `cldr-data-downloader` and custom CLDR resolution logic. Within Bazel we cannot run a downloader using network. We switch this to something more Bazel idiomatic with better caching. For this a new repository rule is introduced that downloads the CLDR JSON repository and extracts it. Within that rule we determine the supported locales so that they can be used to pre-declare outputs (for the locales) within Bazel analysis phase. This allows us to add the generated locale files to a `ts_library` (which we want to have for better testing, and consistent JS transpilation). Note that the removal of `cldr-data-downloader` also requires us to add logic for detecting locales without data. The CLDR data downloader overwrote the `availableLocales.json` file with a file that only lists locales that CLDR provides data for. We use the official `availableLocales` file CLDR provides, but filter out locales for which no data is available. This is needed until we update to CLDR 39 where data is available for all such locales listed in `availableLocales.json`. PR Close #42230
2021-05-21 16:57:42 -04:00
global.ng.common.locales['${normalizeLocale(locale)}'] = ${data};
})(typeof globalThis !== 'undefined' && globalThis || typeof global !== 'undefined' && global || typeof window !== 'undefined' && window);
`;
}
/**
* In Angular the locale is referenced by a "normalized" form.
*/
function normalizeLocale(locale: string): string {
return locale.toLowerCase().replace(/_/g, '-');
}