Install po2json
masterInstall the po2json module via npm to convert PO files to JavaScript objects or JSON strings compatible with Jed.
npm install po2jsonrepository·master·Indexed 41 days ago
https://github.com/mikeedwards/po2jsonA utility and Node.js library for converting Gettext PO files into JSON strings or JavaScript objects. It supports multiple output formats, including raw JSON, Jed (compatible with versions < 1.1.0 and >= 1.1.0), and MessageFormat (mf). The package provides a CLI tool as well as programmatic APIs such as parse(), parseFile(), and parseFileSync() for flexible translation file processing.
Install the po2json module via npm to convert PO files to JavaScript objects or JSON strings compatible with Jed.
npm install po2jsonYou can import po2json into your Node.js project to parse PO data using its API methods.
var po2json = require('po2json');You can use po2json directly from the command line to convert a .po file to a .json file.
Basic usage:
po2json translation.po translation.jsonFor Jed >= 1.1.0 compatibility:
If you are using Jed version 1.1.0 or higher, you must specify the jed1.x format:
po2json translation.po translation.json -f jed1.xYou can use po2json to generate output in the mf format, which is compatible with messageformat.js.
To get the full metadata (like headers and plural functions) required for complex compilation, use { format: 'mf', fullMF: true }.
var po2json = require('po2json'),
MessageFormat = require('messageformat');
// Basic mf usage
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 );
});
// Full mf usage with headers and plural functions
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 );
});To use the output with the jed library, specify the appropriate format based on your Jed version.
{ format: 'jed' }.{ format: 'jedold' }.var po2json = require('po2json'),
Jed = require('jed');
// For Jed >= 1.1.0
po2json.parseFile('messages.po', { format: 'jed' }, function (err, jsonData) {
var i18n = new Jed( jsonData );
});
// For Jed < 1.1.0
po2json.parseFile('messages.po', { format: 'jedold' }, function (err, jsonData) {
var i18n = new Jed( jsonData );
});Parses a PO file provided as a Buffer or a Unicode string into a JSON object or string.
Parameters:
buf: A PO file as a Buffer or a Unicode string.options: An optional configuration object.Options:
fuzzy (boolean): Whether to include fuzzy translations. Defaults to false.stringify (boolean): If true, returns a JSON string. Otherwise returns a plain JavaScript object. Defaults to false.pretty (boolean): If true, the resulting JSON string will be pretty-printed. Has no effect when stringify is false. Defaults to false.format (string): Output format. Defaults to 'raw'.'raw': Produces raw JSON output.'jed': Produces output compatible with Jed >= 1.1.0.'jedold': Produces output compatible with Jed < 1.1.0.'mf': Produces simple key:value output (MessageFormat).domain (string): The domain the messages will be wrapped inside. Only effective if format: 'jed'.fallback-to-msgid (boolean): If true, uses the msgid as the translation for omitted or empty entries. For plural entries, msgid_plural is used for msgstr[1]. This is intended for languages where nplurals=2.var po2json = require('po2json'),
fs = require('fs');
fs.readFile('messages.po', function (err, buffer) {
var jsonData = po2json.parse(buffer);
// do something interesting ...
});A synchronous method to read a PO file from the filesystem and convert it to JSON.
Parameters:
fileName (string): Path to the .po file.options (object): Same options as po2json.parse.Returns: The parsed JSON data.
Note: This method can throw an error if the file cannot be read or parsed; wrap it in a try/catch block.
var po2json = require('po2json');
var jsonData = '';
try {
jsonData = po2json.parseFileSync('messages.po');
// do something interesting ...
} catch (e) {}An asynchronous method to read a PO file from the filesystem and convert it to JSON.
Parameters:
fileName (string): Path to the .po file.options (object): Same options as po2json.parse.cb (function): A callback function receiving (err, jsonData).var po2json = require('po2json');
po2json.parseFile('messages.po', function (err, jsonData) {
// do something interesting ...
});The following flags can be used with the po2json executable to 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)To output in MessageFormat, set format: 'mf'.
If the PO file lacks a Plural-Forms header, you must provide a pluralFunction within mfOptions to prevent errors. If fullMF is set to true, the parser returns the complete object from the underlying gettext-to-messageformat library; otherwise, it returns a simplified object containing only the translations.
const options = {
format: 'mf',
fullMF: false,
mfOptions: {
pluralFunction: () => 0 // Required if Plural-Forms header is missing
}
};
const mfResult = po2json(buffer, options);const options = {
format: 'mf',
fullMF: false,
mfOptions: {
pluralFunction: () => 0
}
};
const mfResult = po2json(buffer, options);To generate a JSON structure compatible with Jed, set format: 'jed'. This wraps the translations in a locale_data object keyed by the domain provided in the options.
const options = {
format: 'jed',
domain: 'messages'
};
const jedResult = po2json(buffer, options);Use parseFileSync to read a PO file from the file system and convert its contents into a JSON object synchronously. This function resolves the real path of the provided fileName before reading. It accepts a file path and an optional options object, which is passed to the underlying parser.
const parseFileSync = require('po2json/lib/parseFileSync');
const translationJson = parseFileSync('path/to/file.po', { /* options */ });