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

po2json

Build Status Dependency Status devDependency Status

Convert PO files to Javascript objects or JSON strings. The result is Jed-compatible.

Getting Started

Install the module with: npm install po2json

As a library

var po2json = require('po2json');

As an executable

po2json translation.po translation.json

If you are using Jed >= 1.1.0, be sure to specify that format specifically.

po2json translation.po translation.json -f jed1.x

Documentation

Methods

po2json has 3 methods, all of which take exactly the same options. The main function is parse which actually does the parsing to JSON. The 2 others - parseFile and parseFileSync are convenience functions to directly read PO data from a file and convert it to JSON.

Parse a PO buffer to JSON

Parse a PO file to JSON

Parse a PO file to JSON (synchronous)

Command Line Arguments

po2json in command-line parametrization support added to allow override default options.

Note: 'format': 'mf' means the json format used by messageFormatter in github.com/SlexAxton/messageformat.js and jedold refers to Jed formats below 1.1.0

Examples

Basic usage with PO data as a buffer/string

var po2json = require('po2json'),
    fs = require('fs');
fs.readFile('messages.po', function (err, buffer) {
  var jsonData = po2json.parse(buffer);
  // do something interesting ...
});

Parse a PO file directly - Asynchronous Usage

var po2json = require('po2json');
po2json.parseFile('messages.po', function (err, jsonData) {
    // do something interesting ...
});

Parse a PO file directly - Synchronous Usage

var po2json = require('po2json');
var jsonData = '';
try {
    jsonData = po2json.parseFileSync('messages.po');
    // do something interesting ...
} catch (e) {}

Parse a PO file to messageformat format

var po2json = require('po2json'),
    MessageFormat = require('messageformat');

po2json.parseFile('es.po', { format: 'mf' }, function (err, translations) {
    var pFunc = function (n) {
      return (n==1 ? 'p0' : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 'p1' : 'p2');
    };
    pFunc.cardinal = [ 'p0', 'p1', 'p2' ];
    var mf = new MessageFormat(
      {
        'es': pFunc
      }
    );
    var i18n = mf.compile( translations );
});

Parse a PO file to messageformat format using the full format

var po2json = require('po2json'),
    MessageFormat = require('messageformat');

po2json.parseFile('messages.po', { format: 'mf', fullMF: true }, function (err, jsonData) {
    var mf = new MessageFormat(
        { [jsonData.headers.language]: jsonData.pluralFunction }
    );
    var i18n = mf.compile( jsonData.translations );
});

Parse a PO file to Jed >= 1.1.0 format

var po2json = require('po2json'),
    Jed = require('jed');
po2json.parseFile('messages.po', { format: 'jed' }, function (err, jsonData) {
    var i18n = new Jed( jsonData );
});

Parse a PO file to Jed < 1.1.0 format

If you are using an older version of Jed, be sure to specify this format specifically.

var po2json = require('po2json'),
    Jed = require('jed');
po2json.parseFile('messages.po', { format: 'jedold' }, function (err, jsonData) {
    var i18n = new Jed( jsonData );
});

Running tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.

Release History

1.0.0 / 2018-09-24

0.4.6 / 2018-09-24

0.4.5 / 2016-10-13

0.4.4 / 2016-08-29

0.4.3 / 2016-08-27

0.4.2 / 2015-04-13

0.4.1 / 2015-03-01

0.4.0 / 2015-03-01

0.3.0 / 2014-07-16

0.2.4 / 2014-07-15

0.2.3 / 2014-01-26

0.2.0 / 2013-11-08

NB! This release is NOT backwards-compatible! It has the following breaking changes:

Other changes in this release:

0.0.7 / 2012-10-26

0.0.6 / 2012-10-22

0.0.5 / 2012-10-19

0.0.4 / 2012-09-18

0.0.2 / 2012-07-03

0.0.1 / 2012-06-06

License

Copyright (c) 2012 Joshua I. Miller Licensed under the GNU, Library, General, Public, License licenses.