Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transifex/restructure: add context to whitespace errors #832

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions bin/util/transifex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down