refactor(common): simplify closure-locale by removing imports (#18926)

This commit is contained in:
Olivier Combe 2017-08-29 20:54:43 +02:00 committed by Victor Berchet
parent 48789360b1
commit 90bd984ff7
2 changed files with 4687 additions and 2230 deletions

File diff suppressed because it is too large Load Diff

View File

@ -7,26 +7,54 @@
*/
const fs = require('fs');
const path = require('path');
const yargs = require('yargs').argv;
const {I18N_FOLDER, I18N_DATA_FOLDER, RELATIVE_I18N_DATA_FOLDER, HEADER} = require('./extract');
const OUTPUT_NAME = `closure-locale.ts`;
module.exports = (gulp, done) => {
let GOOG_LOCALES;
// the locales used by closure that will be used to generate the closure-locale file
// extracted from:
// https://github.com/google/closure-library/blob/master/closure/goog/i18n/datetimepatterns.js#L2136
let GOOG_LOCALES = [
'af', 'am', 'ar', 'ar-DZ', 'az', 'be', 'bg', 'bn', 'br', 'bs',
'ca', 'chr', 'cs', 'cy', 'da', 'de', 'de-AT', 'de-CH', 'el', 'en-AU',
'en-CA', 'en-GB', 'en-IE', 'en-IN', 'en-SG', 'en-ZA', 'es', 'es-419', 'es-MX', 'es-US',
'et', 'eu', 'fa', 'fi', 'fr', 'fr-CA', 'ga', 'gl', 'gsw', 'gu',
'haw', 'hi', 'hr', 'hu', 'hy', 'in', 'is', 'it', 'iw', 'ja',
'ka', 'kk', 'km', 'kn', 'ko', 'ky', 'ln', 'lo', 'lt', 'lv',
'mk', 'ml', 'mn', 'mo', 'mr', 'ms', 'mt', 'my', 'ne', 'nl',
'no', 'or', 'pa', 'pl', 'pt', 'pt-PT', 'ro', 'ru', 'sh', 'si',
'sk', 'sl', 'sq', 'sr', 'sv', 'sw', 'ta', 'te', 'th', 'tl',
'tr', 'uk', 'ur', 'uz', 'vi', 'zh', 'zh-CN', 'zh-HK', 'zh-TW', 'zu'
];
// locale id aliases to support deprecated locale ids used by closure
// it maps deprecated ids --> new ids
// manually extracted from ./cldr-data/supplemental/aliases.json
const ALIASES = {
'in': 'id',
'iw': 'he',
'mo': 'ro-MD',
'no': 'nb',
'nb': 'no-NO',
'sh': 'sr-Latn',
'tl': 'fil',
'pt': 'pt-BR',
'zh-CN': 'zh-Hans-CN',
'zh-Hans-CN': 'zh-Hans',
'zh-HK': 'zh-Hant-HK',
'zh-Hant-HK': 'zh-Hant',
'zh-TW': 'zh-Hant-TW',
'zh-Hant-TW': 'zh-Hant'
};
if (yargs.locales) {
GOOG_LOCALES = yargs.locales.split(',');
} else {
if (!fs.existsSync(path.join(__dirname, 'cldr-data'))) {
throw new Error(`You must run "gulp cldr:download" before you can extract the data`);
}
const cldrData = require('./cldr-data');
GOOG_LOCALES = cldrData.availableLocales;
}
console.log(`Writing file ${I18N_DATA_FOLDER}/${OUTPUT_NAME}`);
fs.writeFileSync(
`${RELATIVE_I18N_DATA_FOLDER}/${OUTPUT_NAME}`, generateAllLocalesFile(GOOG_LOCALES));
`${RELATIVE_I18N_DATA_FOLDER}/${OUTPUT_NAME}`, generateAllLocalesFile(GOOG_LOCALES, ALIASES));
console.log(`Formatting ${I18N_DATA_FOLDER}/${OUTPUT_NAME}..."`);
const format = require('gulp-clang-format');
@ -40,44 +68,59 @@ module.exports = (gulp, done) => {
* Generate a file that contains all locale to import for closure.
* Tree shaking will only keep the data for the `goog.LOCALE` locale.
*/
function generateAllLocalesFile(LOCALES) {
function generateAllLocalesFile(LOCALES, ALIASES) {
function generateCases(locale) {
let str = '';
let localeData;
const equivalentLocales = [locale];
if (locale.match(/-/)) {
str = `case '${locale.replace('-', '_')}':\n`
equivalentLocales.push(locale.replace('-', '_'));
}
// clang-format off
str += `case '${locale}':
l = ${toCamelCase(locale)};
break;
`;
// clang-format on
// check for aliases
const alias = ALIASES[locale];
if (alias) {
equivalentLocales.push(alias);
// to avoid duplicated "case" we regroup all locales in the same "case"
// the simplest way to do that is to have alias aliases
// e.g. 'no' --> 'nb', 'nb' --> 'no-NO'
// which means that we'll have 'no', 'nb' and 'no-NO' in the same "case"
const aliasKeys = Object.keys(ALIASES);
for (let i = 0; i < aliasKeys.length; i++) {
const aliasValue = ALIASES[alias];
if (aliasKeys.indexOf(alias) !== -1 && equivalentLocales.indexOf(aliasValue) === -1) {
equivalentLocales.push(aliasValue);
}
}
}
for (let i = 0; i < equivalentLocales.length; i++) {
str += `case '${equivalentLocales[i]}':\n`;
// find the existing content file
const path = `${RELATIVE_I18N_DATA_FOLDER}/${equivalentLocales[i]}.ts`;
if (fs.existsSync(`${RELATIVE_I18N_DATA_FOLDER}/${equivalentLocales[i]}.ts`)) {
localeData = fs.readFileSync(path, 'utf8').replace(`${HEADER}\nexport default `, '');
}
}
str += ` l = ${localeData}break;\n`;
return str;
}
// clang-format off
return `${HEADER}
import {registerLocaleData} from '../src/i18n/locale_data';
${LOCALES.map(locale => `import ${toCamelCase(locale)} from './${locale}';\n`).join('')}
let l: any;
switch (goog.LOCALE) {
${LOCALES.map(locale => generateCases(locale)).join('')}
default:
l = en;
}
switch (goog.LOCALE.replace(/_/g, '-')) {
${LOCALES.map(locale => generateCases(locale)).join('')}}
registerLocaleData(l);
if(l) {
l[0] = goog.LOCALE;
registerLocaleData(l);
}
`;
// clang-format on
}
/**
* Transform a string to camelCase
*/
function toCamelCase(str) {
return str.replace(/-+([a-z0-9A-Z])/g, (...m) => m[1].toUpperCase());
}
module.exports.I18N_FOLDER = I18N_FOLDER;
module.exports.I18N_DATA_FOLDER = I18N_DATA_FOLDER;