refactor(common): refactor i18n code to ease tree shaking (#18907)

PR Close #18907
This commit is contained in:
Victor Berchet
2017-08-23 17:10:27 -07:00
committed by Miško Hevery
parent 4c45347635
commit 832876d0a1
9 changed files with 85 additions and 98 deletions

View File

@ -58,7 +58,7 @@ module.exports = (gulp, done) => {
console.log(`${index + 1}/${LOCALES.length}`);
console.log(`\t${I18N_DATA_FOLDER}/locale_${locale}.ts`);
fs.writeFileSync(`${RELATIVE_I18N_DATA_FOLDER}/locale_${locale}.ts`, generateLocale(locale, localeData, '@angular/common'));
fs.writeFileSync(`${RELATIVE_I18N_DATA_FOLDER}/locale_${locale}.ts`, generateLocale(locale, localeData));
console.log(`\t${I18N_DATA_EXTRA_FOLDER}/locale_${locale}.ts`);
fs.writeFileSync(`${RELATIVE_I18N_DATA_EXTRA_FOLDER}/locale_${locale}.ts`, generateLocaleExtra(locale, localeData));
});
@ -66,7 +66,7 @@ module.exports = (gulp, done) => {
// additional "en" file that will be included in common
console.log(`Writing file ${I18N_FOLDER}/locale_en.ts`);
fs.writeFileSync(`${RELATIVE_I18N_FOLDER}/locale_en.ts`, generateLocale('en', new cldrJs('en'), './locale_data'));
fs.writeFileSync(`${RELATIVE_I18N_FOLDER}/locale_en.ts`, generateLocale('en', new cldrJs('en')));
console.log(`Writing file ${I18N_FOLDER}/currencies.ts`);
fs.writeFileSync(`${RELATIVE_I18N_FOLDER}/currencies.ts`, generateCurrencies());
@ -87,7 +87,7 @@ module.exports = (gulp, done) => {
/**
* Generate file that contains basic locale data
*/
function generateLocale(locale, localeData, ngLocalePath) {
function generateLocale(locale, localeData) {
// [ localeId, dateTime, number, currency, pluralCase ]
let data = stringify([
locale,
@ -103,8 +103,6 @@ function generateLocale(locale, localeData, ngLocalePath) {
data = data.substring(0, data.lastIndexOf(']')) + `, ${getPluralFunction(locale)}]`;
return `${HEADER}
import {Plural} from '${ngLocalePath}';
export default ${data};
`;
}
@ -450,24 +448,28 @@ function toRegExp(s) {
*/
function getPluralFunction(locale) {
let fn = cldr.extractPluralRuleFunction(locale).toString();
if (fn === EMPTY_RULE) {
fn = DEFAULT_RULE;
}
return fn
fn = fn
.replace(
toRegExp('function anonymous(n\n/**/) {\n'),
'function(n: number): Plural {\n ')
'function(n: number): number {\n ')
.replace(toRegExp('var'), 'let')
.replace(toRegExp('if(typeof n==="string")n=parseInt(n,10);'), '')
.replace(toRegExp('"zero"'), ' Plural.Zero')
.replace(toRegExp('"one"'), ' Plural.One')
.replace(toRegExp('"two"'), ' Plural.Two')
.replace(toRegExp('"few"'), ' Plural.Few')
.replace(toRegExp('"many"'), ' Plural.Many')
.replace(toRegExp('"other"'), ' Plural.Other')
.replace(toRegExp('\n}'), ';\n}');
return normalizePluralRule();
// The replacement values must match the `Plural` enum from common.
// We do not use the enum directly to avoid depending on that package.
return fn
.replace(toRegExp('"zero"'), ' 0')
.replace(toRegExp('"one"'), ' 1')
.replace(toRegExp('"two"'), ' 2')
.replace(toRegExp('"few"'), ' 3')
.replace(toRegExp('"many"'), ' 4')
.replace(toRegExp('"other"'), ' 5');
}
/**
@ -484,13 +486,6 @@ function stringify(obj) {
return util.inspect(obj, {depth: null, maxArrayLength: null})
}
/**
* Transform a string to camelCase
*/
function toCamelCase(str) {
return str.replace(/-+([a-z0-9A-Z])/g, (...m) => m[1].toUpperCase());
}
/**
* To create smaller locale files, we remove duplicated data.
* To be make this work we need to store similar data in arrays, if some value in an array

View File

@ -69,9 +69,6 @@ export declare class DeprecatedPercentPipe implements PipeTransform {
/** @stable */
export declare const DOCUMENT: InjectionToken<Document>;
/** @experimental */
export declare function findLocaleData(locale: string): any;
/** @experimental */
export declare enum FormatWidth {
Short = 0,
@ -182,11 +179,6 @@ export declare class JsonPipe implements PipeTransform {
transform(value: any): string;
}
/** @experimental */
export declare const LOCALE_DATA: {
[localeId: string]: any;
};
/** @stable */
export declare class Location {
constructor(platformStrategy: LocationStrategy);