|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import process from 'node:process' |
| 3 | +import { existsSync, readFileSync, writeFileSync } from 'node:fs' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | + |
| 7 | +const LOCALES_DIRECTORY = fileURLToPath(new URL('../i18n/locales', import.meta.url)) |
| 8 | +const REFERENCE_FILE_NAME = 'en.json' |
| 9 | + |
| 10 | +const COLORS = { |
| 11 | + reset: '\x1b[0m', |
| 12 | + red: '\x1b[31m', |
| 13 | + green: '\x1b[32m', |
| 14 | + yellow: '\x1b[33m', |
| 15 | + cyan: '\x1b[36m', |
| 16 | +} as const |
| 17 | + |
| 18 | +type NestedObject = { [key: string]: unknown } |
| 19 | + |
| 20 | +const loadJson = (filePath: string): NestedObject => { |
| 21 | + if (!existsSync(filePath)) { |
| 22 | + console.error(`${COLORS.red}Error: File not found at ${filePath}${COLORS.reset}`) |
| 23 | + process.exit(1) |
| 24 | + } |
| 25 | + return JSON.parse(readFileSync(filePath, 'utf-8')) as NestedObject |
| 26 | +} |
| 27 | + |
| 28 | +const isNested = (val: unknown): val is NestedObject => |
| 29 | + val !== null && typeof val === 'object' && !Array.isArray(val) |
| 30 | + |
| 31 | +const createTranslationStructure = (reference: NestedObject, prefix = ''): NestedObject => { |
| 32 | + const result: NestedObject = {} |
| 33 | + |
| 34 | + for (const key of Object.keys(reference)) { |
| 35 | + const refValue = reference[key] |
| 36 | + |
| 37 | + if (isNested(refValue)) { |
| 38 | + result[key] = createTranslationStructure(refValue, prefix ? `${prefix}.${key}` : key) |
| 39 | + } else { |
| 40 | + result[key] = `EN TEXT TO REPLACE: ${refValue}` |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return result |
| 45 | +} |
| 46 | + |
| 47 | +const run = (): void => { |
| 48 | + const args = process.argv.slice(2) |
| 49 | + |
| 50 | + if (args.length === 0) { |
| 51 | + console.log(`${COLORS.yellow}Usage: pnpm create-translation <locale-code>${COLORS.reset}`) |
| 52 | + console.log(`Example: pnpm create-translation ko-KR`) |
| 53 | + console.log(`\nAvailable locale codes format: xx-XX (e.g., fr-FR, zh-CN)`) |
| 54 | + process.exit(1) |
| 55 | + } |
| 56 | + |
| 57 | + const localeCode = args[0] |
| 58 | + |
| 59 | + if (!localeCode) { |
| 60 | + console.error(`${COLORS.red}Error: Locale code is required${COLORS.reset}`) |
| 61 | + process.exit(1) |
| 62 | + } |
| 63 | + |
| 64 | + if (!/^[a-z]{2}(-[A-Z]{2})?$/.test(localeCode)) { |
| 65 | + console.error( |
| 66 | + `${COLORS.red}Error: Invalid locale code format. Expected format: xx or xx-XX (e.g., fr or fr-FR)${COLORS.reset}`, |
| 67 | + ) |
| 68 | + process.exit(1) |
| 69 | + } |
| 70 | + |
| 71 | + const targetFileName = `${localeCode}.json` |
| 72 | + const targetFilePath = join(LOCALES_DIRECTORY, targetFileName) |
| 73 | + |
| 74 | + if (existsSync(targetFilePath)) { |
| 75 | + console.error( |
| 76 | + `${COLORS.red}Error: Translation file already exists: ${targetFileName}${COLORS.reset}`, |
| 77 | + ) |
| 78 | + console.log(`Use compare-translations.ts to update existing translations.`) |
| 79 | + process.exit(1) |
| 80 | + } |
| 81 | + |
| 82 | + const referenceFilePath = join(LOCALES_DIRECTORY, REFERENCE_FILE_NAME) |
| 83 | + const referenceContent = loadJson(referenceFilePath) |
| 84 | + |
| 85 | + console.log(`${COLORS.cyan}=== Creating new translation file ===${COLORS.reset}`) |
| 86 | + console.log(`Reference: ${REFERENCE_FILE_NAME}`) |
| 87 | + console.log(`Target: ${targetFileName}`) |
| 88 | + |
| 89 | + const newTranslation = createTranslationStructure(referenceContent) |
| 90 | + |
| 91 | + writeFileSync(targetFilePath, JSON.stringify(newTranslation, null, 2) + '\n', 'utf-8') |
| 92 | + |
| 93 | + console.log(`\n${COLORS.green}✓ Successfully created ${targetFileName}${COLORS.reset}`) |
| 94 | + console.log(`Location: ${targetFilePath}`) |
| 95 | + console.log(`\n${COLORS.yellow}Next steps:${COLORS.reset}`) |
| 96 | + console.log(`1. Translate the English placeholder text to ${localeCode}`) |
| 97 | + console.log(`2. Run: pnpm i18n:check ${localeCode} to verify the translation`) |
| 98 | + console.log(`3. Run: pnpm i18n:check:fix ${localeCode} to sync with latest changes`) |
| 99 | +} |
| 100 | + |
| 101 | +run() |
0 commit comments