diff --git a/scripts/validate-locize-download.mjs b/scripts/validate-locize-download.mjs index 4f53719df4..10ad23bd4f 100644 --- a/scripts/validate-locize-download.mjs +++ b/scripts/validate-locize-download.mjs @@ -19,6 +19,7 @@ if (!baseDir || !currentDir) { const errors = []; const placeholderPattern = /\{\{[^{}]+\}\}|\{[^{}]+\}|%[-+0-9.#]*[a-zA-Z]/g; +const pluralSuffixPattern = /_(zero|one|two|few|many|other)$/; async function jsonFiles(directory, relative = '') { const entries = await readdir(path.join(directory, relative), { withFileTypes: true }); @@ -51,8 +52,17 @@ function flatten(value, prefix = '', output = new Map()) { return output; } -function placeholders(value) { - return (value.match(placeholderPattern) ?? []).sort().join('\u0000'); +/** + * i18next passes `count` to every plural form, so a locale may spell it out where English + * writes the number (`_one`: "1 chat") and omit it where its own grammar does not need it. + */ +function placeholders(value, key) { + const matches = value.match(placeholderPattern) ?? []; + const ignoreCount = pluralSuffixPattern.test(key); + return matches + .filter((match) => !ignoreCount || match.replace(/\s/g, '') !== '{{count}}') + .sort() + .join('\u0000'); } function boundaryWhitespace(value) { @@ -102,7 +112,7 @@ if (currentSet.has(englishRelative)) { const baseline = baselineValuesByFile.get(relative)?.get(key); const changedSinceBaseline = baseline === undefined || baseline !== value; if (changedSinceBaseline && typeof source === 'string' && typeof value === 'string') { - if (placeholders(source) !== placeholders(value)) { + if (placeholders(source, key) !== placeholders(value, key)) { errors.push(`${relative}: placeholder mismatch with English for ${key}`); } if (value !== value.trim() && boundaryWhitespace(source) !== boundaryWhitespace(value)) {