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. */
|
/** Path to the CLDR available locales file. */
|
||||||
const CLDR_AVAILABLE_LOCALES_PATH = 'cldr-core-37.0.0/availableLocales.json';
|
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 providing access to a locale's CLDR data. This type extends the `cldrjs`
|
||||||
* instance type with the missing `bundle` attribute property.
|
* 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
|
* Class that provides access to the CLDR data downloaded as part of
|
||||||
* the `@cldr_data` Bazel repository.
|
* the `@cldr_data` Bazel repository.
|
||||||
|
@ -87,16 +77,6 @@ export class CldrData {
|
||||||
return localeData;
|
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. */
|
/** Gets a list of all locales CLDR provides data for. */
|
||||||
private _getAvailableLocales(): CldrLocaleData[] {
|
private _getAvailableLocales(): CldrLocaleData[] {
|
||||||
const allLocales =
|
const allLocales =
|
||||||
|
|
|
@ -6,45 +6,35 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* 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 {fileHeader} from './file-header';
|
||||||
import {BaseCurrencies} from './locale-base-currencies';
|
import {BaseCurrencies} from './locale-base-currencies';
|
||||||
import {generateLocale} from './locale-file';
|
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.
|
* Generate a file that contains all locale to import for closure.
|
||||||
* Tree shaking will only keep the data for the `goog.LOCALE` locale.
|
* Tree shaking will only keep the data for the `goog.LOCALE` locale.
|
||||||
*/
|
*/
|
||||||
export function generateClosureLocaleFile(cldrData: CldrData, baseCurrencies: BaseCurrencies) {
|
export function generateClosureLocaleFile(cldrData: CldrData, baseCurrencies: BaseCurrencies) {
|
||||||
const locales: ClosureLocale[] =
|
const locales = cldrData.availableLocales;
|
||||||
[...cldrData.availableLocales.map(data => ({closureLocaleName: data.locale, data}))];
|
|
||||||
const aliases = cldrData.getLanguageAliases();
|
|
||||||
|
|
||||||
// We also generate locale data for aliases known within CLDR. Closure compiler does not
|
function generateLocaleConstant(localeData: CldrStatic): string {
|
||||||
// limit its locale identifiers to CLDR-canonical identifiers/or BCP47 identifiers.
|
const locale = localeData.locale;
|
||||||
// To ensure deprecated/historical locale identifiers which are supported by Closure
|
const localeNameFormattedForJs = formatLocale(locale);
|
||||||
// can work with closure-compiled Angular applications, we respect CLDR locale aliases.
|
return generateLocale(locale, localeData, baseCurrencies)
|
||||||
for (const [aliasName, data] of Object.entries(aliases)) {
|
.replace(`${fileHeader}\n`, '')
|
||||||
// We skip bibliographic aliases as those have never been supported by Closure compiler.
|
.replace('export default ', `export const locale_${localeNameFormattedForJs} = `)
|
||||||
if (data._reason === 'bibliographic') {
|
.replace('function plural', `function plural_${localeNameFormattedForJs}`)
|
||||||
continue;
|
.replace(/,\s+plural/, `, plural_${localeNameFormattedForJs}`)
|
||||||
}
|
.replace(/\s*const u = undefined;\s*/, '');
|
||||||
|
}
|
||||||
|
|
||||||
const localeData = cldrData.getLocaleData(data._replacement);
|
function generateCase(localeName: string) {
|
||||||
|
return `case '${localeName}':\n` +
|
||||||
// If CLDR does not provide data for the replacement locale, we skip this alias.
|
`l = locale_${formatLocale(localeName)};\n` +
|
||||||
if (localeData === null) {
|
`break;\n`;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
locales.push({closureLocaleName: aliasName, data: localeData});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${fileHeader}
|
return `${fileHeader}
|
||||||
|
@ -58,28 +48,12 @@ ${locales.map(locale => `${generateLocaleConstant(locale)}`).join('\n')}
|
||||||
let l: any;
|
let l: any;
|
||||||
|
|
||||||
switch (goog.LOCALE) {
|
switch (goog.LOCALE) {
|
||||||
${locales.map(locale => generateCase(locale)).join('')}}
|
${locales.map(localeData => generateCase(localeData.locale)).join('')}}
|
||||||
|
|
||||||
if (l) {
|
if (l) {
|
||||||
registerLocaleData(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 {
|
function formatLocale(locale: string): string {
|
||||||
|
|
|
@ -23,7 +23,7 @@ export function generateLocale(
|
||||||
return `${fileHeader}
|
return `${fileHeader}
|
||||||
const u = undefined;
|
const u = undefined;
|
||||||
|
|
||||||
${getPluralFunction(localeData)}
|
${getPluralFunction(locale)}
|
||||||
|
|
||||||
export default ${generateBasicLocaleString(locale, localeData, baseCurrencies)};
|
export default ${generateBasicLocaleString(locale, localeData, baseCurrencies)};
|
||||||
`;
|
`;
|
||||||
|
|
|
@ -27,7 +27,7 @@ export function generateLocaleGlobalFile(
|
||||||
global.ng.common = global.ng.common || {};
|
global.ng.common = global.ng.common || {};
|
||||||
global.ng.common.locales = global.ng.common.locales || {};
|
global.ng.common.locales = global.ng.common.locales || {};
|
||||||
const u = undefined;
|
const u = undefined;
|
||||||
${getPluralFunction(localeData, false)}
|
${getPluralFunction(locale, false)}
|
||||||
global.ng.common.locales['${normalizeLocale(locale)}'] = ${data};
|
global.ng.common.locales['${normalizeLocale(locale)}'] = ${data};
|
||||||
})(typeof globalThis !== 'undefined' && globalThis || typeof global !== 'undefined' && global || typeof window !== 'undefined' && window);
|
})(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
|
* 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
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import {CldrLocaleData} from './cldr-data';
|
|
||||||
|
|
||||||
// There are no types available for `cldr`.
|
// There are no types available for `cldr`.
|
||||||
const cldr = require('cldr');
|
const cldr = require('cldr');
|
||||||
|
@ -15,13 +14,8 @@ const cldr = require('cldr');
|
||||||
* TODO(ocombe): replace "cldr" extractPluralRuleFunction with our own extraction using "CldrJS"
|
* 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
|
* 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) {
|
export function getPluralFunction(locale: string, withTypes = true) {
|
||||||
// We use the resolved bundle for extracting the plural function. This matches with the
|
let fn = cldr.extractPluralRuleFunction(locale).toString();
|
||||||
// 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' : '';
|
const numberType = withTypes ? ': number' : '';
|
||||||
fn =
|
fn =
|
||||||
|
|
Loading…
Reference in New Issue