smhg / gettext-parser

Parse and compile gettext po and mo files, nothing more, nothing less
MIT License
158 stars 44 forks source link

What kind of 'compile' to use? #36

Closed sabsaxo closed 6 years ago

sabsaxo commented 6 years ago

I can only produce output (in written file) like:

[object Object]

from an ajax response.

Doing this:

let po = gettextParser.po.parse( response );

and

let po = gettextParser.po.compile( response ); // This fails with:

TypeError: Cannot create property 'headers' on string 'msgid ""

I go about it like this:

xhr('https://localise.biz/api/export/locale/es.po?filter=login&format=script&key=ZnD4KUNjK5uV-fH8CN8rRYlEGGzMak-7S', {
    method: 'GET',
    name: login
}, function ( err, response ) {
    if ( err ) {
        throw err;
    }

    let po = gettextParser.po.parse( response );

    fs.writeFileSync('es_trans.json', po.translations[''], 'utf8', ( err ) => {
        console.log("File error:", err);
    }, 'utf-8');
});

Basically trying to read the response from the server, and write it to a json file.

What am I missing?

sabsaxo commented 6 years ago

Response from server:

msgid ""
msgstr ""
"Project-Id-Version: Publish Frontend\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-08-22 12:32+0100\n"
"PO-Revision-Date: 2017-11-28 09:09+0000\n"
"Last-Translator: Sam Blåsvær\n"
"Language-Team: Danish\n"
"Language: da\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Loco https://localise.biz\n"
"X-Loco-Source-Locale: en\n"
"X-Loco-Project-Id: 17418\n"
"X-Loco-Tagged: login\n"
"X-Loco-Api-Version: 1.0.18 20171124-1"

#. Login form title
#: ../../publish/node_modules/login/index.js:275
#: ../../publish/node_modules/login/index.js:282 loco:59f9d143a39ff3d3488b456b
msgid "Login"
msgstr "Log ind"

#: ../../publish/node_modules/login/index.js:280 loco:59f9d143a39ff3d3488b456c
msgid "Password"
msgstr "Kodeord"

#. Register form title
#. Registration button
#: ../../publish/node_modules/login/index.js:283 loco:59f9d143a39ff3d3488b456e
msgid "Register"
msgstr "Registrer"

#: ../../publish/node_modules/login/index.js:49 loco:5a1d24e4c430fe6c288b4568
msgid "Wrong e-mail or password"
msgstr "Forkert e-mail eller password"

After running: let po = gettextParser.po.parse(response);

and fs.writeFileSync('es_trans.json', po.translations[''], 'utf8', ( err ) => { … }, 'utf-8');

or: fs.writeFileSync('es_trans.json', po, 'utf8', ( err ) => { … }, 'utf-8');

PO:

{
    charset: 'utf-8',
    headers: 
    { 'project-id-version': 'Publish Frontend',
        'report-msgid-bugs-to': '',
        'pot-creation-date': '2016-08-22 12:32+0100',
        'po-revision-date': '2017-11-28 09:09+0000',
        'last-translator': 'Sam Blåsvær',
        'language-team': 'Danish',
        language: 'da',
        'plural-forms': 'nplurals=2; plural=n != 1;',
        'mime-version': '1.0',
        'content-type': 'text/plain; charset=UTF-8',
        'content-transfer-encoding': '8bit',
        'x-generator': 'Loco https://localise.biz',
        'x-loco-source-locale': 'en',
        'x-loco-project-id': '17418',
        'x-loco-tagged': 'login',
        'x-loco-api-version': '1.0.18 20171124-1'
    },
    translations: 
    { '': 
        { '': [Object],
            Login: [Object],
            Password: [Object],
            Register: [Object],
            'Wrong e-mail or password': [Object]
        }
    }
}

Somehow something becomes [Object]?

Any (complete) examples of reading from an AJAX response and compiling to JSON file in some format that can be read by node-gettext?

smhg commented 6 years ago

@sabsaxo there are 2 misconceptions in your report:

After these considerations, your code works fine. Basic debugging would probably have led you to this conclusion regarding at least the second misconception. I'd urge you to read up on this.