Skip to content

Commit d7fe450

Browse files
committed
feat: improve validate-plural-forms.js (statistics, instruction, check forms without {{count}})
1 parent f0b6867 commit d7fe450

4 files changed

Lines changed: 96 additions & 13 deletions

File tree

‎.roo/rules-translate/001-general-rules.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Example:
106106

107107
To list the plural forms of any locale, run: `node -p 'new Intl.PluralRules("<locale>").resolvedOptions().pluralCategories'`
108108

109-
`node scripts/find-missing-translations.js` reports a plural key only when a locale has no key with its base name (`key` or `key_*`). `node scripts/validate-plural-forms.js` reports missing forms, extra forms, bare keys next to plural forms and plural mistakes in English.
109+
`node scripts/find-missing-translations.js` reports a plural key only when a locale has no key with its base name (`key` or `key_*`). `node scripts/validate-plural-forms.js` reports missing forms, forms without `{{count}}`, extra forms, bare keys next to plural forms and plural mistakes in English.
110110

111111
# 7. WORKFLOW AND APPROACH
112112

‎.roo/skills/roo-translation/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Example:
152152

153153
To list the plural forms of any locale, run: `node -p 'new Intl.PluralRules("<locale>").resolvedOptions().pluralCategories'`.
154154

155-
`node scripts/find-missing-translations.js` reports a plural key only when a locale has no key with its base name (`key` or `key_*`). `node scripts/validate-plural-forms.js` reports missing forms, extra forms, bare keys next to plural forms and plural mistakes in English.
155+
`node scripts/find-missing-translations.js` reports a plural key only when a locale has no key with its base name (`key` or `key_*`). `node scripts/validate-plural-forms.js` reports missing forms, forms without `{{count}}`, extra forms, bare keys next to plural forms and plural mistakes in English.
156156

157157
## Translation Workflow
158158

‎scripts/validate-plural-forms.js‎

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Validate Plural Forms
4242
4343
Checks that every plural key has exactly the CLDR plural forms of each locale, English included:
4444
- no missing form (ru needs one, few, many, other; ja needs only other)
45+
- {{count}} in every form except "zero" (ru "one" also covers 21, fr "one" also covers 0)
4546
- no form the locale does not have (no _one in ja)
4647
- no bare key next to the plural forms (i18next uses it in place of a missing form)
4748
@@ -91,30 +92,43 @@ function readJson(filePath) {
9192
}
9293
}
9394

