diff --git a/package.json b/package.json index 9398b6a5..cdb48429 100644 --- a/package.json +++ b/package.json @@ -397,7 +397,10 @@ "@vue/runtime-dom": "^3.3.4", "@vue/test-utils": "^2.3.2", "@vue/tsconfig": "^0.4.0", + "axios": "^1.12.2", + "commander": "^14.0.1", "consola": "^3.0.2", + "dotenv": "^17.2.3", "eslint": "^8.47.0", "hygen": "^6.2.11", "jsdom": "^22.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d92264d8..00c64b69 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1077,9 +1077,18 @@ importers: '@vue/tsconfig': specifier: ^0.4.0 version: 0.4.0 + axios: + specifier: ^1.12.2 + version: 1.12.2 + commander: + specifier: ^14.0.1 + version: 14.0.1 consola: specifier: ^3.0.2 version: 3.4.0 + dotenv: + specifier: ^17.2.3 + version: 17.2.3 eslint: specifier: ^8.47.0 version: 8.47.0 @@ -4970,6 +4979,9 @@ packages: resolution: {integrity: sha512-wvUjBtSGN7+7SjNpq/9M2Tg350UZD3q62IFZLbRAR1bSMlCo1ZaeW+BJ+D090e4hIIZLBcTDWe4Mh4jvUDajzQ==} engines: {node: '>= 0.4'} + axios@1.12.2: + resolution: {integrity: sha512-vMJzPewAlRyOgxV2dU0Cuz2O8zzzx9VYtbJOaBgXFeLc4IV/Eg50n4LowmehOOR61S8ZMpc2K5Sa7g6A4jfkUw==} + axobject-query@4.1.0: resolution: {integrity: sha512-qIj0G9wZbMGNLjLmg1PT6v2mE9AH2zlnADJD/2tC6E00hgmhUOfEB6greHPAfLRSufHqROIUTkw6E+M3lH0PTQ==} engines: {node: '>= 0.4'} @@ -5419,6 +5431,10 @@ packages: resolution: {integrity: sha512-/rFeCpNJQbhSZjGVwO9RFV3xPqbnERS8MmIQzCtD/zl6gpJuV/bMLuN92oG3F7d8oDEHHRrujSXNUr8fpjntKw==} engines: {node: '>=18'} + commander@14.0.1: + resolution: {integrity: sha512-2JkV3gUZUVrbNA+1sjBOYLsMZ5cEEl8GTFP2a4AVz5hvasAMCQ1D2l2le/cX+pV4N6ZU17zjUahLpIXRrnWL8A==} + engines: {node: '>=20'} + commander@2.20.3: resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==} @@ -6145,6 +6161,10 @@ packages: resolution: {integrity: sha512-xHF8EP4XH/Ba9fvAF2LDd5O3IITVolerVV6xvkxoM8zlGEiCUrggpAnHyOoKJKCrhvPcGATFAUwIujj7bRG5UA==} hasBin: true + dotenv@17.2.3: + resolution: {integrity: sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==} + engines: {node: '>=12'} + dotignore@0.1.2: resolution: {integrity: sha512-UGGGWfSauusaVJC+8fgV+NVvBXkCTmVv7sk6nojDZZvuOUNGUy0Zk4UpHQD6EDjS0jpBwcACvH4eofvyzBcRDw==} hasBin: true @@ -6282,10 +6302,6 @@ packages: resolution: {integrity: sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==} engines: {node: '>= 0.4'} - es-set-tostringtag@2.0.2: - resolution: {integrity: sha512-BuDyupZt65P9D2D2vA/zqcI3G5xRsklm5N3xCwuiy+/vKy8i0ifdsQP1sLgO4tZDSCaQUSnmC48khknGMV3D2Q==} - engines: {node: '>= 0.4'} - es-set-tostringtag@2.1.0: resolution: {integrity: sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==} engines: {node: '>= 0.4'} @@ -6776,6 +6792,10 @@ packages: resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==} engines: {node: '>= 6'} + form-data@4.0.4: + resolution: {integrity: sha512-KrGhL9Q4zjj0kiUt5OO4Mr/A/jlI2jDYs5eHBpYHPcBEVSiipAvn2Ko2HnPe20rmcuuvMHNdZFp+4IlGTMF0Ow==} + engines: {node: '>= 6'} + formdata-polyfill@4.0.10: resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==} engines: {node: '>=12.20.0'} @@ -6887,10 +6907,6 @@ packages: get-func-name@2.0.2: resolution: {integrity: sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==} - get-intrinsic@1.2.7: - resolution: {integrity: sha512-VW6Pxhsrk0KAOqs3WEd0klDiF/+V7gQOpAvY1jVU/LHmaD/kQO4523aiJuikX/QAKYiW6x8Jh+RJej1almdtCA==} - engines: {node: '>= 0.4'} - get-intrinsic@1.3.0: resolution: {integrity: sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==} engines: {node: '>= 0.4'} @@ -7023,9 +7039,6 @@ packages: resolution: {integrity: sha512-eWv1ds/zAlz+M1ioHsyKJomfY7jbDDPpwSkv14KQj89bycx1nvK5/2Cj/T9g7kzJcX5Bc7Yv22FjfBZS/jl94A==} engines: {node: '>= 10.13.0'} - gopd@1.0.1: - resolution: {integrity: sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==} - gopd@1.2.0: resolution: {integrity: sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==} engines: {node: '>= 0.4'} @@ -9292,6 +9305,9 @@ packages: protocol-buffers-schema@2.2.0: resolution: {integrity: sha512-RgyxCE5wHb22ALgI7pq56FFCccLJlluxVXHvoYelIb/dkyJ/s8b8cdR6eCEwWtRcBswpPuM5b/MpqamCBeMPsQ==} + proxy-from-env@1.1.0: + resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} + prr@1.0.1: resolution: {integrity: sha512-yPw4Sng1gWghHQWj0B3ZggWUm4qVbPwPFcRG8KyxiU7J2OHFSoEHKS+EZ3fv5l1t9CyCiop6l/ZYeWbrgoQejw==} @@ -16080,7 +16096,7 @@ snapshots: define-properties: 1.2.1 es-abstract: 1.23.9 es-object-atoms: 1.1.1 - get-intrinsic: 1.2.7 + get-intrinsic: 1.3.0 is-string: 1.0.7 array-slice@1.1.0: {} @@ -16187,6 +16203,14 @@ snapshots: dependencies: possible-typed-array-names: 1.0.0 + axios@1.12.2: + dependencies: + follow-redirects: 1.15.9 + form-data: 4.0.4 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + axobject-query@4.1.0: {} b4a@1.6.7: {} @@ -16690,6 +16714,8 @@ snapshots: commander@13.1.0: {} + commander@14.0.1: {} + commander@2.20.3: {} commander@6.2.1: {} @@ -17459,6 +17485,8 @@ snapshots: commander: 6.2.1 glob: 7.2.3 + dotenv@17.2.3: {} + dotignore@0.1.2: dependencies: minimatch: 3.1.2 @@ -17584,7 +17612,7 @@ snapshots: arraybuffer.prototype.slice: 1.0.2 available-typed-arrays: 1.0.7 call-bind: 1.0.8 - es-set-tostringtag: 2.0.2 + es-set-tostringtag: 2.1.0 es-to-primitive: 1.2.1 function.prototype.name: 1.1.6 get-intrinsic: 1.3.0 @@ -17686,12 +17714,6 @@ snapshots: dependencies: es-errors: 1.3.0 - es-set-tostringtag@2.0.2: - dependencies: - get-intrinsic: 1.3.0 - has-tostringtag: 1.0.2 - hasown: 2.0.2 - es-set-tostringtag@2.1.0: dependencies: es-errors: 1.3.0 @@ -18303,6 +18325,14 @@ snapshots: combined-stream: 1.0.8 mime-types: 2.1.35 + form-data@4.0.4: + dependencies: + asynckit: 0.4.0 + combined-stream: 1.0.8 + es-set-tostringtag: 2.1.0 + hasown: 2.0.2 + mime-types: 2.1.35 + formdata-polyfill@4.0.10: dependencies: fetch-blob: 3.2.0 @@ -18407,19 +18437,6 @@ snapshots: get-func-name@2.0.2: {} - get-intrinsic@1.2.7: - dependencies: - call-bind-apply-helpers: 1.0.2 - es-define-property: 1.0.1 - es-errors: 1.3.0 - es-object-atoms: 1.1.1 - function-bind: 1.1.2 - get-proto: 1.0.1 - gopd: 1.2.0 - has-symbols: 1.1.0 - hasown: 2.0.2 - math-intrinsics: 1.1.0 - get-intrinsic@1.3.0: dependencies: call-bind-apply-helpers: 1.0.2 @@ -18596,10 +18613,6 @@ snapshots: dependencies: sparkles: 2.1.0 - gopd@1.0.1: - dependencies: - get-intrinsic: 1.3.0 - gopd@1.2.0: {} graceful-fs@4.2.11: {} @@ -20375,7 +20388,7 @@ snapshots: define-properties: 1.2.1 es-abstract: 1.23.9 es-object-atoms: 1.1.1 - gopd: 1.0.1 + gopd: 1.2.0 safe-array-concat: 1.1.3 object.pick@1.3.0: @@ -21022,6 +21035,8 @@ snapshots: protocol-buffers-schema@2.2.0: {} + proxy-from-env@1.1.0: {} + prr@1.0.1: optional: true diff --git a/scripts/translate-missings.mjs b/scripts/translate-missings.mjs new file mode 100644 index 00000000..370f5f26 --- /dev/null +++ b/scripts/translate-missings.mjs @@ -0,0 +1,116 @@ +import { promises as fs } from "fs"; +import path from 'node:path'; +import yaml from 'yaml'; +import dotenv from 'dotenv'; +import axios from 'axios'; +import { Command } from 'commander'; + +dotenv.config(); + +const program = new Command(); +const GOOGLE_TRANSLATE_API_KEY = process.env.GOOGLE_TRANSLATE_API_KEY; +const TRANSLATE_ENDPOINT = 'https://translation.googleapis.com/language/translate/v2'; + +if (!GOOGLE_TRANSLATE_API_KEY) { + console.error('❌ Missing GOOGLE_TRANSLATE_API_KEY environment variable'); + process.exit(1); +} + +program + .name('translate-yaml') + .description('Sync and translate YAML files using Google Translate') + .requiredOption('-e, --english ', 'English YAML file') + .requiredOption('-t, --targets ', 'Target language YAML files') + .option('--dry-run', 'Count characters to translate without sending API requests') + .parse(process.argv); + +const options = program.opts(); + +async function translateText(text, targetLang) { + const response = await axios.post(TRANSLATE_ENDPOINT, null, { + params: { + q: text, + target: targetLang, + key: GOOGLE_TRANSLATE_API_KEY, + }, + }); + return response.data.data.translations[0].translatedText; +} + +function flattenYaml(obj, prefix = '') { + const result = {}; + for (const [key, value] of Object.entries(obj)) { + const fullKey = prefix ? `${prefix}.${key}` : key; + if (typeof value === 'object' && value !== null) { + Object.assign(result, flattenYaml(value, fullKey)); + } else { + result[fullKey] = String(value); + } + } + return result; +} + +function unflattenYaml(flat) { + const result = {}; + for (const [key, value] of Object.entries(flat)) { + const keys = key.split('.'); + let current = result; + keys.forEach((k, i) => { + if (i === keys.length - 1) { + current[k] = value; + } else { + current[k] = current[k] || {}; + current = current[k]; + } + }); + } + return result; +} + +async function syncTranslations(englishFile, targetFiles, dryRun = false) { + const englishYaml = yaml.parse(await fs.readFile(englishFile, 'utf8')); + const englishFlat = flattenYaml(englishYaml); + + let totalChars = 0; + + for (const file of targetFiles) { + const langCode = path.basename(file, '.yml'); + if (langCode === 'en') { + continue; + } + const targetYaml = yaml.parse(await fs.readFile(file, 'utf8')) || {}; + const targetFlat = flattenYaml(targetYaml); + + const updatedFlat = { ...targetFlat }; + let langCharCount = 0; + + for (const [key, englishText] of Object.entries(englishFlat)) { + const needsTranslation = !targetFlat[key] || targetFlat[key] === englishText; + if (needsTranslation) { + if (dryRun) { + langCharCount += englishText.length; + } else { + const translated = await translateText(englishText, langCode); + updatedFlat[key] = translated; + console.log(`Translated [${key}] to ${langCode}: ${translated}`); + } + } + } + + if (dryRun) { + console.log(`Dry run for ${langCode}: ${langCharCount} characters to translate.`); + totalChars += langCharCount; + } else { + const updatedYaml = unflattenYaml(updatedFlat); + await fs.writeFile(file, yaml.stringify(updatedYaml), 'utf8'); + } + } + + if (dryRun) { + console.log(`Total characters across all languages: ${totalChars}`); + } +} + +syncTranslations(options.english, options.targets, options.dryRun) + .then(() => console.log(options.dryRun ? 'Dry run complete.' : 'Translation sync complete.')) + .catch(console.error);