-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathfind-missing-i18n-key.js
More file actions
204 lines (171 loc) · 5.52 KB
/
Copy pathfind-missing-i18n-key.js
File metadata and controls
204 lines (171 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const fs = require("fs")
const path = require("path")
// Parse command-line arguments
const args = process.argv.slice(2).reduce((acc, arg) => {
if (arg === "--help") {
acc.help = true
} else if (arg.startsWith("--locale=")) {
acc.locale = arg.split("=")[1]
} else if (arg.startsWith("--file=")) {
acc.file = arg.split("=")[1]
}
return acc
}, {})
// Display help information
if (args.help) {
console.log(`
Find missing i18n translations
A useful script to identify whether the i18n keys used in component files exist in all language files.
Usage:
node scripts/find-missing-i18n-key.js [options]
Options:
--locale=<locale> Only check a specific language (e.g., --locale=de)
--file=<file> Only check a specific file (e.g., --file=chat.json)
--help Display help information
Output:
- Generate a report of missing translations
`)
process.exit(0)
}
// Directories to traverse and their corresponding locales
const DIRS = {
components: {
path: path.join(__dirname, "../webview-ui/src/components"),
localesDir: path.join(__dirname, "../webview-ui/src/i18n/locales"),
},
src: {
path: path.join(__dirname, "../src"),
localesDir: path.join(__dirname, "../src/i18n/locales"),
},
}
// Regular expressions to match i18n keys
const i18nPatterns = [
/{t\("([^"]+)"\)}/g, // Match {t("key")} format
/i18nKey="([^"]+)"/g, // Match i18nKey="key" format
/t\("([a-zA-Z][a-zA-Z0-9_]*[:.][a-zA-Z0-9_.]+)"\)/g, // Match t("key") format, where key contains a colon or dot
]
// Get all language directories for a specific locales directory
function getLocaleDirs(localesDir) {
try {
const allLocales = fs.readdirSync(localesDir).filter((file) => {
const stats = fs.statSync(path.join(localesDir, file))
return stats.isDirectory() // Do not exclude any language directories
})
// Filter to a specific language if specified
return args.locale ? allLocales.filter((locale) => locale === args.locale) : allLocales
} catch (error) {
if (error.code === "ENOENT") {
console.warn(`Warning: Locales directory not found: ${localesDir}`)
return []
}
throw error
}
}
// Get the value from JSON by path
function getValueByPath(obj, path) {
const parts = path.split(".")
let current = obj
for (const part of parts) {
if (current === undefined || current === null) {
return undefined
}
current = current[part]
}
return current
}
// Check if the key exists in all language files, return a list of missing language files
function checkKeyInLocales(key, localeDirs, localesDir) {
const [file, ...pathParts] = key.split(":")
const jsonPath = pathParts.join(".")
const missingLocales = []
localeDirs.forEach((locale) => {
const filePath = path.join(localesDir, locale, `${file}.json`)
if (!fs.existsSync(filePath)) {
missingLocales.push(`${locale}/${file}.json`)
return
}
const json = JSON.parse(fs.readFileSync(filePath, "utf8"))
if (getValueByPath(json, jsonPath) === undefined) {
missingLocales.push(`${locale}/${file}.json`)
}
})
return missingLocales
}
// Recursively traverse the directory
function findMissingI18nKeys() {
const results = []
function walk(dir, baseDir, localeDirs, localesDir) {
const files = fs.readdirSync(dir)
for (const file of files) {
const filePath = path.join(dir, file)
const stat = fs.statSync(filePath)
// Exclude test files and __mocks__ directory
if (filePath.includes(".test.") || filePath.includes("__mocks__")) continue
if (stat.isDirectory()) {
walk(filePath, baseDir, localeDirs, localesDir) // Recursively traverse subdirectories
} else if (stat.isFile() && [".ts", ".tsx", ".js", ".jsx"].includes(path.extname(filePath))) {
const content = fs.readFileSync(filePath, "utf8")
// Match all i18n keys
for (const pattern of i18nPatterns) {
let match
while ((match = pattern.exec(content)) !== null) {
const key = match[1]
const missingLocales = checkKeyInLocales(key, localeDirs, localesDir)
if (missingLocales.length > 0) {
results.push({
key,
missingLocales,
file: path.relative(baseDir, filePath),
})
}
}
}
}
}
}
// Walk through all directories
Object.entries(DIRS).forEach(([name, config]) => {
const localeDirs = getLocaleDirs(config.localesDir)
if (localeDirs.length > 0) {
console.log(`\nChecking ${name} directory with ${localeDirs.length} languages: ${localeDirs.join(", ")}`)
walk(config.path, config.path, localeDirs, config.localesDir)
}
})
return results
}
// Execute and output the results
function main() {
try {
if (args.locale) {
// Check if the specified locale exists in any of the locales directories
const localeExists = Object.values(DIRS).some((config) => {
const localeDirs = getLocaleDirs(config.localesDir)
return localeDirs.includes(args.locale)
})
if (!localeExists) {
console.error(`Error: Language '${args.locale}' not found in any locales directory`)
process.exit(1)
}
}
const missingKeys = findMissingI18nKeys()
if (missingKeys.length === 0) {
console.log("\n✅ All i18n keys are present!")
return
}
console.log("\nMissing i18n keys:\n")
missingKeys.forEach(({ key, missingLocales, file }) => {
console.log(`File: ${file}`)
console.log(`Key: ${key}`)
console.log("Missing in:")
missingLocales.forEach((file) => console.log(` - ${file}`))
console.log("-------------------")
})
// Exit code 1 indicates missing keys
process.exit(1)
} catch (error) {
console.error("Error:", error.message)
console.error(error.stack)
process.exit(1)
}
}
main()