function generateAllLocalesFile(LOCALES, ALIASES) {
  const existingLocalesAliases = {};
  const existingLocalesData = {};

  // 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(/-/)) {
      eqLocales.add(locale.replace(/-/g, '_'));
    }

    // check for aliases
    const alias = ALIASES[locale];
    if (alias) {
      eqLocales.add(alias);

      if (alias.match(/-/)) {
        eqLocales.add(alias.replace(/-/g, '_'));
      }

      // 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 && !eqLocales.has(aliasValue)) {
          eqLocales.add(aliasValue);

          if (aliasValue.match(/-/)) {
            eqLocales.add(aliasValue.replace(/-/g, '_'));
          }
        }
      }
    }

    for (let l of eqLocales) {
      // find the existing content file
      const path = `${RELATIVE_I18N_DATA_FOLDER}/${l}.ts`;
      if (fs.existsSync(`${RELATIVE_I18N_DATA_FOLDER}/${l}.ts`)) {
        const localeName = formatLocale(locale);
        existingLocalesData[locale] =
            fs.readFileSync(path, 'utf8')
                .replace(`${HEADER}\n`, '')
                .replace('export default ', `export const locale_${localeName} = `)
                .replace('function plural', `function plural_${localeName}`)
                .replace(/,(\n  | )plural/, `, plural_${localeName}`)
                .replace('const u = undefined;\n\n', '');
      }
    }

    existingLocalesAliases[locale] = eqLocales;
  });

  function generateCases(locale) {
    let str = '';
    let locales = [];
    const eqLocales = existingLocalesAliases[locale];
    for (let l of eqLocales) {
      str += `case '${l}':\n`;
      locales.push(`'${l}'`);
    }
    let localesStr = '[' + locales.join(',') + ']';

    str += `  l = locale_${formatLocale(locale)};
    locales = ${localesStr};
    break;\n`;
    return str;
  }

  function formatLocale(locale) { return locale.replace(/-/g, '_'); }
  // clang-format off
  return `${HEADER}
import {registerLocaleData} from '../src/i18n/locale_data';

const u = undefined;

${LOCALES.map(locale => `${existingLocalesData[locale]}`).join('\n')}

let l: any;
let locales: string[] = [];

switch (goog.LOCALE) {
${LOCALES.map(locale => generateCases(locale)).join('')}}

if(l) {
  locales.forEach(locale => registerLocaleData(l, locale));
}
`;
  // clang-format on
}
function generateAllLocalesFile(LOCALES, ALIASES) {
  const existingLocalesAliases = {};
  const existingLocalesData = {};

  // 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(/-/)) {
      eqLocales.add(locale.replace(/-/g, '_'));
    }

    // check for aliases
    const alias = ALIASES[locale];
    if (alias) {
      eqLocales.add(alias);

      if (alias.match(/-/)) {
        eqLocales.add(alias.replace(/-/g, '_'));
      }

      // 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 && !eqLocales.has(aliasValue)) {
          eqLocales.add(aliasValue);

          if (aliasValue.match(/-/)) {
            eqLocales.add(aliasValue.replace(/-/g, '_'));
          }
        }
      }
    }

    for (let l of eqLocales) {
      // find the existing content file
      const path = `${RELATIVE_I18N_DATA_FOLDER}/${l}.ts`;
      if (fs.existsSync(`${RELATIVE_I18N_DATA_FOLDER}/${l}.ts`)) {
        const localeName = formatLocale(locale);
        existingLocalesData[locale] =
            fs.readFileSync(path, 'utf8')
                .replace(`${HEADER}\n`, '')
                .replace('export default ', `export const locale_${localeName} = `)
                .replace('function plural', `function plural_${localeName}`)
                .replace(/,(\n  | )plural/, `, plural_${localeName}`)
                .replace('const u = undefined;\n\n', '');
      }
    }

    existingLocalesAliases[locale] = eqLocales;
  });

  function generateCases(locale) {
    let str = '';
    let locales = [];
    const eqLocales = existingLocalesAliases[locale];
    for (let l of eqLocales) {
      str += `case '${l}':\n`;
      locales.push(`'${l}'`);
    }
    let localesStr = '[' + locales.join(',') + ']';

    str += `  l = locale_${formatLocale(locale)};
    locales = ${localesStr};
    break;\n`;
    return str;
  }

  function formatLocale(locale) { return locale.replace(/-/g, '_'); }
  // clang-format off
  return `${HEADER}
import {registerLocaleData} from '../src/i18n/locale_data';

const u = undefined;

${LOCALES.map(locale => `${existingLocalesData[locale]}`).join('\n')}

let l: any;
let locales: string[] = [];

switch (goog.LOCALE) {
${LOCALES.map(locale => generateCases(locale)).join('')}}

if(l) {
  locales.forEach(locale => registerLocaleData(l, locale));
}
`;
  // clang-format on
}
function generateAllLocalesFile ( LOCALES , ALIASES ) { const existingLocalesAliases = { } ; const existingLocalesData = { } ; // 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 ( / - / ) ) { eqLocales . add ( locale . replace ( / - / g , '_' ) ) ; } // check for aliases const alias = ALIASES [ locale ] ; if ( alias ) { eqLocales . add ( alias ) ; if ( alias . match ( / - / ) ) { eqLocales . add ( alias . replace ( / - / g , '_' ) ) ; } // 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 && ! eqLocales . has ( aliasValue ) ) { eqLocales . add ( aliasValue ) ; if ( aliasValue . match ( / - / ) ) { eqLocales . add ( aliasValue . replace ( / - / g , '_' ) ) ; } } } } for ( let l of eqLocales ) { // find the existing content file const path = ` ${ RELATIVE_I18N_DATA_FOLDER } ${ l } ` ; if ( fs . existsSync ( ` ${ RELATIVE_I18N_DATA_FOLDER } ${ l } ` ) ) { const localeName = formatLocale ( locale ) ; existingLocalesData [ locale ] = fs . readFileSync ( path , 'utf8' ) . replace ( ` ${ HEADER } \n ` , '' ) . replace ( 'export default ' , ` ${ localeName } ` ) . replace ( 'function plural' , ` ${ localeName } ` ) . replace ( / ,(\n  | )plural / , ` ${ localeName } ` ) . replace ( 'const u = undefined;\n\n' , '' ) ; } } existingLocalesAliases [ locale ] = eqLocales ; } ) ; function generateCases ( locale ) { let str = '' ; let locales = [ ] ; const eqLocales = existingLocalesAliases [ locale ] ; for ( let l of eqLocales ) { str += ` ${ l } \n ` ; locales . push ( ` ${ l } ` ) ; } let localesStr = '[' + locales . join ( ',' ) + ']' ; str += ` ${ formatLocale ( locale ) } ${ localesStr } \n ` ; return str ; } function formatLocale ( locale ) { return locale . replace ( / - / g , '_' ) ; } // clang-format off return ` ${ HEADER } ${ LOCALES . map ( locale => ` ${ existingLocalesData [ locale ] } ` ) . join ( '\n' ) } ${ LOCALES . map ( locale => generateCases ( locale ) ) . join ( '' ) } ` ; // clang-format on }
Generate a file that contains all locale to import for closure.
Tree shaking will only keep the data for the `goog.LOCALE` locale.
Generate a file that contains all locale to import for closure . Tree shaking will only keep the data for the goog . LOCALE locale .
Generate all locales file