|
| 1 | +/** |
| 2 | + * @file This script builds our translation files for Hugo and JS because Hugo cannot do that itself. It replaces our |
| 3 | + * old custom Webpack i18n loader. |
| 4 | + * |
| 5 | + * It supports building for production (without any flags) or watch mode for development (with `--watch`). It is run by |
| 6 | + * `yarn dev` and `yarn build`. |
| 7 | + */ |
| 8 | +import { readFileSync, writeFileSync, mkdirSync } from 'fs'; |
| 9 | +import { join, basename } from 'path'; |
| 10 | +import glob from 'glob'; |
| 11 | +import { getDirname } from 'cross-dirname'; |
| 12 | +import deepmerge from 'deepmerge'; |
| 13 | +import { watch as chokidar } from 'chokidar'; |
| 14 | + |
| 15 | +type TranslationScope = string; |
| 16 | +type TranslationKey = string; |
| 17 | +type TranslationFile = Record<TranslationScope, Record<TranslationKey, string>>; |
| 18 | + |
| 19 | +const watch = process.argv.includes('--watch'); |
| 20 | +const quiet = process.argv.includes('--quiet'); |
| 21 | + |
| 22 | +const dirname = getDirname(); |
| 23 | + |
| 24 | +const inputDir = join(dirname, '..', 'src', 'i18n'); |
| 25 | +const hugoOutputDir = join(dirname, '..', 'i18n'); |
| 26 | +const jsOutputDir = join(dirname, '..', 'static', 'js'); |
| 27 | + |
| 28 | +const allTranslations = Object.fromEntries( |
| 29 | + glob |
| 30 | + .sync('*.json', { cwd: inputDir, absolute: true }) |
| 31 | + .map((p) => [basename(p, '.json'), readFileSync(p, 'utf8')] as const) |
| 32 | + .map((r) => [r[0], JSON.parse(r[1]) as TranslationFile] as const) |
| 33 | +); |
| 34 | + |
| 35 | +// eslint-disable-next-line no-console |
| 36 | +const log = (...messages: unknown[]) => !quiet && console.log('[i18n]', ...messages, `(${new Date().toISOString()})`); |
| 37 | + |
| 38 | +/** |
| 39 | + * Emit the Hugo and JS translations for the given input translations. |
| 40 | + */ |
| 41 | +const emitTranslations = (translations: Record<string, TranslationFile>) => { |
| 42 | + const en = allTranslations['en']; |
| 43 | + |
| 44 | + // Ensure output directories exist. |
| 45 | + mkdirSync(hugoOutputDir, { recursive: true }); |
| 46 | + mkdirSync(jsOutputDir, { recursive: true }); |
| 47 | + |
| 48 | + // eslint-disable-next-line prefer-const |
| 49 | + for (let [language, data] of Object.entries(translations)) { |
| 50 | + // Since we support languages where we cannot guarantee that all strings are always translated, we need to |
| 51 | + // fallback to English for untranslated strings. |
| 52 | + data = deepmerge(en, data); |
| 53 | + |
| 54 | + // Macros allow us set set common strings (like the site name) once in the translations, and then use that value |
| 55 | + // in many places. Macros are used by inserting `${macro_name}` somewhere in a translation, where `macro_name` |
| 56 | + // is the key of the translation under the `macros` context that sets the macro's value. |
| 57 | + let json = JSON.stringify(data); |
| 58 | + for (const [name, value] of Object.entries(data.macros)) { |
| 59 | + json = json.replace(new RegExp(`\\\${${name}}`, 'g'), value); |
| 60 | + } |
| 61 | + data = JSON.parse(json); |
| 62 | + |
| 63 | + // Emit the translation files for Hugo. |
| 64 | + if (data.hugo) { |
| 65 | + const hugoData = data.hugo; |
| 66 | + |
| 67 | + // To avoid translating the same strings multiple times, we can import other translations into Hugo here. |
| 68 | + const importKeys = { |
| 69 | + generator: [ |
| 70 | + 'access-request-statement', |
| 71 | + 'erasure-request-statement', |
| 72 | + 'rectification-request-statement', |
| 73 | + 'objection-request-statement', |
| 74 | + ], |
| 75 | + } as const; |
| 76 | + |
| 77 | + for (const scope of Object.keys(importKeys) as (keyof typeof importKeys)[]) { |
| 78 | + const keys = importKeys[scope].filter((key) => data[scope][key] !== undefined); |
| 79 | + |
| 80 | + for (const key of keys) hugoData[`imported--${scope}-${key}`] = data[scope][key]; |
| 81 | + } |
| 82 | + |
| 83 | + writeFileSync(join(hugoOutputDir, `${language}.json`), JSON.stringify(hugoData, null, 4)); |
| 84 | + } |
| 85 | + |
| 86 | + // The JS translation files don't need to include the translations only used by Hugo (#620). |
| 87 | + delete data.hugo; |
| 88 | + |
| 89 | + // Emit the translation files to be included in the HTML. |
| 90 | + writeFileSync( |
| 91 | + join(jsOutputDir, `translations-${language}.gen.js`), |
| 92 | + `window.I18N_DEFINITION = ${JSON.stringify(data)}` |
| 93 | + ); |
| 94 | + log('Emitted translations for:', language); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +/** |
| 99 | + * Emit the special requests translations file to be included in the HTML. |
| 100 | + */ |
| 101 | +const emitRequestsTranslations = () => { |
| 102 | + const requestsTranslations = Object.entries(allTranslations).reduce( |
| 103 | + (acc, [language, translations]) => ({ ...acc, [language]: translations.requests || {} }), |
| 104 | + {} |
| 105 | + ); |
| 106 | + |
| 107 | + writeFileSync( |
| 108 | + join(jsOutputDir, 'translations-requests.gen.js'), |
| 109 | + `window.I18N_DEFINITION_REQUESTS = ${JSON.stringify(requestsTranslations)}` |
| 110 | + ); |
| 111 | + log('Emitted requests translations.'); |
| 112 | +}; |
| 113 | + |
| 114 | +if (watch) { |
| 115 | + log('Watching translation files for changes …'); |
| 116 | + |
| 117 | + const watcher = chokidar(inputDir, { |
| 118 | + ignored: (path, stats) => !!stats?.isFile() && !path.endsWith('.json'), |
| 119 | + ignoreInitial: false, |
| 120 | + depth: 0, |
| 121 | + }); |
| 122 | + |
| 123 | + const handler = (path: string) => { |
| 124 | + try { |
| 125 | + const translations = { |
| 126 | + [basename(path, '.json')]: JSON.parse(readFileSync(path, 'utf8')) as TranslationFile, |
| 127 | + }; |
| 128 | + emitTranslations(translations); |
| 129 | + emitRequestsTranslations(); |
| 130 | + } catch (err) { |
| 131 | + log('Error while rebuilding translations:', err); |
| 132 | + } |
| 133 | + }; |
| 134 | + watcher.on('add', handler); |
| 135 | + watcher.on('change', handler); |
| 136 | + |
| 137 | + process.on('SIGINT', () => { |
| 138 | + watcher.close(); |
| 139 | + log('Stopped watching translation files.'); |
| 140 | + process.exit(); |
| 141 | + }); |
| 142 | +} else { |
| 143 | + emitTranslations(allTranslations); |
| 144 | + emitRequestsTranslations(); |
| 145 | +} |
0 commit comments