|
1
|
import fs from "fs" |
|
2
|
import path from "path" |
|
3
|
import { describe, expect, test } from "vitest" |
|
4
|
|
|
5
|
import schema from "../$schema.json" |
|
6
|
import pluralConfig from "../../plural-config.json" |
|
7
|
|
|
8
|
const translationsDir = path.join(__dirname, "..") |
|
9
|
|
|
10
|
function getRequiredKeysFromSchema(schema: any, prefix = ""): string[] { |
|
11
|
const keys: string[] = [] |
|
12
|
|
|
13
|
if (schema.type === "object" && schema.properties) { |
|
14
|
Object.entries(schema.properties).forEach(([key, value]: [string, any]) => { |
|
15
|
const fullKey = prefix ? `${prefix}.${key}` : key |
|
16
|
|
|
17
|
if (value.type === "object") { |
|
18
|
keys.push(...getRequiredKeysFromSchema(value, fullKey)) |
|
19
|
} else if (schema.required?.includes(key)) { |
|
20
|
keys.push(fullKey) |
|
21
|
} |
|
22
|
}) |
|
23
|
} |
|
24
|
|
|
25
|
return keys.sort() |
|
26
|
} |
|
27
|
|
|
28
|
function getAllKeysFromSchema(schema: any, prefix = ""): Set<string> { |
|
29
|
const keys = new Set<string>() |
|
30
|
|
|
31
|
if (schema.type === "object" && schema.properties) { |
|
32
|
Object.entries(schema.properties).forEach(([key, value]: [string, any]) => { |
|
33
|
const fullKey = prefix ? `${prefix}.${key}` : key |
|
34
|
keys.add(fullKey) |
|
35
|
|
|
36
|
if (value.type === "object") { |
|
37
|
const nestedKeys = getAllKeysFromSchema(value, fullKey) |
|
38
|
nestedKeys.forEach((k) => keys.add(k)) |
|
39
|
} |
|
40
|
}) |
|
41
|
} |
|
42
|
|
|
43
|
return keys |
|
44
|
} |
|
45
|
|
|
46
|
function getTranslationKeys(obj: any, prefix = ""): string[] { |
|
47
|
const keys: string[] = [] |
|
48
|
|
|
49
|
Object.entries(obj).forEach(([key, value]) => { |
|
50
|
const fullKey = prefix ? `${prefix}.${key}` : key |
|
51
|
|
|
52
|
if (value && typeof value === "object" && !Array.isArray(value)) { |
|
53
|
keys.push(...getTranslationKeys(value, fullKey)) |
|
54
|
} else { |
|
55
|
keys.push(fullKey) |
|
56
|
} |
|
57
|
}) |
|
58
|
|
|
59
|
return keys.sort() |
|
60
|
} |
|
61
|
|
|
62
|
function findPluralGroups( |
|
63
|
obj: any, |
|
64
|
currentPath = "" |
|
65
|
): Map<string, Set<string>> { |
|
66
|
const groups = new Map<string, Set<string>>() |
|
67
|
|
|
68
|
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { |
|
69
|
return groups |
|
70
|
} |
|
71
|
|
|
72
|
Object.entries(obj).forEach(([key, value]) => { |
|
73
|
const pluralMatch = key.match(/^(.+)_(zero|one|two|few|many|other)$/) |
|
74
|
|
|
75
|
if (pluralMatch) { |
|
76
|
const [, baseKey, form] = pluralMatch |
|
77
|
const fullPath = currentPath ? `${currentPath}.${baseKey}` : baseKey |
|
78
|
|
|
79
|
if (!groups.has(fullPath)) { |
|
80
|
groups.set(fullPath, new Set()) |
|
81
|
} |
|
82
|
groups.get(fullPath)!.add(form) |
|
83
|
} |
|
84
|
|
|
85
|
if (value && typeof value === "object" && !Array.isArray(value)) { |
|
86
|
const fullPath = currentPath ? `${currentPath}.${key}` : key |
|
87
|
const nestedGroups = findPluralGroups(value, fullPath) |
|
88
|
nestedGroups.forEach((forms, path) => groups.set(path, forms)) |
|
89
|
} |
|
90
|
}) |
|
91
|
|
|
92
|
return groups |
|
93
|
} |
|
94
|
|
|
95
|
describe("translation schema validation", () => { |
|
96
|
test("en.json should have all required keys defined in schema", () => { |
|
97
|
const enPath = path.join(translationsDir, "en.json") |
|
98
|
const enTranslations = JSON.parse(fs.readFileSync(enPath, "utf-8")) |
|
99
|
|
|
100
|
const schemaKeys = getRequiredKeysFromSchema(schema) |
|
101
|
const translationKeys = getTranslationKeys(enTranslations) |
|
102
|
|
|
103
|
const missing = schemaKeys.filter((key) => !translationKeys.includes(key)) |
|
104
|
|
|
105
|
if (missing.length > 0) { |
|
106
|
console.error("\nMissing required keys in en.json:", missing) |
|
107
|
} |
|
108
|
|
|
109
|
expect(missing).toEqual([]) |
|
110
|
}) |
|
111
|
|
|
112
|
test("en.json should not have extra keys", () => { |
|
113
|
const enPath = path.join(translationsDir, "en.json") |
|
114
|
const enTranslations = JSON.parse(fs.readFileSync(enPath, "utf-8")) |
|
115
|
|
|
116
|
const allSchemaKeys = getAllKeysFromSchema(schema) |
|
117
|
const translationKeys = getTranslationKeys(enTranslations) |
|
118
|
const extra = translationKeys.filter((key) => !allSchemaKeys.has(key)) |
|
119
|
|
|
120
|
if (extra.length > 0) { |
|
121
|
console.error("\nExtra keys in en.json:", extra) |
|
122
|
} |
|
123
|
|
|
124
|
expect(extra).toEqual([]) |
|
125
|
}) |
|
126
|
|
|
127
|
test("en.json plural keys should match required forms", () => { |
|
128
|
const enPath = path.join(translationsDir, "en.json") |
|
129
|
const enTranslations = JSON.parse(fs.readFileSync(enPath, "utf-8")) |
|
130
|
|
|
131
|
const requiredForms = pluralConfig.en |
|
132
|
const pluralGroups = findPluralGroups(enTranslations) |
|
133
|
const errors: string[] = [] |
|
134
|
|
|
135
|
pluralGroups.forEach((forms, baseKey) => { |
|
136
|
forms.forEach((form) => { |
|
137
|
if (!requiredForms.includes(form)) { |
|
138
|
errors.push(`${baseKey}_${form} - English doesn't use "${form}" form`) |
|
139
|
} |
|
140
|
}) |
|
141
|
requiredForms.forEach((form) => { |
|
142
|
if (!forms.has(form)) { |
|
143
|
errors.push(`${baseKey}_${form} - Missing required form for English`) |
|
144
|
} |
|
145
|
}) |
|
146
|
}) |
|
147
|
|
|
148
|
if (errors.length > 0) { |
|
149
|
console.error("\nPlural form errors in en.json:", errors) |
|
150
|
} |
|
151
|
|
|
152
|
expect(errors).toEqual([]) |
|
153
|
}) |
|
154
|
}) |