|
| 1 | +import { isObject } from 'lodash' |
| 2 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { inspect } from 'node:util' |
| 6 | +import { Plugin } from 'vite' |
| 7 | + |
| 8 | +const translationsFile = fileURLToPath( |
| 9 | + new URL('../src/i18n/translations.json', import.meta.url), |
| 10 | +) |
| 11 | +const outputDir = fileURLToPath( |
| 12 | + new URL('../src/i18n/generated', import.meta.url), |
| 13 | +) |
| 14 | + |
| 15 | +export function generateTranslations(): Plugin { |
| 16 | + splitTranslations() |
| 17 | + |
| 18 | + return { |
| 19 | + name: 'generate-translations', |
| 20 | + apply: 'serve', |
| 21 | + configureServer(server) { |
| 22 | + server.watcher.on('change', (filePath) => { |
| 23 | + if (filePath === translationsFile) { |
| 24 | + try { |
| 25 | + splitTranslations() |
| 26 | + } catch (e) { |
| 27 | + console.error('Failed to generate translations:', e) |
| 28 | + } |
| 29 | + } |
| 30 | + }) |
| 31 | + }, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function splitTranslations() { |
| 36 | + if (!existsSync(outputDir)) { |
| 37 | + mkdirSync(outputDir, { recursive: true }) |
| 38 | + } |
| 39 | + |
| 40 | + const translationsJson = readFileSync(translationsFile, 'utf-8') |
| 41 | + const languages = Object.keys( |
| 42 | + JSON.parse(translationsJson).essentials.language, |
| 43 | + ) |
| 44 | + const essentials: Record<string, Record<string, unknown>> = {} |
| 45 | + |
| 46 | + for (const language of languages) { |
| 47 | + const languageTranslations = JSON.parse(translationsJson, (key, value) => { |
| 48 | + if ( |
| 49 | + isObject(value) && |
| 50 | + Object.keys(value).some((key) => languages.includes(key)) |
| 51 | + ) { |
| 52 | + return value[language] || '__NOT_TRANSLATED__' |
| 53 | + } |
| 54 | + return value |
| 55 | + }) |
| 56 | + |
| 57 | + essentials[language] = languageTranslations.essentials |
| 58 | + delete languageTranslations.essentials |
| 59 | + |
| 60 | + writeTsFile(`${language}.ts`, languageTranslations) |
| 61 | + } |
| 62 | + |
| 63 | + writeTsFile(`essentials.ts`, essentials) |
| 64 | + |
| 65 | + console.log(`Translations generated for ${languages.join(', ')}`) |
| 66 | +} |
| 67 | + |
| 68 | +function writeTsFile(filename: string, jsonObject: Record<string, unknown>) { |
| 69 | + const filePath = join(outputDir, filename) |
| 70 | + |
| 71 | + const literalObject = inspect(jsonObject, { |
| 72 | + depth: 100, |
| 73 | + maxStringLength: Infinity, |
| 74 | + breakLength: Infinity, |
| 75 | + sorted: false, |
| 76 | + compact: false, |
| 77 | + }) |
| 78 | + |
| 79 | + const content = `// This file is auto-generated by generate-translations.ts, do not edit it directly. |
| 80 | +export default ${literalObject} as const` |
| 81 | + |
| 82 | + if (existsSync(filePath) && readFileSync(filePath, 'utf-8') === content) { |
| 83 | + // skip writing to avoid unnecessary hot reloads |
| 84 | + return |
| 85 | + } |
| 86 | + writeFileSync(filePath, content, 'utf-8') |
| 87 | +} |
0 commit comments