This reverts commit 044e0229bd
.
PR Close #42583
This commit is contained in:
parent
b52e93543f
commit
d6cca3cf9d
File diff suppressed because it is too large
Load Diff
|
@ -31,9 +31,6 @@ const CLDR_DATA_GLOBS = [
|
|||
/** Path to the CLDR available locales file. */
|
||||
const CLDR_AVAILABLE_LOCALES_PATH = 'cldr-core-37.0.0/availableLocales.json';
|
||||
|
||||
/** Path to the CLDR locale aliases file. */
|
||||
const CLDR_LOCALE_ALIASES_PATH = 'cldr-core-37.0.0/supplemental/aliases.json';
|
||||
|
||||
/**
|
||||
* Instance providing access to a locale's CLDR data. This type extends the `cldrjs`
|
||||
* instance type with the missing `bundle` attribute property.
|
||||
|
@ -48,13 +45,6 @@ export type CldrLocaleData = CldrStatic&{
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Possible reasons for an alias in the CLDR supplemental data. See:
|
||||
* https://unicode.org/reports/tr35/tr35-info.html#Appendix_Supplemental_Metadata.
|
||||
*/
|
||||
export type CldrLocaleAliasReason =
|
||||
'deprecated'|'overlong'|'macrolanguage'|'legacy'|'bibliographic';
|
||||
|
||||
/**
|
||||
* Class that provides access to the CLDR data downloaded as part of
|
||||
* the `@cldr_data` Bazel repository.
|
||||
|
@ -87,16 +77,6 @@ export class CldrData {
|
|||
return localeData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the CLDR language aliases.
|
||||
* http://cldr.unicode.org/index/cldr-spec/language-tag-equivalences.
|
||||
*/
|
||||
getLanguageAliases():
|
||||
{[localeName: string]: {_reason: CldrLocaleAliasReason, _replacement: string}} {
|
||||
return require(`${this.cldrDataDir}/${CLDR_LOCALE_ALIASES_PATH}`)
|
||||
.supplemental.metadata.alias.languageAlias;
|
||||
}
|
||||
|
||||
/** Gets a list of all locales CLDR provides data for. */
|
||||
private _getAvailableLocales(): CldrLocaleData[] {
|
||||
const allLocales =
|
||||
|
|
|
@ -6,45 +6,35 @@
|
|||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {CldrData, CldrLocaleData} from './cldr-data';
|
||||
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';
|
||||
|
||||
interface ClosureLocale {
|
||||
/** Locale name to match with a Closure-supported locale. */
|
||||
closureLocaleName: string;
|
||||
/** Locale data. Can have a different locale name if this captures an aliased locale. */
|
||||
data: CldrLocaleData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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: ClosureLocale[] =
|
||||
[...cldrData.availableLocales.map(data => ({closureLocaleName: data.locale, data}))];
|
||||
const aliases = cldrData.getLanguageAliases();
|
||||
const locales = cldrData.availableLocales;
|
||||
|
||||
// We also generate locale data for aliases known within CLDR. Closure compiler does not
|
||||
// limit its locale identifiers to CLDR-canonical identifiers/or BCP47 identifiers.
|
||||
// To ensure deprecated/historical locale identifiers which are supported by Closure
|
||||
// can work with closure-compiled Angular applications, we respect CLDR locale aliases.
|
||||
for (const [aliasName, data] of Object.entries(aliases)) {
|
||||
// We skip bibliographic aliases as those have never been supported by Closure compiler.
|
||||
if (data._reason === 'bibliographic') {
|
||||
continue;
|
||||
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*/, '');
|
||||
}
|
||||
|
||||
const localeData = cldrData.getLocaleData(data._replacement);
|
||||
|
||||
// If CLDR does not provide data for the replacement locale, we skip this alias.
|
||||
if (localeData === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
locales.push({closureLocaleName: aliasName, data: localeData});
|
||||
function generateCase(localeName: string) {
|
||||
return `case '${localeName}':\n` +
|
||||
`l = locale_${formatLocale(localeName)};\n` +
|
||||
`break;\n`;
|
||||
}
|
||||
|
||||
return `${fileHeader}
|
||||
|
@ -58,28 +48,12 @@ ${locales.map(locale => `${generateLocaleConstant(locale)}`).join('\n')}
|
|||
let l: any;
|
||||
|
||||
switch (goog.LOCALE) {
|
||||
${locales.map(locale => generateCase(locale)).join('')}}
|
||||
${locales.map(localeData => generateCase(localeData.locale)).join('')}}
|
||||
|
||||
if (l) {
|
||||
registerLocaleData(l);
|
||||
}
|
||||
`;
|
||||
|
||||
function generateLocaleConstant(locale: ClosureLocale): string {
|
||||
const localeNameFormattedForJs = formatLocale(locale.closureLocaleName);
|
||||
return generateLocale(locale.closureLocaleName, locale.data, 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(locale: ClosureLocale) {
|
||||
return `case '${locale.closureLocaleName}':\n` +
|
||||
`l = locale_${formatLocale(locale.closureLocaleName)};\n` +
|
||||
`break;\n`;
|
||||
}
|
||||
}
|
||||
|
||||
function formatLocale(locale: string): string {
|
||||
|
|
|
@ -23,7 +23,7 @@ export function generateLocale(
|
|||
return `${fileHeader}
|
||||
const u = undefined;
|
||||
|
||||
${getPluralFunction(localeData)}
|
||||
${getPluralFunction(locale)}
|
||||
|
||||
export default ${generateBasicLocaleString(locale, localeData, baseCurrencies)};
|
||||
`;
|
||||
|
|
|
@ -27,7 +27,7 @@ export function generateLocaleGlobalFile(
|
|||
global.ng.common = global.ng.common || {};
|
||||
global.ng.common.locales = global.ng.common.locales || {};
|
||||
const u = undefined;
|
||||
${getPluralFunction(localeData, false)}
|
||||
${getPluralFunction(locale, false)}
|
||||
global.ng.common.locales['${normalizeLocale(locale)}'] = ${data};
|
||||
})(typeof globalThis !== 'undefined' && globalThis || typeof global !== 'undefined' && global || typeof window !== 'undefined' && window);
|
||||
`;
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
* 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');
|
||||
|
@ -15,13 +14,8 @@ const cldr = require('cldr');
|
|||
* 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();
|
||||
export function getPluralFunction(locale: string, withTypes = true) {
|
||||
let fn = cldr.extractPluralRuleFunction(locale).toString();
|
||||
|
||||
const numberType = withTypes ? ': number' : '';
|
||||
fn =
|
||||
|
|
Loading…
Reference in New Issue