|
| 1 | +/** |
| 2 | + * Translates src/assets/print-view/fetch-orcid.js for every locale produced by |
| 3 | + * `ng build --localize` and writes the result to dist/<locale>/print-view/fetch-orcid.js. |
| 4 | + * |
| 5 | + * This is required because Angular's `scripts` bundle entries do not run through |
| 6 | + * the @angular/localize inlining pass. Instead, we use Babel + |
| 7 | + * makeEs2015TranslatePlugin to perform the same substitution here. |
| 8 | + * |
| 9 | + * Special cases: |
| 10 | + * - `en` (source locale): $localize calls are replaced with their English source text. |
| 11 | + * - Locales whose XLF filename differs from the folder name (pl→pl_PL, tr→tr_TR, |
| 12 | + * zh-CN→zh_CN, zh-TW→zh_TW) are handled via XLF_LOCALE_MAP. |
| 13 | + */ |
| 14 | + |
| 15 | +import { existsSync, readFileSync, writeFileSync } from 'fs' |
| 16 | +import { |
| 17 | + makeEs2015TranslatePlugin, |
| 18 | + Xliff1TranslationParser, |
| 19 | + Diagnostics, |
| 20 | +} from '@angular/localize/tools' |
| 21 | + |
| 22 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 23 | +const babel = require('@babel/core') |
| 24 | +// eslint-disable-next-line @typescript-eslint/no-var-requires |
| 25 | +const glob = require('glob') |
| 26 | + |
| 27 | +/** Maps Angular locale folder name → XLF filename suffix when they differ. */ |
| 28 | +const XLF_LOCALE_MAP: Record<string, string> = { |
| 29 | + pl: 'pl_PL', |
| 30 | + tr: 'tr_TR', |
| 31 | + 'zh-CN': 'zh_CN', |
| 32 | + 'zh-TW': 'zh_TW', |
| 33 | + src: 'source', |
| 34 | +} |
| 35 | + |
| 36 | +/** Source file (pre-translation). */ |
| 37 | +const PRINT_VIEW_SOURCE = './src/assets/print-view/fetch-orcid.js' |
| 38 | + |
| 39 | +function xlfFileForLocale(locale: string): string { |
| 40 | + const suffix = XLF_LOCALE_MAP[locale] ?? locale |
| 41 | + return `./src/locale/messages.${suffix}.xlf` |
| 42 | +} |
| 43 | + |
| 44 | +function getTranslations(locale: string): Record<string, any> | null { |
| 45 | + const xlf = xlfFileForLocale(locale) |
| 46 | + if (!existsSync(xlf)) { |
| 47 | + return null |
| 48 | + } |
| 49 | + const content = readFileSync(xlf, 'utf8') |
| 50 | + const parser = new Xliff1TranslationParser() |
| 51 | + const analyzed = (parser as any).analyze(xlf, content) as { canParse: boolean; hint: any } |
| 52 | + if (!analyzed.canParse) { |
| 53 | + console.warn(`[print-view-localize] Could not parse XLF for locale "${locale}": ${xlf}`) |
| 54 | + return null |
| 55 | + } |
| 56 | + const { translations } = parser.parse(xlf, content, analyzed.hint) |
| 57 | + return translations |
| 58 | +} |
| 59 | + |
| 60 | +export function localizeAndWritePrintViewScript(): void { |
| 61 | + const source = readFileSync(PRINT_VIEW_SOURCE, 'utf8') |
| 62 | + |
| 63 | + // Process every dist locale folder (index.html is the canonical indicator |
| 64 | + // that the folder is a locale build output). |
| 65 | + const indexFiles: string[] = glob.sync('./dist/*/index.html') |
| 66 | + |
| 67 | + for (const indexFile of indexFiles) { |
| 68 | + const localeFolderSegments = indexFile.split('/') |
| 69 | + // indexFile pattern: ./dist/<locale>/index.html |
| 70 | + const locale = localeFolderSegments[2] |
| 71 | + const localeFolder = localeFolderSegments.slice(0, 3).join('/') |
| 72 | + const destDir = `${localeFolder}/print-view` |
| 73 | + const destFile = `${destDir}/fetch-orcid.js` |
| 74 | + |
| 75 | + let output: string |
| 76 | + |
| 77 | + if (locale === 'share-assets') { |
| 78 | + // Not a locale folder — skip. |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + const translations = getTranslations(locale) |
| 83 | + |
| 84 | + if (!translations) { |
| 85 | + // No XLF for this locale (includes 'en' which is the source locale): |
| 86 | + // inline the source strings via Babel using an empty translation map and |
| 87 | + // missingTranslation:'ignore' so the source text is used as the fallback. |
| 88 | + const diagnostics = new Diagnostics() |
| 89 | + const result = babel.transformSync(source, { |
| 90 | + filename: PRINT_VIEW_SOURCE, |
| 91 | + plugins: [ |
| 92 | + makeEs2015TranslatePlugin(diagnostics, {}, { |
| 93 | + missingTranslation: 'ignore', |
| 94 | + }), |
| 95 | + ], |
| 96 | + configFile: false, |
| 97 | + babelrc: false, |
| 98 | + }) |
| 99 | + output = result?.code ?? source |
| 100 | + } else { |
| 101 | + const diagnostics = new Diagnostics() |
| 102 | + const result = babel.transformSync(source, { |
| 103 | + filename: PRINT_VIEW_SOURCE, |
| 104 | + plugins: [ |
| 105 | + makeEs2015TranslatePlugin(diagnostics, translations, { |
| 106 | + missingTranslation: 'warning', |
| 107 | + }), |
| 108 | + ], |
| 109 | + configFile: false, |
| 110 | + babelrc: false, |
| 111 | + }) |
| 112 | + |
| 113 | + if (diagnostics.hasErrors) { |
| 114 | + console.warn( |
| 115 | + `[print-view-localize] Errors for locale "${locale}":`, |
| 116 | + diagnostics.messages, |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + output = result?.code ?? source |
| 121 | + } |
| 122 | + |
| 123 | + writeFileSync(destFile, output, 'utf8') |
| 124 | + console.log(`[print-view-localize] ✓ ${destFile}`) |
| 125 | + } |
| 126 | +} |
0 commit comments