🈯 fix: Accept Locale Count Placeholders in Locize Plural Forms (#17625)

This commit is contained in:
Marco Beretta 2026-10-29 17:38:45 +01:00 • committed by GitHub
parent a5cd61ef6e
commit 960f6daf6a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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)) {