diff --git a/scripts/i18n.test.ts b/scripts/i18n.test.ts index 2926aed5c9..2931f5296b 100644 --- a/scripts/i18n.test.ts +++ b/scripts/i18n.test.ts @@ -203,6 +203,75 @@ test('rich-text validation preserves attributed and self-closing tags', () => { ) }) +test('rich-text validation catches renumbered numbered tags, broken nesting, and stricter HTML/comment tags', () => { + // Valid numbered tags and valid sibling reorder pass. + expect(validateTranslation('<0>Open <1>docs', '<1>docs <0>Ouvrir', 'fr')).toEqual( + [], + ) + // Math comparison with space is not mistaken for a tag. + expect(validateTranslation('Width < 5 m', 'Largeur < 5 m', 'fr')).toEqual([]) + + // Renumbered tags fail. + expect(validateTranslation('<0>Open', '<1>Ouvrir', 'fr')).toContain( + 'rich-text tags differ from English', + ) + + // Broken nesting fails even when the sorted tag set matches English. + expect(validateTranslation('<0><1>Open', '<0><1>Ouvrir', 'fr')).toContain( + 'broken rich-text tag nesting', + ) + expect(validateTranslation('<0>Open', 'Ouvrir<0>', 'fr')).toContain( + 'broken rich-text tag nesting', + ) + expect( + validateTranslation( + 'Open', + 'Ouvrir', + 'fr', + ), + ).toContain('broken rich-text tag nesting') + + // Stricter tag check catches slash-separated HTML tags, comments, and unclosed tags. + for (const injected of [ + 'Ouvrir ', + 'Ouvrir ', + 'Ouvrir ', + 'Ouvrir { + const english = '{gender, select, male {He} female {She} other {They}}' + + expect( + validateTranslation(english, '{gender, select, male {Il} female {Elle} other {Ils}}', 'fr'), + ).toEqual([]) + expect(validateTranslation(english, '{gender, select, male {Il} other {Ils}}', 'fr')).toContain( + 'select selector "gender" is missing branch "female"', + ) + expect(validateTranslation(english, '{gender, select, male {Il} female {Elle}}', 'fr')).toContain( + 'select selector "gender" is missing branch "other"', + ) + expect( + validateTranslation( + '{gender, select, male {He} female {She}}', + '{gender, select, male {Il} female {Elle}}', + 'fr', + ), + ).toContain('select selector "gender" is missing "other"') + expect( + validateTranslation( + '{count, plural, one {# item} other {# items}}', + '{count, plural, one {# élément}}', + 'fr', + ), + ).toContain('plural selector "count" is missing "other"') +}) + test('source drift fails checking, then extraction updates English and flags translations', () => { const root = catalogProject() const sample = join(root, 'packages/editor/src/sample.tsx') diff --git a/scripts/i18n.ts b/scripts/i18n.ts index 1bcceb009b..3712a01788 100644 --- a/scripts/i18n.ts +++ b/scripts/i18n.ts @@ -193,9 +193,17 @@ function variableNames(tokens: CompiledToken[]): string[] { return [...names].sort() } -function tags(message: string): string[] { +function extractTags(message: string): string[] { const found: string[] = [] for (let start = message.indexOf('<'); start >= 0; start = message.indexOf('<', start + 1)) { + const next = message[start + 1] + if (!next) continue + // Treat any `<` followed by a letter, `!`, or `/` as a tag (catches ``, + // ``, ``, and closing tags), plus Lingui numbered tags (`<0>`, `<0/>`). + const isStrictTagStart = /[A-Za-z!/]/.test(next) + const isNumericTagStart = /\d/.test(next) + if (!(isStrictTagStart || isNumericTagStart)) continue + let quote: '"' | "'" | undefined let end = start + 1 for (; end < message.length; end += 1) { @@ -208,11 +216,44 @@ function tags(message: string): string[] { break } } - if (end >= message.length) continue + if (end >= message.length) { + if (isStrictTagStart) found.push(message.slice(start)) + continue + } const token = message.slice(start, end + 1) - if (/^<\s*\/?\s*[A-Za-z][\w-]*(?:\s|\/?>)/.test(token)) found.push(token) + if (isStrictTagStart || /^<\d+\s*\/?>$/.test(token)) { + found.push(token) + } } - return found.sort() + return found +} + +function tags(message: string): string[] { + return extractTags(message).sort() +} + +function tagNestingErrors(orderedTags: readonly string[]): string[] { + const stack: string[] = [] + for (const token of orderedTags) { + if (!token.endsWith('>')) return ['malformed rich-text tag'] + if (token.startsWith('')) continue + const closeMatch = /^<\/\s*([A-Za-z0-9][\w-]*)\s*>$/.exec(token) + if (closeMatch) { + const name = closeMatch[1]! + if (stack.length === 0 || stack[stack.length - 1] !== name) { + return ['broken rich-text tag nesting'] + } + stack.pop() + continue + } + const openMatch = /^<\s*([A-Za-z0-9][\w-]*)(?:[\s/>]|$)/.exec(token) + if (openMatch) { + stack.push(openMatch[1]!) + } + } + if (stack.length > 0) return ['broken rich-text tag nesting'] + return [] } function plurals(tokens: CompiledToken[]): Map }> { @@ -242,6 +283,26 @@ function plurals(tokens: CompiledToken[]): Map> { + const found = new Map>() + const visit = (entries: CompiledToken[]) => { + for (const token of entries) { + if (typeof token === 'string') continue + const type = token[1] + const choices = token[2] + if (type === 'select' && choices && typeof choices === 'object') { + found.set(token[0], new Set(Object.keys(choices))) + } + if (!(choices && typeof choices === 'object')) continue + for (const branch of Object.values(choices)) { + if (Array.isArray(branch)) visit(branch as CompiledToken[]) + } + } + } + visit(tokens) + return found +} + function requiredPluralCategories(locale: string, type: string): Intl.LDMLPluralRule[] { return new Intl.PluralRules(locale, { type: type === 'selectordinal' ? 'ordinal' : 'cardinal', @@ -300,9 +361,11 @@ export function validateTranslation( ) { errors.push('placeholder names differ from English') } - if (JSON.stringify(tags(source)) !== JSON.stringify(tags(translation))) { + const translatedOrderedTags = extractTags(translation) + if (JSON.stringify(tags(source)) !== JSON.stringify([...translatedOrderedTags].sort())) { errors.push('rich-text tags differ from English') } + errors.push(...tagNestingErrors(translatedOrderedTags)) const sourcePlurals = plurals(sourceTokens) const translatedPlurals = plurals(translatedTokens) @@ -312,12 +375,44 @@ export function validateTranslation( errors.push(`plural selector "${name}" is missing or changed`) continue } - for (const category of requiredPluralCategories(locale, sourcePlural.type)) { + const required = new Set([ + ...requiredPluralCategories(locale, sourcePlural.type), + 'other', + ]) + for (const category of required) { if (!translatedPlural.categories.has(category)) { errors.push(`plural selector "${name}" is missing "${category}"`) } } } + for (const [name, translatedPlural] of translatedPlurals) { + if (!sourcePlurals.has(name) && !translatedPlural.categories.has('other')) { + errors.push(`plural selector "${name}" is missing "other"`) + } + } + + const sourceSelects = selects(sourceTokens) + const translatedSelects = selects(translatedTokens) + for (const [name, sourceBranches] of sourceSelects) { + const translatedBranches = translatedSelects.get(name) + if (!translatedBranches) { + errors.push(`select selector "${name}" is missing or changed`) + continue + } + for (const branch of sourceBranches) { + if (!translatedBranches.has(branch)) { + errors.push(`select selector "${name}" is missing branch "${branch}"`) + } + } + if (!translatedBranches.has('other')) { + errors.push(`select selector "${name}" is missing "other"`) + } + } + for (const [name, translatedBranches] of translatedSelects) { + if (!sourceSelects.has(name) && !translatedBranches.has('other')) { + errors.push(`select selector "${name}" is missing "other"`) + } + } errors.push(...validateGlossary(source, translation, locale, glossary)) return errors }