94-
// Each English plural key must have exactly the locale's forms, and no bare key next to them
95+
// {{count}}, {{count, number}} and {{- count}}
96+
const COUNT_PLACEHOLDER = /\{\{-?\s*count\s*(,[^}]*)?\}\}/
97+
98+
// Each English plural key must have exactly the locale's forms, each with {{count}} (except the "zero" override),
99+
// and no bare key next to them
95100
function validatePluralForms(locale, values, pluralBases) {
96101
const problems = []
97102

98103
for (const [base, entry] of pluralBases) {
99104
const required = requiredPluralForms(locale, entry)
100105

101106
for (const form of required) {
102-
if (!values.has(`${base}_${form}`)) {
103-
problems.push({ key: `${base}_${form}`, problem: `missing the "${form}" plural form` })
107+
const value = values.get(`${base}_${form}`)
108+
if (value === undefined) {
109+
problems.push({ type: "missing", key: `${base}_${form}`, problem: `missing the "${form}" plural form` })
110+
} else if (form !== "zero" && typeof value === "string" && !COUNT_PLACEHOLDER.test(value)) {
111+
// "_one" also covers 21 in ru and 0 in fr, so a hardcoded "1" shows the wrong number
112+
problems.push({
113+
type: "nocount",
114+
key: `${base}_${form}`,
115+
problem: `the "${form}" form has no {{count}} placeholder`,
116+
})
104117
}
105118
}
106119

107120
for (const form of PLURAL_FORMS) {
108121
if (!required.includes(form) && values.has(`${base}_${form}`)) {
109122
problems.push({
123+
type: "extra",
110124
key: `${base}_${form}`,
111125
problem: `"${form}" is not a CLDR plural form for ${locale}; remove it`,
112126
})
113127
}
114128
}
115129

116130
if (typeof values.get(base) === "string") {
117-
problems.push({ key: base, problem: "bare key next to plural forms; remove it" })
131+
problems.push({ type: "bare", key: base, problem: "bare key next to plural forms; remove it" })
118132
}
119133
}
120134

@@ -151,7 +165,7 @@ function validateArea(area) {
151165
const pluralBases = new Map(
152166
englishFiles.map((file) => [file, findPluralBases(flatten(readJson(path.join(englishDir, file))))]),
153167
)
154-
let hasProblems = false
168+
const report = []
155169

156170
for (const locale of locales) {
157171
const problemsByFile = []
@@ -169,26 +183,51 @@ function validateArea(area) {
169183
continue
170184
}
171185

172-
hasProblems = true
173186
console.log(`📝 ${locale}:`)
174187
for (const { file, problems } of problemsByFile) {
175188
console.log(` - ${file}: ${problems.length} plural form problem(s)`)
176189
for (const { key, problem } of problems) {
177190
console.log(` ${key}: ${problem}`)
178191
}
192+
report.push({ area, locale, file, problems })
179193
}
180194
console.log("")
181195
}
182196

183-
return hasProblems
197+
return report
198+
}
199+
200+
// Counts the problems of every checked area by type
201+
function printStatistics(report) {
202+
const problems = report.flatMap((entry) => entry.problems)
203+
const count = (type) => problems.filter((problem) => problem.type === type).length
204+
const locales = new Set(report.map((entry) => entry.locale))
205+
206+
console.log("\nPlural Forms Statistics:\n")
207+
console.log(` Missing plural forms: ${count("missing")}`)
208+
console.log(` Forms without {{count}}: ${count("nocount")}`)
209+
console.log(` Forms that are not CLDR plural forms: ${count("extra")}`)
210+
console.log(` Bare keys next to plural forms: ${count("bare")}`)
211+
console.log(` Total problems: ${problems.length}`)
212+
console.log(` Locale files with problems: ${report.length}`)
213+
console.log(` Locales with problems: ${locales.size}`)
184214
}
185215

186216
const areas = args.area === "all" ? ["core", "webview"] : [args.area]
187-
const hasProblems = areas.map(validateArea).some(Boolean)
217+
const report = areas.flatMap(validateArea)
188218

189-
if (hasProblems) {
190-
console.log("\n✏️ Give each plural key exactly the CLDR plural forms of its locale.")
191-
console.log("List a locale's forms with Intl.PluralRules(<locale>).resolvedOptions().pluralCategories")
219+
if (report.length > 0) {
220+
printStatistics(report)
221+
console.log("\n✏️ To fix the plural form problems:")
222+
console.log(
223+
"1. List the plural forms of a locale with: node -p 'new Intl.PluralRules(\"<locale>\").resolvedOptions().pluralCategories'",
224+
)
225+
console.log(
226+
"2. Synchronize the plural forms of each key with that list: add each missing form, translated from the English plural key, and remove each form that is not in the list",
227+
)
228+
console.log('3. Put {{count}} in every form, including "one", instead of a hardcoded number')
229+
console.log("4. Remove each bare key that is next to plural forms")
230+
console.log("5. Run this script again to verify that all plural forms are correct")
192231
process.exit(1)
193232
}
194233

‎scripts/validate-plural-forms.test.mjs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ describe("validate-plural-forms", () => {
2323
assert.equal(status, 0, output)
2424
})
2525

26+
it("accepts formatted and unescaped {{count}} placeholders", () => {
27+
const { status, output } = validate({
28+
en: { files_one: "{{count, number}} file", files_other: "{{- count}} files" },
29+
ja: { files_other: "{{ count }} ファイル" },
30+
})
31+
assert.equal(status, 0, output)
32+
})
33+
2634
it("lists missing forms from zero to other", () => {
2735
const { status, output } = validate({ en: { ...en, files_zero: "No files" }, fr: {}, ru: {} })
2836
assert.equal(status, 1, output)
@@ -37,6 +45,30 @@ describe("validate-plural-forms", () => {
3745
assert.deepEqual(forms("ru"), ["zero", "one", "few", "many", "other"])
3846
})
3947

48+
it("prints statistics by problem type and the steps to fix them", () => {
49+
const { status, output } = validate({
50+
en,
51+
ru: { ...ru, files_one: "1 файл", files_few: undefined, files: "{{count}} файл" },
52+
ja: { ...ja, files_one: "1 ファイル" },
53+
})
54+
assert.equal(status, 1, output)
55+
assert.match(output, /Missing plural forms: +1\n/)
56+
assert.match(output, /Forms without \{\{count\}\}: +1\n/)
57+
assert.match(output, /Forms that are not CLDR plural forms: +1\n/)
58+
assert.match(output, /Bare keys next to plural forms: +1\n/)
59+
assert.match(output, /Total problems: +4\n/)
60+
assert.match(output, /Locale files with problems: +2\n/)
61+
assert.match(output, /Locales with problems: +2\n/)
62+
const steps = [...output.split("To fix the plural form problems:")[1].matchAll(/^(\d)\. /gm)].map(([, n]) => n)
63+
assert.deepEqual(steps, ["1", "2", "3", "4", "5"])
64+
})
65+
66+
it("prints no statistics when every plural form is correct", () => {
67+
const { status, output } = validate({ en, ru, ja })
68+
assert.equal(status, 0, output)
69+
assert.doesNotMatch(output, /Plural Forms Statistics|To fix the plural form problems/)
70+
})
71+
4072
it("ignores keys that are not plural", () => {
4173
const { status, output } = validate({ en: { ...en, title: "Files" }, ja })
4274
assert.equal(status, 0, output)
@@ -87,6 +119,18 @@ describe("validate-plural-forms", () => {
87119
"ru",
88120
/files_few: missing the "few" plural form/,
89121
],
122+
[
123+
"a hardcoded number in place of {{count}}",
124+
{ en, ru: { ...ru, files_one: "1 файл" } },
125+
"ru",
126+
/files_one: the "one" form has no \{\{count\}\} placeholder/,
127+
],
128+
[
129+
"an English form without {{count}}",
130+
{ en: { ...en, files_one: "One file" }, ja },
131+
"en",
132+
/files_one: the "one" form has no \{\{count\}\} placeholder/,
133+
],
90134
]) {
91135
it(`rejects ${name}`, () => {
92136
const { status, output } = validate(byLocale)

0 commit comments

Comments
 (0)