refactor(common): export locale data from closure-locale (#18973)
PR Close #18973
This commit is contained in:
parent
cf7d47dda0
commit
57abe8d078
File diff suppressed because it is too large
Load Diff
@ -68,21 +68,24 @@ module.exports = (gulp, done) => {
|
|||||||
* Tree shaking will only keep the data for the `goog.LOCALE` locale.
|
* Tree shaking will only keep the data for the `goog.LOCALE` locale.
|
||||||
*/
|
*/
|
||||||
function generateAllLocalesFile(LOCALES, ALIASES) {
|
function generateAllLocalesFile(LOCALES, ALIASES) {
|
||||||
function generateCases(locale) {
|
const existingLocalesAliases = {};
|
||||||
let str = '';
|
const existingLocalesData = {};
|
||||||
let localeData;
|
|
||||||
const equivalentLocales = [locale];
|
// for each locale, get the data and the list of equivalent locales
|
||||||
|
LOCALES.forEach(locale => {
|
||||||
|
const eqLocales = new Set();
|
||||||
|
eqLocales.add(locale);
|
||||||
if (locale.match(/-/)) {
|
if (locale.match(/-/)) {
|
||||||
equivalentLocales.push(locale.replace(/-/g, '_'));
|
eqLocales.add(locale.replace(/-/g, '_'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for aliases
|
// check for aliases
|
||||||
const alias = ALIASES[locale];
|
const alias = ALIASES[locale];
|
||||||
if (alias) {
|
if (alias) {
|
||||||
equivalentLocales.push(alias);
|
eqLocales.add(alias);
|
||||||
|
|
||||||
if (alias.match(/-/)) {
|
if (alias.match(/-/)) {
|
||||||
equivalentLocales.push(alias.replace(/-/g, '_'));
|
eqLocales.add(alias.replace(/-/g, '_'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// to avoid duplicated "case" we regroup all locales in the same "case"
|
// to avoid duplicated "case" we regroup all locales in the same "case"
|
||||||
@ -92,33 +95,47 @@ function generateAllLocalesFile(LOCALES, ALIASES) {
|
|||||||
const aliasKeys = Object.keys(ALIASES);
|
const aliasKeys = Object.keys(ALIASES);
|
||||||
for (let i = 0; i < aliasKeys.length; i++) {
|
for (let i = 0; i < aliasKeys.length; i++) {
|
||||||
const aliasValue = ALIASES[alias];
|
const aliasValue = ALIASES[alias];
|
||||||
if (aliasKeys.indexOf(alias) !== -1 && equivalentLocales.indexOf(aliasValue) === -1) {
|
if (aliasKeys.indexOf(alias) !== -1 && !eqLocales.has(aliasValue)) {
|
||||||
equivalentLocales.push(aliasValue);
|
eqLocales.add(aliasValue);
|
||||||
|
|
||||||
if (aliasValue.match(/-/)) {
|
if (aliasValue.match(/-/)) {
|
||||||
equivalentLocales.push(aliasValue.replace(/-/g, '_'));
|
eqLocales.add(aliasValue.replace(/-/g, '_'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < equivalentLocales.length; i++) {
|
for (let l of eqLocales) {
|
||||||
str += `case '${equivalentLocales[i]}':\n`;
|
|
||||||
|
|
||||||
// find the existing content file
|
// find the existing content file
|
||||||
const path = `${RELATIVE_I18N_DATA_FOLDER}/${equivalentLocales[i]}.ts`;
|
const path = `${RELATIVE_I18N_DATA_FOLDER}/${l}.ts`;
|
||||||
if (fs.existsSync(`${RELATIVE_I18N_DATA_FOLDER}/${equivalentLocales[i]}.ts`)) {
|
if (fs.existsSync(`${RELATIVE_I18N_DATA_FOLDER}/${l}.ts`)) {
|
||||||
localeData = fs.readFileSync(path, 'utf8').replace(`${HEADER}\nexport default `, '');
|
existingLocalesData[locale] =
|
||||||
|
fs.readFileSync(path, 'utf8').replace(`${HEADER}\nexport default `, '');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
str += ` l = ${localeData}break;\n`;
|
existingLocalesAliases[locale] = eqLocales;
|
||||||
|
});
|
||||||
|
|
||||||
|
function generateCases(locale) {
|
||||||
|
let str = '';
|
||||||
|
const eqLocales = existingLocalesAliases[locale];
|
||||||
|
for (let l of eqLocales) {
|
||||||
|
str += `case '${l}':\n`;
|
||||||
|
}
|
||||||
|
|
||||||
|
str += ` l = ${formatLocale(locale)};
|
||||||
|
break;\n`;
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatLocale(locale) { return `locale_${locale.replace(/-/g, '_')}`; }
|
||||||
// clang-format off
|
// clang-format off
|
||||||
return `${HEADER}
|
return `${HEADER}
|
||||||
import {registerLocaleData} from '../src/i18n/locale_data';
|
import {registerLocaleData} from '../src/i18n/locale_data';
|
||||||
|
|
||||||
|
${LOCALES.map(locale => `export const ${formatLocale(locale)} = ${existingLocalesData[locale]}`).join('\n')}
|
||||||
|
|
||||||
let l: any;
|
let l: any;
|
||||||
|
|
||||||
switch (goog.LOCALE) {
|
switch (goog.LOCALE) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user