avast / vuei18n-po

transform gettext .po files for vue-i18n
BSD 3-Clause "New" or "Revised" License
14 stars 5 forks source link

Error: Cannot parse plural definition #8

Closed Heziode closed 11 months ago

Heziode commented 1 year ago

I got the following error using this command:

vuei18n-po --messagesDir=lang "translations/*.po"
fname:  /Users/heziode/Library/pnpm/global/5/node_modules/vuei18n-po/cli.js
(node:5976) UnhandledPromiseRejectionWarning: Error: Cannot parse plural definition: 
    at parsePlurals (/Users/heziode/Library/pnpm/global/5/.pnpm/vuei18n-po@1.3.0/node_modules/vuei18n-po/index.js:145:11)
    at /Users/heziode/Library/pnpm/global/5/.pnpm/vuei18n-po@1.3.0/node_modules/vuei18n-po/index.js:129:18
    at /Users/heziode/Library/pnpm/global/5/.pnpm/pofile@1.1.4/node_modules/pofile/lib/po.js:71:9
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
(node:5976) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5976) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
fname:  translations/en.po
fname:  translations/fr.po

As we can see, the first file parsed is /Users/heziode/Library/pnpm/global/5/node_modules/vuei18n-po/cli.js, and I don't know why…

I think the following line should add a filter for PO files:

https://github.com/avast/vuei18n-po/blob/3dc265e5a8448290f52216396bf3544521616ffa/index.js#L117

 return Promise.all(fileNames.filter(fname => fname.endsWith(".po")).map(fname => new Promise((resolve, reject) => { 
pavelstudeny commented 1 year ago

Hi, the filter should not be necessary - that's why there is the "translations/*.po" argument.

I can't see right away why .../Library/pnpm/global/5/node_modules/vuei18n-po/cli.js would match "translations/*.po".

When invoking vuei18n-po --messagesDir=lang "translations/*.po", what is your current directory and where are the translations/*.po located?

Heziode commented 1 year ago

I can't see right away why .../Library/pnpm/global/5/node_modules/vuei18n-po/cli.js would match "translations/*.po".

In fact it does not mach the pattern, but it seems to be passed as arguments or something like that using node via PNPM.

what is your current directory and where are the translations/*.po located?

The current directory is my project dir, and *.po files are all located in translations, like translations/en.po, etc.

pavelstudeny commented 1 year ago

it's possible that pnpm passes the arguments slightly differently. in my case, in cli.js:3, process.argv is

[
  ".../bin/node",
  ".../vuei18n-po/cli.js",
  "--messagesDir=lang",
  "translations/*.po",
]

the parsed argv

{
  _: [
    '/usr/local/bin/node',
    '/Users/pavelstudeny/src/avast/vuei18n-po/cli.js',
    'translations/*.po'
  ],
  messagesDir: 'lang'
}

and then, at cli.js:65, the processed argv would look like

{ messagesDir: 'lang', po: [ 'translations/*.po' ] }

How is it for you?

pavelstudeny commented 1 year ago

Looks like you have been able to resolve this on your side, @Heziode ?

Heziode commented 1 year ago

Sorry for the delay @pavelstudeny.

I modified the begining of cli.js:

#!/usr/bin/env node
let argv = require('minimist')(process.argv);
const chokidar = require('chokidar');

console.log("argv: ", argv);
// rest of the code

And here is my result:

argv:  {
  _: [
    '/Users/heziode/Library/pnpm/nodejs/12.22.12/bin/node',
    '/Users/heziode/Library/pnpm/global/5/node_modules/vuei18n-po/cli.js',
    'translations/ar.po',
    'translations/cs.po',
    'translations/da.po',
    'translations/de.po',
    'translations/el.po',
    'translations/en.po',
    'translations/es.po',
    'translations/fi.po',
    'translations/fr.po',
    'translations/hu.po',
    'translations/it.po',
    'translations/nl.po',
    'translations/no.po',
    'translations/pl.po',
    'translations/pt-BR.po',
    'translations/pt.po',
    'translations/ro.po',
    'translations/ru.po',
    'translations/sk.po',
    'translations/sv.po',
    'translations/th.po',
    'translations/tr.po'
  ],
  messagesDir: 'lang'
}

And line 65 of cli.js I got this:

argv:  {
  messagesDir: 'lang',
  po: [
    '/Users/heziode/Library/pnpm/nodejs/12.22.12/bin/node',
    '/Users/heziode/Library/pnpm/global/5/node_modules/vuei18n-po/cli.js',
    'translations/ar.po',
    'translations/cs.po',
    'translations/da.po',
    'translations/de.po',
    'translations/el.po',
    'translations/en.po',
    'translations/es.po',
    'translations/fi.po',
    'translations/fr.po',
    'translations/hu.po',
    'translations/it.po',
    'translations/nl.po',
    'translations/no.po',
    'translations/pl.po',
    'translations/pt-BR.po',
    'translations/pt.po',
    'translations/ro.po',
    'translations/ru.po',
    'translations/sk.po',
    'translations/sv.po',
    'translations/th.po',
    'translations/tr.po'
  ]
}

So, to sumup, this result is obtained with the following environment:

My current workaround, it to add a filter for .po files (as describe in the first message). But maybe it is more relevant to add this filter in cli.js inside argv._ processing (line 5-17).

With something like:

if (argv._) {
  console.log("__filename: ", __filename);
  let myself = argv._.indexOf(__filename);
  if (myself == -1) {
    myself = argv._.findIndex(a => a.indexOf('.bin/vuei18n-po') != -1);
  }
  if (myself != -1 && myself + 1 < argv._.length) {
    argv.po = argv._.slice(myself + 1);
  }
  else if (myself == -1 && argv._.length > 0) {
    argv.po = argv._.slice();
  }
+  argv.po = argv.po.filter(fname => fname.endsWith(".po"));
}
pavelstudeny commented 11 months ago

Since this is specific to pnpm and there is an easy workaround, I'm closing this issue. If there were more reports or the workaround was causing undesired behavior, I will reopen and address this.