Convert PO files to Javascript objects or JSON strings. The result is Jed-compatible.
Install the module with: npm install po2json
var po2json = require('po2json');
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
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
po2json.parse(buf[, options])
buf
- a po file as a Buffer or an unicode string.options
- an optional object with the following possible parameters:fuzzy
Whether to include fuzzy translation in JSON or not. Should be eithertrue
orfalse
. Defaults tofalse
.stringify
Iftrue
, returns a JSON string. Otherwise returns a plain Javascript object. Defaults tofalse
.pretty
Iftrue
, the resulting JSON string will be pretty-printed. Has no effect whenstringify
isfalse
. Defaults tofalse
format
Defaults toraw
.raw
produces a "raw" JSON outputjed
produces an output that is 100% compatible with Jed >= 1.1.0jedold
produces an output that is 100% compatible with Jed < 1.1.0mf
produces simple key:value output.
domain
- the domain the messages will be wrapped inside. Only has effect ifformat: 'jed'
.fallback-to-msgid
Iftrue
, for those entries that would be omitted (fuzzy entries without the fuzzy flag) and for those that are empty, the msgid will be used as translation in the json file. If the entry is plural, msgid_plural will be used for msgstr[1]. This means that this option makes sense only for those languages that have nplurals=2.
Parse a PO file to JSON
po2json.parseFile(fileName[,options], cb)
fileName
- path to the po fileoptions
- same as forpo2json.parse
cb
- a function that receives 2 arguments:err
andjsonData
Parse a PO file to JSON (synchronous)
po2json.parseFileSync(fileName[, options])
fileName
- path to the po fileoptions
- same as forpo2json.parse
po2json in command-line parametrization support added to allow override default options.
- --pretty, -p: same as pretty = true in function options
- --fuzzy, -F: same as fuzzy = true in function options
- --format, -f: Output format (raw, jed, jedold, or mf)
- --full-mf, -M: return full messageformat output (instead of only translations)
- --domain, -d: same as domain in function options
- --fallback-to-msgid': 'use msgid if translation is missing (nplurals must match)
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
var po2json = require('po2json'),
fs = require('fs');
fs.readFile('messages.po', function (err, buffer) {
var jsonData = po2json.parse(buffer);
// do something interesting ...
});
var po2json = require('po2json');
po2json.parseFile('messages.po', function (err, jsonData) {
// do something interesting ...
});
var po2json = require('po2json');
var jsonData = '';
try {
jsonData = po2json.parseFileSync('messages.po');
// do something interesting ...
} catch (e) {}
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 );
});
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 );
});
var po2json = require('po2json'),
Jed = require('jed');
po2json.parseFile('messages.po', { format: 'jed' }, function (err, jsonData) {
var i18n = new Jed( jsonData );
});
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 );
});
npm test
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.
- Updated dependencies.
- Replaced nomnom with commander.
- Switched formats values for Jed versions.
jed
now refers to versions >= 1.1.0. - Added tests for MessageFormat compilation.
- Add documentation and tests for different Jed versions.
- Updated dependencies.
- Fixed issue with jed 1.x and ja-JP pluralization.
- Fix parsing of .po files with no headers.
- Better support for plurals in languages like Japanese.
- Updated documentation for Jed > 1.1.0
- Use msgid_plural when there is no translation
- Updated Jed-format code and test to deal with the new plural form
- Added Jed > 1.1.0 compatible format (Evan Moses)
- Added command line flags for fuzzy, pretty, format, and domain (Szigetvári Áron)
- Deals with fallback-to-msgid for fuzzy entries without the fuzzy flag (Szigetvári Áron)
- Fixed fuzzy flag (mahata)
- Raised minimum node version requirement to 0.8
- Raised lodash version to ~2.4.1
- Clean up documentations
NB! This release is NOT backwards-compatible! It has the following breaking changes:
po2json.parse_po
has been replaced withpo2json.parse
po2json.parse
has been replaced withpo2json.parseFile
po2json.parseSync
has been replaced withpo2json.parseFileSync
Other changes in this release:
- The library has been competely rewritten, it now uses the gettext-parser module to parse PO files. (Illimar Tambek)
- Tests have been completely rewritten (Illimar Tambek)
- Fixed issue with double-escaping quotes (Illimar Tambek)
- Option to skip/include fuzzy translations (Illimar Tambek)
- Fixed linting bugs and added a better fr.po fixture (Mike Edwards)
- Add tests for po2json.parse and po2json.parseSync (Dan MacTough)
- updated README.md with version history (Mike Edwards)
- updated history (Mike Edwards)
- Add AUTHORS to identify contributors (Dan MacTough)
- Update README with revision history and basic examples (Dan MacTough)
- cut out fake README example from grunt boilerplate (Mike Edwards)
- fixed README.md markdown (Mike Edwards)
- fixes tests (Mike Edwards)
- added first test for parse_po (Mike Edwards)
- Added boilerplate using grunt init (Mike Edwards)
- Changed exports.parse to use node's convetional error-first callback style. Added exports.parseSync for synchronous parsing. (Dan MacTough)
- Properly escape linebreaks (Zach Carter)
- Update package.json (Mike Edwards)
- package.json: define main module (Asbjørn Sloth Tønnesen)
- fix package, fix pretty print return, remove debug logs (gilles)
- upped version (Mike Edwards)
- Added build status to README (Mike Edwards)
- Removed built=ints from the dependencies (Mike Edwards)
- Added a .travis file for continuous integration (Mike Edwards)
- Added usage note to README.md (Mike Edwards)
- First working script! (Mike Edwards)
- Added new git repo (Mike Edwards)
- initial commit (Mike Edwards)
- Initial commit (Mike Edwards)
Copyright (c) 2012 Joshua I. Miller Licensed under the GNU, Library, General, Public, License licenses.