1 const Ajv = require("ajv")
2 const fs = require("fs")
3 const path = require("path")
4
5 const schema = require("../../src/i18n/translations/$schema.json")
6 const pluralConfig = require("../../src/i18n/plural-config.json")
7
8 const ajv = new Ajv({ allErrors: true, strict: false })
9 const validateSchema = ajv.compile(schema)
10
11 const fileName = process.argv[2]
12 if (!fileName) {
13 console.error("Please provide a file name (e.g., en.json) as an argument.")
14 process.exit(1)
15 }
16
17 const langCode = fileName.replace(".json", "")
18 const requiredForms = pluralConfig[langCode]
19
20 if (!requiredForms) {
21 console.error(`Language "${langCode}" not found in plural-config.json`)
22 process.exit(1)
23 }
24
25 const filePath = path.join(__dirname, "../../src/i18n/translations", fileName)
26 const translations = JSON.parse(fs.readFileSync(filePath, "utf-8"))
27
28 function findAllPluralKeys(obj, currentPath = "") {
29 const pluralKeys = []
30
31 if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
32 return pluralKeys
33 }
34
35 Object.entries(obj).forEach(([key, value]) => {
36 if (/_(?:zero|one|two|few|many|other)$/.test(key)) {
37 pluralKeys.push({ path: currentPath, key })
38 }
39
40 if (typeof value === "object" && value !== null && !Array.isArray(value)) {
41 const fullPath = currentPath ? `${currentPath}.${key}` : key
42 pluralKeys.push(...findAllPluralKeys(value, fullPath))
43 }
44 })
45
46 return pluralKeys
47 }
48
49 function getNestedValue(obj, path) {
50 if (!path) return obj
51 return path.split(".").reduce((current, key) => current?.[key], obj)
52 }
53
54 const schemaValid = validateSchema(translations)
55
56 const errors = []
57 const allPluralKeys = findAllPluralKeys(translations)
58 const pluralGroups = new Map()
59
60 allPluralKeys.forEach(({ path, key }) => {
61 const baseKey = key.replace(/_(?:zero|one|two|few|many|other)$/, "")
62 const groupKey = path ? `${path}.${baseKey}` : baseKey
63
64 if (!pluralGroups.has(groupKey)) {
65 pluralGroups.set(groupKey, { path, baseKey })
66 }
67 })
68
69 pluralGroups.forEach(({ path, baseKey }, groupKey) => {
70 const parent = getNestedValue(translations, path)
71 const instancePath = path ? `/${path.replace(/\./g, "/")}` : ""
72
73 requiredForms.forEach((form) => {
74 const key = `${baseKey}_${form}`
75 if (!parent?.[key]) {
76 errors.push({
77 type: "missing",
78 instancePath,
79 key,
80 })
81 }
82 })
83
84 const allForms = ["zero", "one", "two", "few", "many", "other"]
85
86 allForms.forEach((form) => {
87 const key = `${baseKey}_${form}`
88 if (parent?.[key] && !requiredForms.includes(form)) {
89 errors.push({
90 type: "extra",
91 instancePath,
92 key,
93 form,
94 })
95 }
96 })
97 })
98
99 if (!schemaValid) {
100 console.error(`\nSchema validation failed for ${fileName}:`)
101 validateSchema.errors?.forEach((error) => {
102 const location = error.instancePath || "root"
103 if (error.keyword === "required") {
104 const missingKey = error.params.missingProperty
105 console.error(` Missing required key: "${missingKey}" at ${location}`)
106 } else if (error.keyword === "additionalProperties") {
107 const extraKey = error.params.additionalProperty
108 console.error(` Unexpected key: "${extraKey}" at ${location}`)
109 } else {
110 console.error(` Error: ${error.message} at ${location}`)
111 }
112 })
113 }
114
115 if (errors.length > 0) {
116 console.error(
117 `\nPlural validation failed for ${fileName} [${requiredForms.join(", ")}]:`
118 )
119
120 errors.forEach(({ type, instancePath, key }) => {
121 const location = instancePath || "root"
122 if (type === "missing") {
123 console.error(` Missing required plural key: "${key}" at ${location}`)
124 } else {
125 console.error(` Unexpected plural key: "${key}" at ${location}`)
126 }
127 })
128 }
129
130 if (!schemaValid || errors.length > 0) {
131 process.exit(1)
132 }
133
134 console.log(`\n✓ ${fileName} is valid and matches the schema.`)
135 process.exit(0)