|
| 1 | +import fs from "node:fs" |
| 2 | +import path from "node:path" |
| 3 | + |
| 4 | +type LanguageCompletion = Record<string, number> |
| 5 | + |
| 6 | +function getLanguageFiles(dir: string): string[] { |
| 7 | + return fs.readdirSync(dir).filter((file) => file.endsWith(".json")) |
| 8 | +} |
| 9 | + |
| 10 | +function getNamespaces(localesDir: string): string[] { |
| 11 | + return fs |
| 12 | + .readdirSync(localesDir) |
| 13 | + .filter((file) => fs.statSync(path.join(localesDir, file)).isDirectory()) |
| 14 | +} |
| 15 | + |
| 16 | +function countKeys(obj: any): number { |
| 17 | + let count = 0 |
| 18 | + for (const key in obj) { |
| 19 | + if (typeof obj[key] === "object") { |
| 20 | + count += countKeys(obj[key]) |
| 21 | + } else { |
| 22 | + count++ |
| 23 | + } |
| 24 | + } |
| 25 | + return count |
| 26 | +} |
| 27 | + |
| 28 | +function calculateCompleteness(localesDir: string): LanguageCompletion { |
| 29 | + const namespaces = getNamespaces(localesDir) |
| 30 | + const languages = new Set<string>() |
| 31 | + const keyCount: Record<string, number> = {} |
| 32 | + |
| 33 | + namespaces.forEach((namespace) => { |
| 34 | + const namespaceDir = path.join(localesDir, namespace) |
| 35 | + const files = getLanguageFiles(namespaceDir) |
| 36 | + |
| 37 | + files.forEach((file) => { |
| 38 | + const lang = path.basename(file, ".json") |
| 39 | + languages.add(lang) |
| 40 | + |
| 41 | + const content = JSON.parse(fs.readFileSync(path.join(namespaceDir, file), "utf-8")) |
| 42 | + keyCount[lang] = (keyCount[lang] || 0) + countKeys(content) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + const enCount = keyCount["en"] || 0 |
| 47 | + const completeness: LanguageCompletion = {} |
| 48 | + |
| 49 | + languages.forEach((lang) => { |
| 50 | + if (lang !== "en") { |
| 51 | + const percent = Math.round((keyCount[lang] / enCount) * 100) |
| 52 | + completeness[lang] = percent |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + return completeness |
| 57 | +} |
| 58 | + |
| 59 | +const i18n = calculateCompleteness(path.resolve(__dirname, "../locales")) |
| 60 | +export default i18n |
0 commit comments