Skip to content
Open
22 changes: 16 additions & 6 deletions bin/util/transifex.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,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);
Expand Down Expand Up @@ -666,8 +676,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(
Expand Down