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

Conversation

alxndrsn
Copy link
Contributor

Example

Add a new translation string:

diff --git a/src/components/account/login.vue b/src/components/account/login.vue
index 86dc3f8d..416a53c8 100644
--- a/src/components/account/login.vue
+++ b/src/components/account/login.vue
@@ -143,6 +143,7 @@ export default {
 {
   "en": {
     "alert": {
+      "someNewAlert": "I have                    a lot of space.",
       "alreadyLoggedIn": "A user is already logged in. Please refresh the page to continue.",
       "changePassword": "Your password is shorter than 10 characters. To protect your account, please change your password to make it longer."
     },

Current master

$ node bin/transifex/restructure.js
I have                    a lot of space.
could not parse the Vue I18n JSON of AccountLogin
/home/user/workspaces/odk/frontend/bin/util/transifex.js:692
        throw e;
        ^

Error: unexpected white space
    at logThenThrow (/home/user/workspaces/odk/frontend/bin/util/util.js:6:9)
    at Function.fromVueI18n (/home/user/workspaces/odk/frontend/bin/util/transifex.js:83:9)
    at reviver (/home/user/workspaces/odk/frontend/bin/util/transifex.js:667:46)
    at transform (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:83:5)
    at parse_object (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:283:17)
    at walk (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:365:12)
    at parse_object (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:283:33)
    at walk (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:365:12)
    at parse_object (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:283:33)
    at walk (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:365:12)

This PR

$ node bin/transifex/restructure.js
unexpected white space in translation string 'someNewAlert':
  [I have                    a lot of space.]
  [      ^^^^^^^^^^^^^^^^^^^^               ]
could not parse the Vue I18n JSON of AccountLogin
/home/user/workspaces/odk/frontend/bin/util/transifex.js:699
        throw e;
        ^

Error: unexpected whitespace in message 'someNewAlert' at index 6 ("I have                    a lot of space.")
    at Function.fromVueI18n (/home/user/workspaces/odk/frontend/bin/util/transifex.js:89:15)
    at reviver (/home/user/workspaces/odk/frontend/bin/util/transifex.js:674:46)
    at transform (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:83:5)
    at parse_object (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:283:17)
    at walk (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:365:12)
    at parse_object (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:283:33)
    at walk (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:365:12)
    at parse_object (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:283:33)
    at walk (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:365:12)
    at parse (/home/user/workspaces/odk/frontend/node_modules/comment-json/src/parse.js:417:16)

@alxndrsn alxndrsn changed the title transifex/restructure: throw more helpful whitespace errors transifex/restructure: add context to whitespace errors Jul 26, 2023
Copy link
Member

@matthew-white matthew-white left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a clearer description of the error!

bin/util/transifex.js Outdated Show resolved Hide resolved
bin/util/transifex.js Outdated Show resolved Hide resolved
Comment on lines 87 to 89
console.error(` [${message}]`); // eslint-disable-line no-console
console.error(` [${''.padStart(badLength, '^').padStart(badWhitespace.index + badLength, ' ').padEnd(message.length, ' ')}]`); // eslint-disable-line no-console
throw new Error(`unexpected whitespace in message '${path}' at index ${badWhitespace.index} ("${message}")`);
Copy link
Member

@matthew-white matthew-white Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this doesn't fully account for the difference between message and form. As an example, for the pluralized message App User | App Users, the second plural form is the one with multiple spaces. Here, the entire message would be shown (both plural forms), but then the index from the match on only the second form would be used to position the ^^^ and describe the index of the unexpected whitespace.

How about something like:

const error = forms.length === 1
  ? `unexpected white space in message '${path}'`
  : `unexpected white space in plural form of message '${path}'`;
console.error(`${error}:`); // eslint-disable-line no-console
const badLength = badWhitespace[0].length;
console.error(`  [${form}]`); // eslint-disable-line no-console
console.error(`  [${''.padStart(badLength, '^').padStart(badWhitespace.index + badLength, ' ').padEnd(message.length, ' ')}]`); // eslint-disable-line no-console
if (forms.length !== 1)
  console.error(`The entire message is: [${message}]`); // eslint-disable-line no-console
throw new Error(`${error} at index ${badWhitespace.index} ("${form}")`);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

I've updated to consider the index of the form. Some example errors:

single-value

unexpected white space in translation string 'datasets':
  [Data  sets]
  [    ^^    ]
...
Error: unexpected whitespace in message 'datasets' at index 4 ("Data  sets")

singular of multi-value

unexpected white space in translation string 'appUser[0]':
  [App  User]
  [   ^^    ]
...
Error: unexpected whitespace in message 'appUser[0]' at index 3 ("App  User | App  Users")

plural of multi-value

unexpected white space in translation string 'appUser[1]':
  [App  Users]
  [   ^^     ]
...
Error: unexpected whitespace in message 'appUser[1]' at index 3 ("App User | App  Users")

@alxndrsn alxndrsn force-pushed the more-helpful-transifex-output branch from ec25972 to f47d54a Compare September 6, 2023 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants