Skip to content

Commit

Permalink
Revert "build: generate alias locale data for closure locale (#42230)" (
Browse files Browse the repository at this point in the history
#42583)

This reverts commit 044e022.

PR Close #42583
  • Loading branch information
alxhub committed Jun 16, 2021
1 parent b52e935 commit d6cca3c
Show file tree
Hide file tree
Showing 6 changed files with 1,113 additions and 4,961 deletions.
5,976 changes: 1,090 additions & 4,886 deletions packages/common/locales/closure-locale.ts

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions packages/common/locales/generate-locales-tool/cldr-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

const localeData = cldrData.getLocaleData(data._replacement);

// If CLDR does not provide data for the replacement locale, we skip this alias.
if (localeData === null) {
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*/, '');
}

locales.push({closureLocaleName: aliasName, data: localeData});
function generateCase(localeName: string) {
return `case '${localeName}':\n` +
`l = locale_${formatLocale(localeName)};\n` +
`break;\n`;
}

return `${fileHeader}
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function generateLocale(
return `${fileHeader}
const u = undefined;
${getPluralFunction(localeData)}
${getPluralFunction(locale)}
export default ${generateBasicLocaleString(locale, localeData, baseCurrencies)};
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
`;
Expand Down
10 changes: 2 additions & 8 deletions packages/common/locales/generate-locales-tool/plural-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 =
Expand Down

0 comments on commit d6cca3c

Please sign in to comment.