In the past, the closure file has been generated so that all individual locale files were imported individually. This resulted in a huge slow-down in g3 due to the large amount of imports. With 90bd984ff74f7605d7c08fd9fdbf610ba7fa67a5 this changed so that we inline the locale data for the g3 closure locale file. Also the file only contained data for locales being supported by Closure. For this a list of locales has been extracted from Closure Compiler, as well as a list of locale aliases. This logic is prone to CLDR version updates, and also broke as part of the Gulp -> Bazel migration where this logic has been slightly modified but caused issues in G3. e.g. a locale `zh-Hant` was requested in g3, but the locale data had the name of the alias locale that provided the data at index zero (which represents the locale name). Note that the locale names at index zero always could differentiate from the requested `goog.LOCALE` due to the aliasing logic. This just didn't come up before. We simplify this logic by generating a `goog.LOCALE` case for all locales CLDR provides data for. We don't need to bother about aliasing because with the refactorings to the CLDR generation tool, all locales are built (which also captures the aliases), and we can generate the locale file on the fly (which has not been done before). PR Close #42230
62 lines
1.8 KiB
TypeScript
62 lines
1.8 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 {CldrStatic} from 'cldrjs';
|
|
|
|
import {CldrData} from './cldr-data';
|
|
import {fileHeader} from './file-header';
|
|
import {BaseCurrencies} from './locale-base-currencies';
|
|
import {generateLocale} from './locale-file';
|
|
|
|
/**
|
|
* Generate a file that contains all locale to import for closure.
|
|
* Tree shaking will only keep the data for the `goog.LOCALE` locale.
|
|
*/
|
|
export function generateClosureLocaleFile(cldrData: CldrData, baseCurrencies: BaseCurrencies) {
|
|
const locales = cldrData.availableLocales;
|
|
|
|
function generateLocaleConstant(localeData: CldrStatic): string {
|
|
const locale = localeData.locale;
|
|
const localeNameFormattedForJs = formatLocale(locale);
|
|
return generateLocale(locale, localeData, baseCurrencies)
|
|
.replace(`${fileHeader}\n`, '')
|
|
.replace('export default ', `export const locale_${localeNameFormattedForJs} = `)
|
|
.replace('function plural', `function plural_${localeNameFormattedForJs}`)
|
|
.replace(/,\s+plural/, `, plural_${localeNameFormattedForJs}`)
|
|
.replace(/\s*const u = undefined;\s*/, '');
|
|
}
|
|
|
|
function generateCase(localeName: string) {
|
|
return `case '${localeName}':\n` +
|
|
`l = locale_${formatLocale(localeName)};\n` +
|
|
`break;\n`;
|
|
}
|
|
|
|
return `${fileHeader}
|
|
|
|
import {registerLocaleData} from '../src/i18n/locale_data';
|
|
|
|
const u = undefined;
|
|
|
|
${locales.map(locale => `${generateLocaleConstant(locale)}`).join('\n')}
|
|
|
|
let l: any;
|
|
|
|
switch (goog.LOCALE) {
|
|
${locales.map(localeData => generateCase(localeData.locale)).join('')}}
|
|
|
|
if (l) {
|
|
registerLocaleData(l);
|
|
}
|
|
`;
|
|
}
|
|
|
|
function formatLocale(locale: string): string {
|
|
return locale.replace(/-/g, '_');
|
|
}
|