Skip to content

Commit 2df634c

Browse files
committed
Add script to find missing translations
1 parent be191ab commit 2df634c

File tree

3 files changed

+222
-2
lines changed

3 files changed

+222
-2
lines changed

.roomodes

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
"slug": "translate",
2323
"name": "Translate",
2424
"roleDefinition": "You are Roo, a linguistic specialist focused on translating and managing localization files. Your responsibility is to help maintain and update translation files for the application, ensuring consistency and accuracy across all language resources.",
25-
"customInstructions": "When internationalizing and translating content:\n\n# Translation Style and Tone\n- Maintain a direct and concise style that mirrors the tone of the original text\n- Carefully account for colloquialisms and idiomatic expressions in both source and target languages\n- Aim for culturally relevant and meaningful translations rather than literal translations\n- Adapt the formality level to match the original content (whether formal or informal)\n- Preserve the personality and voice of the original content\n- Use natural-sounding language that feels native to speakers of the target language\n- Don't translate the word \"token\" as it means something specific in English that all languages will understand\n\n# Technical Implementation\n- Use namespaces to organize translations logically\n- Handle pluralization using i18next's built-in capabilities\n- Implement proper interpolation for variables using {{variable}} syntax\n- Don't include defaultValue. The `en` translations are the fallback.\n\n# Quality Assurance\n- Maintain consistent terminology across all translations\n- Respect the JSON structure of translation files\n- Watch for placeholders and preserve them in translations\n- Be mindful of text length in UI elements when translating to languages that might require more characters\n- Use context-aware translations when the same string has different meanings\n\n# Supported Languages\n- Localize all strings into the following locale files: ar, ca, cs, de, en, es, fr, hi, hu, it, ja, ko, pl, pt, pt-BR, ru, tr, zh-CN, zh-TW",
25+
"customInstructions": "When internationalizing and translating content:\n\n# Translation Style and Tone\n- Maintain a direct and concise style that mirrors the tone of the original text\n- Carefully account for colloquialisms and idiomatic expressions in both source and target languages\n- Aim for culturally relevant and meaningful translations rather than literal translations\n- Adapt the formality level to match the original content (whether formal or informal)\n- Preserve the personality and voice of the original content\n- Use natural-sounding language that feels native to speakers of the target language\n- Don't translate the word \"token\" as it means something specific in English that all languages will understand\n\n# Technical Implementation\n- Use namespaces to organize translations logically\n- Handle pluralization using i18next's built-in capabilities\n- Implement proper interpolation for variables using {{variable}} syntax\n- Don't include defaultValue. The `en` translations are the fallback.\n- Always use apply_diff instead of write_to_file when editing existing translation files as it's much faster and more reliable\n- When using apply_diff, make sure to carefully identify the exact JSON structure to edit to avoid syntax errors\n\n# Quality Assurance\n- Maintain consistent terminology across all translations\n- Respect the JSON structure of translation files\n- Watch for placeholders and preserve them in translations\n- Be mindful of text length in UI elements when translating to languages that might require more characters\n- Use context-aware translations when the same string has different meanings\n- Always validate your translation work by running the missing translations script:\n ```\n node scripts/find-missing-translations.js\n ```\n- Before completing any translation task, ensure there are no missing translations by running the script with the target locale(s):\n ```\n node scripts/find-missing-translations.js --locale=<locale-code>\n ```\n- Address any missing translations identified by the script to ensure complete coverage across all locales\n\n# Supported Languages\n- Localize all strings into the following locale files: ar, ca, cs, de, en, es, fr, hi, hu, it, ja, ko, pl, pt, pt-BR, ru, tr, zh-CN, zh-TW",
2626
"groups": [
2727
"read",
28+
"command",
2829
[
2930
"edit",
3031
{

knip.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"e2e/**",
1717
"src/activate/**",
1818
"src/exports/**",
19-
"src/extension.ts"
19+
"src/extension.ts",
20+
"scripts/**"
2021
],
2122
"workspaces": {
2223
"webview-ui": {
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/**
2+
* Script to find missing translations in locale files
3+
*
4+
* Usage:
5+
* node scripts/find-missing-translations.js [options]
6+
*
7+
* Options:
8+
* --locale=<locale> Only check a specific locale (e.g. --locale=fr)
9+
* --file=<file> Only check a specific file (e.g. --file=chat.json)
10+
* --help Show this help message
11+
*/
12+
13+
const fs = require("fs")
14+
const path = require("path")
15+
16+
// Process command line arguments
17+
const args = process.argv.slice(2).reduce((acc, arg) => {
18+
if (arg === "--help") {
19+
acc.help = true
20+
} else if (arg.startsWith("--locale=")) {
21+
acc.locale = arg.split("=")[1]
22+
} else if (arg.startsWith("--file=")) {
23+
acc.file = arg.split("=")[1]
24+
}
25+
return acc
26+
}, {})
27+
28+
// Show help if requested
29+
if (args.help) {
30+
console.log(`
31+
Find Missing Translations
32+
33+
A utility script to identify missing translations across locale files.
34+
Compares non-English locale files to the English ones to find any missing keys.
35+
36+
Usage:
37+
node scripts/find-missing-translations.js [options]
38+
39+
Options:
40+
--locale=<locale> Only check a specific locale (e.g. --locale=fr)
41+
--file=<file> Only check a specific file (e.g. --file=chat.json)
42+
--help Show this help message
43+
44+
Output:
45+
- Generates a report of missing translations
46+
`)
47+
process.exit(0)
48+
}
49+
50+
// Path to the locales directory
51+
const LOCALES_DIR = path.join(__dirname, "../webview-ui/src/i18n/locales")
52+
53+
// Recursively find all keys in an object
54+
function findKeys(obj, parentKey = "") {
55+
let keys = []
56+
57+
for (const [key, value] of Object.entries(obj)) {
58+
const currentKey = parentKey ? `${parentKey}.${key}` : key
59+
60+
if (typeof value === "object" && value !== null) {
61+
// If value is an object, recurse
62+
keys = [...keys, ...findKeys(value, currentKey)]
63+
} else {
64+
// If value is a primitive, add the key
65+
keys.push(currentKey)
66+
}
67+
}
68+
69+
return keys
70+
}
71+
72+
// Get value at a dotted path in an object
73+
function getValueAtPath(obj, path) {
74+
const parts = path.split(".")
75+
let current = obj
76+
77+
for (const part of parts) {
78+
if (current === undefined || current === null) {
79+
return undefined
80+
}
81+
current = current[part]
82+
}
83+
84+
return current
85+
}
86+
87+
// Main function to find missing translations
88+
function findMissingTranslations() {
89+
try {
90+
// Get all locale directories (or filter to the specified locale)
91+
const allLocales = fs.readdirSync(LOCALES_DIR).filter((item) => {
92+
const stats = fs.statSync(path.join(LOCALES_DIR, item))
93+
return stats.isDirectory() && item !== "en" // Exclude English as it's our source
94+
})
95+
96+
// Filter to the specified locale if provided
97+
const locales = args.locale ? allLocales.filter((locale) => locale === args.locale) : allLocales
98+
99+
if (args.locale && locales.length === 0) {
100+
console.error(`Error: Locale '${args.locale}' not found in ${LOCALES_DIR}`)
101+
process.exit(1)
102+
}
103+
104+
console.log(`Checking ${locales.length} non-English locale(s): ${locales.join(", ")}`)
105+
106+
// Get all English JSON files
107+
const englishDir = path.join(LOCALES_DIR, "en")
108+
let englishFiles = fs.readdirSync(englishDir).filter((file) => file.endsWith(".json") && !file.startsWith("."))
109+
110+
// Filter to the specified file if provided
111+
if (args.file) {
112+
if (!englishFiles.includes(args.file)) {
113+
console.error(`Error: File '${args.file}' not found in ${englishDir}`)
114+
process.exit(1)
115+
}
116+
englishFiles = englishFiles.filter((file) => file === args.file)
117+
}
118+
119+
// Load file contents
120+
const englishFileContents = englishFiles.map((file) => ({
121+
name: file,
122+
content: JSON.parse(fs.readFileSync(path.join(englishDir, file), "utf8")),
123+
}))
124+
125+
console.log(
126+
`Checking ${englishFileContents.length} translation file(s): ${englishFileContents.map((f) => f.name).join(", ")}`,
127+
)
128+
129+
// Results object to store missing translations
130+
const missingTranslations = {}
131+
132+
// For each locale, check for missing translations
133+
for (const locale of locales) {
134+
missingTranslations[locale] = {}
135+
136+
for (const { name, content: englishContent } of englishFileContents) {
137+
const localeFilePath = path.join(LOCALES_DIR, locale, name)
138+
139+
// Check if the file exists in the locale
140+
if (!fs.existsSync(localeFilePath)) {
141+
missingTranslations[locale][name] = { file: "File is missing entirely" }
142+
continue
143+
}
144+
145+
// Load the locale file
146+
const localeContent = JSON.parse(fs.readFileSync(localeFilePath, "utf8"))
147+
148+
// Find all keys in the English file
149+
const englishKeys = findKeys(englishContent)
150+
151+
// Check for missing keys in the locale file
152+
const missingKeys = []
153+
154+
for (const key of englishKeys) {
155+
const englishValue = getValueAtPath(englishContent, key)
156+
const localeValue = getValueAtPath(localeContent, key)
157+
158+
if (localeValue === undefined) {
159+
missingKeys.push({
160+
key,
161+
englishValue,
162+
})
163+
}
164+
}
165+
166+
if (missingKeys.length > 0) {
167+
missingTranslations[locale][name] = missingKeys
168+
}
169+
}
170+
}
171+
172+
// Output results
173+
let hasMissingTranslations = false
174+
175+
console.log("\nMissing Translations Report:\n")
176+
177+
for (const [locale, files] of Object.entries(missingTranslations)) {
178+
if (Object.keys(files).length === 0) {
179+
console.log(`✅ ${locale}: No missing translations`)
180+
continue
181+
}
182+
183+
hasMissingTranslations = true
184+
console.log(`📝 ${locale}:`)
185+
186+
for (const [fileName, missingItems] of Object.entries(files)) {
187+
if (missingItems.file) {
188+
console.log(` - ${fileName}: ${missingItems.file}`)
189+
continue
190+
}
191+
192+
console.log(` - ${fileName}: ${missingItems.length} missing translations`)
193+
194+
for (const { key, englishValue } of missingItems) {
195+
console.log(` ${key}: "${englishValue}"`)
196+
}
197+
}
198+
199+
console.log("")
200+
}
201+
202+
if (!hasMissingTranslations) {
203+
console.log("\n✅ All translations are complete!")
204+
} else {
205+
console.log("✏️ To add missing translations:")
206+
console.log("1. Add the missing keys to the corresponding locale files")
207+
console.log("2. Translate the English values to the appropriate language")
208+
console.log("3. Run this script again to verify all translations are complete")
209+
}
210+
} catch (error) {
211+
console.error("Error:", error.message)
212+
console.error(error.stack)
213+
process.exit(1)
214+
}
215+
}
216+
217+
// Run the main function
218+
findMissingTranslations()

0 commit comments

Comments
 (0)