Skip to content

Commit a541fed

Browse files
committed
Auto-fill missing plural forms with first translated form
This is related to #8.
1 parent 27b71a3 commit a541fed

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/utils/xmlTranslation.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ function validatePluralForms(forms, originalMsgid, expectedCount, itemId, logger
118118
let hasIssues = false;
119119
const issues = [];
120120

121+
// Find first non-empty form to use as fallback.
122+
const fallbackForm = correctedForms.find(f => f && f.trim() !== '') || '';
123+
121124
// Ensure we have exactly the right number of forms.
122125
if (correctedForms.length < expectedCount) {
123126
hasIssues = true;
@@ -127,12 +130,12 @@ function validatePluralForms(forms, originalMsgid, expectedCount, itemId, logger
127130
if (verbosityLevel >= 2) {
128131
logger.warn(
129132
`Insufficient plural forms at index ${itemId} for "${originalMsgid.substring(0, 50)}...": ` +
130-
`Expected ${expectedCount}, got ${correctedForms.length}. Padding with empty strings.`
133+
`Expected ${expectedCount}, got ${correctedForms.length}. Padding with first form as fallback.`
131134
);
132135
}
133136

134137
while (correctedForms.length < expectedCount) {
135-
correctedForms.push('');
138+
correctedForms.push(fallbackForm);
136139
}
137140
} else if (correctedForms.length > expectedCount) {
138141
hasIssues = true;
@@ -164,9 +167,12 @@ function validatePluralForms(forms, originalMsgid, expectedCount, itemId, logger
164167
if (verbosityLevel >= 2) {
165168
logger.warn(
166169
`Incomplete plural forms at index ${itemId} for "${originalMsgid.substring(0, 50)}...": ` +
167-
`Forms at indices [${emptyIndices.join(', ')}] are empty.`
170+
`Forms at indices [${emptyIndices.join(', ')}] are empty. Filling with first form as fallback.`
168171
);
169172
}
173+
174+
// Fill empty forms with the first non-empty form as fallback.
175+
correctedForms = correctedForms.map(f => (f && f.trim() !== '') ? f : fallbackForm);
170176
}
171177

172178
if (hasIssues && verbosityLevel >= 3) {

0 commit comments

Comments
 (0)