diff --git a/bin/util/transifex.js b/bin/util/transifex.js index d5b88ec11..276aede1d 100644 --- a/bin/util/transifex.js +++ b/bin/util/transifex.js @@ -72,15 +72,25 @@ const parseVars = (pluralForm) => { class PluralForms { static empty(length) { return new PluralForms(new Array(length).fill('')); } - static fromVueI18n(message) { + static fromVueI18n(key, message) { const forms = message.split(' | '); if (forms.length > 2) logThenThrow(message, 'a pluralized message must have exactly two forms'); - for (const form of forms) { + for (let i = 0; i < forms.length; i += 1) { + const form = forms[i]; + if (form.includes('|')) logThenThrow(message, 'unexpected |'); - if (/(^\s|\s$|\s\s)/.test(form)) - logThenThrow(message, 'unexpected white space'); + + const badWhitespace = form.match(/^\s+|\s+$|\s\s+/); + if (badWhitespace) { + const badLength = badWhitespace[0].length; + const badPath = forms.length === 1 ? key : `${key}[${i}]`; + console.error(`unexpected white space in translation string '${badPath}':`); // eslint-disable-line no-console + console.error(` [${form}]`); // eslint-disable-line no-console + console.error(` [${''.padStart(badLength, '^').padStart(badWhitespace.index + badLength, ' ').padEnd(form.length, ' ')}]`); // eslint-disable-line no-console + throw new Error(`unexpected whitespace in message '${badPath}' at index ${badWhitespace.index} ("${message}")`); + } } return new PluralForms(forms); @@ -663,8 +673,8 @@ const rekeyTranslations = (source, translated, transifexPaths) => { // Returns the Vue I18n messages for the source locale after converting them to // PluralForms objects. const readSourceMessages = (localesDir, filenamesByComponent) => { - const reviver = (_, value) => - (typeof value === 'string' ? PluralForms.fromVueI18n(value) : value); + const reviver = (key, value) => + (typeof value === 'string' ? PluralForms.fromVueI18n(key, value) : value); // Read the root messages. const messages = parse(