mikeedwards / po2json

Pure Javascript implementation of Uniforum message translation. Based on a great gist.
https://gist.github.com/1739769
Other
178 stars 62 forks source link

Why is null added as the first entry in the translations? #71

Closed sabsaxo closed 6 years ago

sabsaxo commented 6 years ago

When using po2json as opposed to gettext-parser, a null is added as the first entry in the translation array: why is that?

And will it still work as expected?

Example: "Login": [null, "Log ind"]

mikeedwards commented 6 years ago

Hmm, what kind of output is that? There are a couple versions of Jed available, as well as MF (messageFormatter) and "raw". As I recall, one of the versions of Jed has the null, but that changed in later versions. My memory is a little fuzzy on that, TBH.

sabsaxo commented 6 years ago

I'm not sure, as I was experimenting pretty heavily with multiple po to json converters ... but I ended up adding this format : 'mf', and it's now clean. Thanks.

kenjiuno commented 5 years ago

I have same problem.

Here is repro:

test.po

# English translations for PACKAGE package.
# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# Automatically generated, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-11-09 09:13+0900\n"
"PO-Revision-Date: 2018-11-09 09:57+0900\n"
"Last-Translator: Automatically generated\n"
"Language-Team: none\n"
"Language: en_US\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.8\n"

#: ../es2015/HeaderGUI.js:60
#, javascript-format
msgid "%d 秒"
msgstr "%d seconds"

conversion:

.\node_modules\.bin\po2json -p test.po test.json

test.json

{
   "": {
      "project-id-version": "",
      "report-msgid-bugs-to": "",
      "pot-creation-date": "2018-11-09 09:13+0900",
      "po-revision-date": "2018-11-09 09:57+0900",
      "last-translator": "Automatically generated",
      "language-team": "none",
      "language": "en_US",
      "mime-version": "1.0",
      "content-type": "text/plain; charset=UTF-8",
      "content-transfer-encoding": "8bit",
      "x-generator": "Poedit 2.0.8"
   },
   "%d 秒": [
      null,
      "%d seconds"
   ]
}

I have noticed this because gettext doesn't convert it.

i18n.gettext("%d 秒")
"%d 秒"

However ngettext n=1 works.

i18n.ngettext("%d 秒", 1)
"%d seconds"
mikeedwards commented 5 years ago

Hmm, ok. Is there a particular format you're trying to output for @kenjiuno ? At this point, if you don't have a preference, I'd recommend either the latest version of Jed or mf. That is, invoke it either with:

.\node_modules\.bin\po2json -f jed -p test.po test.json

or

.\node_modules\.bin\po2json -f mf -p test.po test.json

The default raw version (what you get without specifying a format) can produce weird result like with the example you have there, and the old Jed format has similar issues. If what you're working on isn't specifically taking one output format over the other, I'd recommend mf, since it's the latest format and it still getting development (as opposed to Jed, which may be largely abandoned by now.)

Hope that helps.

kenjiuno commented 5 years ago

@mikeedwards Thanks for reply! I'm using output json with gettext.js.

The default raw version (what you get without specifying a format) can produce weird result like with the example you have there, and the old Jed format has similar issues. If what you're working on isn't specifically taking one output format over the other, I'd recommend mf, since it's the latest format and it still getting development (as opposed to Jed, which may be largely abandoned by now.)

OK, finally I will use mf format because it is working well! po2json is introduced at Required JSON format section. I found out about Po2json there.

The following part is just FYI:

With -f mf option...

po-en.json

{
   "%d 秒": "%d seconds"
}

loader (ddgettext.js)

import gettextJs from 'gettext.js'
import en from './po-en.json'

export var i18n = gettextJs.i18n();
i18n.setMessages("messages", "en_US", en);

consumer:

import { i18n } from './ddgettext'
...
secsLeftTimeo.text(sprintf(i18n.gettext('%d 秒'), secs));
chrisnicola commented 5 years ago

@mikeedwards I'm seeing this behaviour (adding a null entry for any singular translation) for raw, jed and jed1.x. Is this expected? I mean I can strip out the null entries but that doesn't seem right to me.

chrisnicola commented 5 years ago

Sorry I spoke too soon. Only 'raw' and 'jed1.x' have the extra null entries for singular translations. The 'jed' format still uses an array for singular translations though. In all cases the output is not compatible with gettext.js: https://www.npmjs.com/package/gettext.js#required-json-format

mikeedwards commented 5 years ago

That makes sense. How does the 'mf' format do for you, though? That's the format that's been getting more development and support in the past few years. Jed is close to obsolete, although it's still in use so I keep its options. If mf doesn't work, open up a new issue and I'll start tracking it from there.

chrisnicola commented 5 years ago

@mikeedwards 'mf' is also not compatible with gettext.js, but I'm considering making the move to the message format library in the next version of our i18n library, so that might do the trick.

For now I'm adding a filter after loading to strip the null entries out.

Makes sense.

firestar300 commented 4 years ago

Using the latest 1.0.0-beta-2 works for me instead of the version 0.4.5.