### Install po2json Source: https://github.com/mikeedwards/po2json/blob/master/README.md Install the module using npm. ```bash npm install po2json ``` -------------------------------- ### Example Usage: Parse with Options Source: https://context7.com/mikeedwards/po2json/llms.txt Demonstrates how to use the parse method with a specific set of options. ```APIDOC ## Example Usage: Parse with Options ### Description This example shows how to parse a PO buffer using the `po2json.parse` method with a custom options object. ### Method POST ### Endpoint `/api/parse` (Conceptual - actual usage is via library function) ### Request Body ```json { "poBuffer": "PO file content as string or buffer", "options": { "format": "jed", "domain": "myapp", "fuzzy": true, "stringify": true, "pretty": true } } ``` ### Request Example ```javascript const po2json = require('po2json'); const poBuffer = '...'; // Your PO file content const result = po2json.parse(poBuffer, { format: 'jed', domain: 'myapp', fuzzy: true, stringify: true, pretty: true }); ``` ### Response #### Success Response (200) - **result** (string|object) - The parsed JSON output, either as a string or an object depending on the `stringify` option. #### Response Example ```json { "domain": "myapp", "locale_data": { "myapp": { "": { "domain": "myapp", "plural_forms": "...", "lang": "es" }, "msgid": ["translation"] } } } ``` ``` -------------------------------- ### Parse PO file to MessageFormat format Source: https://github.com/mikeedwards/po2json/blob/master/README.md Parse a PO file and convert translations to the MessageFormat format using `po2json.parseFile` with the 'mf' format option. This example also demonstrates integrating with the `messageformat` library. ```javascript 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 PO Data with Custom Options Source: https://context7.com/mikeedwards/po2json/llms.txt Use the 'po2json.parse' method with a buffer containing PO data and a custom options object to control the parsing and output. This example demonstrates setting format, domain, fuzzy, stringify, and pretty options. ```javascript // Example: Parse with all options const result = po2json.parse(poBuffer, { format: 'jed', domain: 'myapp', fuzzy: true, stringify: true, pretty: true }); ``` -------------------------------- ### Parse PO buffer/string to JSON Source: https://github.com/mikeedwards/po2json/blob/master/README.md Use the `parse` method to convert PO data from a buffer or string into a Javascript object. This example reads from a file asynchronously. ```javascript var po2json = require('po2json'), fs = require('fs'); fs.readFile('messages.po', function (err, buffer) { var jsonData = po2json.parse(buffer); // do something interesting ... }); ``` -------------------------------- ### Run project tests Source: https://github.com/mikeedwards/po2json/blob/master/README.md Executes the test suite for the project. ```bash npm test ``` -------------------------------- ### Configure po2json Parsing Options Source: https://context7.com/mikeedwards/po2json/llms.txt Define the 'options' object to customize parsing behavior and output format. Options like 'format', 'domain', 'fuzzy', 'stringify', 'pretty', and 'fallback-to-msgid' are available. The 'mfOptions' can be used for custom MessageFormat replacements. ```javascript const po2json = require('po2json'); // Complete options reference const options = { // Output format: 'raw', 'jed', 'jedold', or 'mf' // - 'raw': Default format with msgid as key, [plural, ...translations] as value // - 'jed': Jed >= 1.1.0 compatible format // - 'jedold': Jed < 1.1.0 compatible format // - 'mf': MessageFormat compatible format format: 'jed', // Domain name for Jed format (default: 'messages') domain: 'messages', // Include fuzzy translations (default: false) fuzzy: false, // Return JSON string instead of object (default: false) stringify: false, // Pretty print JSON when stringify is true (default: false) pretty: false, // Use msgid as fallback when translation is empty (default: false) 'fallback-to-msgid': false, // Return full MessageFormat output with headers and pluralFunction (default: false) // Only applies when format is 'mf' fullMF: false, // Custom options passed to gettext-to-messageformat (for 'mf' format) mfOptions: { replacements: [ // Custom replacement patterns for MessageFormat conversion { pattern: /%(\d+)(?:\$\w)?/g, replacement: (_, n) => `{${n - 1}}` } ] } }; ``` -------------------------------- ### po2json CLI with Pretty Printing Source: https://context7.com/mikeedwards/po2json/llms.txt Use the -p flag for pretty-printed JSON output, enhancing readability. ```bash po2json -p translation.po translation.json ``` -------------------------------- ### Command Line Interface Source: https://github.com/mikeedwards/po2json/blob/master/README.md po2json can be used as a command-line tool to convert PO files to JSON. Various options can be passed as arguments to customize the output. ```APIDOC ## po2json [input.po] [output.json] [options] ### Description Converts a PO file to a JSON file using the command line. ### Method Command Line Execution ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Command Line Arguments - `--pretty`, `-p`: Enable pretty-printing for the JSON output. - `--fuzzy`, `-F`: Include fuzzy translations in the output. - `--format`, `-f`: Specify the output format (`raw`, `jed`, `jedold`, `mf`). - `--full-mf`, `-M`: Return full messageformat output (instead of only translations). - `--domain`, `-d`: Set the domain for Jed format output. - `--fallback-to-msgid`: Use `msgid` if translation is missing (requires `nplurals` match). ### Request Example ```bash po2json translation.po translation.json -f jed1.x ``` ### Response #### Success Response (200) - **output.json** (file) - The converted JSON file. #### Response Example None ``` -------------------------------- ### po2json CLI for MessageFormat Source: https://context7.com/mikeedwards/po2json/llms.txt Convert PO files to MessageFormat using the -f mf flag. Use -M for full output including headers and plural function. ```bash po2json -f mf translation.po translation.json ``` ```bash po2json -f mf -M translation.po translation.json ``` -------------------------------- ### po2json CLI Combined Options Source: https://context7.com/mikeedwards/po2json/llms.txt Combine multiple CLI options for advanced conversion scenarios. ```bash po2json -p -F -f jed -d app locales/es.po dist/es.json ``` -------------------------------- ### Full MessageFormat Output Structure Source: https://context7.com/mikeedwards/po2json/llms.txt Illustrates the structure of the output when the 'mf' format and 'fullMF' options are used. ```APIDOC ## Full MessageFormat Output Structure ### Description This example shows the structure of the output when the `format` option is set to `'mf'` and `fullMF` is set to `true`. ### Method POST ### Endpoint `/api/parse/fullmf` (Conceptual - actual usage is via library function) ### Request Body ```json { "poBuffer": "PO file content as string or buffer", "options": { "format": "mf", "fullMF": true } } ``` ### Request Example ```javascript const po2json = require('po2json'); const poBuffer = '...'; // Your PO file content const fullMfOutput = po2json.parse(poBuffer, { format: 'mf', fullMF: true }); ``` ### Response #### Success Response (200) - **output** (object) - The parsed JSON object including headers and plural function. #### Response Example ```json { "headers": { "language": "es", ... }, "translations": { "": { ... }, "context": { ... } }, "pluralFunction": "function(n) { ... }" } ``` ``` -------------------------------- ### Command Line Interface (CLI) Source: https://context7.com/mikeedwards/po2json/llms.txt The po2json CLI provides a command-line interface for converting PO files to JSON without writing code. It supports all major options through command-line flags, making it suitable for build scripts, CI/CD pipelines, and quick one-off conversions. ```APIDOC ### po2json CLI ### Description Provides a command-line interface for converting PO files to JSON. Supports various options via flags for build scripts, CI/CD, and quick conversions. ### Usage ```bash po2json [options] ``` ### Options - **-p, --pretty** : Pretty print the output JSON. - **-f, --format** (jed | jedold | mf) : Specify the output format. Defaults to 'jed'. - **-M, --fullMF** : When format is 'mf', include headers and plural function in the output. - **-F, --fuzzy** : Include fuzzy translations in the output. - **-d, --domain** (string) : Specify a custom domain for the Jed format. - **--fallback-to-msgid** : Use msgid as fallback when translation is missing. - **-h, --help** : Display help message. - **-v, --version** : Output the version number. ### Examples ```bash # Basic conversion - raw format po2json translation.po translation.json # Convert with pretty printing po2json -p translation.po translation.json # Convert to Jed >= 1.1.0 format (recommended for Jed) po2json -f jed translation.po translation.json # Convert to Jed < 1.1.0 format (legacy) po2json -f jedold translation.po translation.json # Convert to MessageFormat po2json -f mf translation.po translation.json # Full MessageFormat output with headers and plural function po2json -f mf -M translation.po translation.json # Include fuzzy translations po2json -F translation.po translation.json # Specify custom domain for Jed format po2json -f jed -d mydomain translation.po translation.json # Fallback to msgid when translation is missing po2json --fallback-to-msgid translation.po translation.json # Combine multiple options po2json -p -F -f jed -d app locales/es.po dist/es.json # Use in npm scripts (package.json) # { # "scripts": { # "build:i18n": "po2json -f jed locales/en.po dist/en.json && po2json -f jed locales/es.po dist/es.json" # } # } # Use in shell script for batch conversion #!/bin/bash for file in locales/*.po; do basename="${file%.po}" po2json -f jed -p "$file" "dist/${basename##*/}.json" done ``` ``` -------------------------------- ### Raw Format Output Structure Source: https://context7.com/mikeedwards/po2json/llms.txt Illustrates the structure of the output when the 'raw' format option is used. ```APIDOC ## Raw Format Output Structure ### Description This example shows the structure of the output when the `format` option is set to `'raw'`. ### Method POST ### Endpoint `/api/parse/raw` (Conceptual - actual usage is via library function) ### Request Body ```json { "poBuffer": "PO file content as string or buffer", "options": { "format": "raw" } } ``` ### Request Example ```javascript const po2json = require('po2json'); const poBuffer = '...'; // Your PO file content const rawOutput = po2json.parse(poBuffer, { format: 'raw' }); ``` ### Response #### Success Response (200) - **output** (object) - The parsed JSON object in raw format. #### Response Example ```json { "": { "headers": { ... } }, "msgid": [null, "translation"], // singular "msgid": ["plural", "sing", "plur1", ...] // plural } ``` ``` -------------------------------- ### po2json CLI for Jed Format Source: https://context7.com/mikeedwards/po2json/llms.txt Convert PO files to Jed format using the -f jed flag. Supports versions >= 1.1.0 (recommended) and legacy < 1.1.0 (jedold). ```bash po2json -f jed translation.po translation.json ``` ```bash po2json -f jedold translation.po translation.json ``` -------------------------------- ### MessageFormat Output Structure Source: https://context7.com/mikeedwards/po2json/llms.txt Illustrates the structure of the output when the 'mf' format option is used. ```APIDOC ## MessageFormat Output Structure ### Description This example shows the structure of the output when the `format` option is set to `'mf'`. ### Method POST ### Endpoint `/api/parse/mf` (Conceptual - actual usage is via library function) ### Request Body ```json { "poBuffer": "PO file content as string or buffer", "options": { "format": "mf" } } ``` ### Request Example ```javascript const po2json = require('po2json'); const poBuffer = '...'; // Your PO file content const mfOutput = po2json.parse(poBuffer, { format: 'mf' }); ``` ### Response #### Success Response (200) - **output** (object) - The parsed JSON object in MessageFormat compatible structure. #### Response Example ```json { "msgid": "translation", "msgid": "{0, plural, one{singular} other{{0} plural}}" } ``` ``` -------------------------------- ### po2json CLI in npm Scripts Source: https://context7.com/mikeedwards/po2json/llms.txt Integrate po2json into npm scripts for automated i18n file generation during the build process. ```json { "scripts": { "build:i18n": "po2json -f jed locales/en.po dist/en.json && po2json -f jed locales/es.po dist/es.json" } } ``` -------------------------------- ### po2json CLI Basic Conversion Source: https://context7.com/mikeedwards/po2json/llms.txt Perform basic PO to JSON conversion directly from the command line. Specify input and output file paths. ```bash po2json translation.po translation.json ``` -------------------------------- ### po2json CLI in Shell Script for Batch Conversion Source: https://context7.com/mikeedwards/po2json/llms.txt Automate the conversion of multiple PO files to JSON using a shell script loop. ```bash #!/bin/bash for file in locales/*.po; do basename="${file%.po}" po2json -f jed -p "$file" "dist/${basename##*/}.json" done ``` -------------------------------- ### Jed Format Output Structure Source: https://context7.com/mikeedwards/po2json/llms.txt Illustrates the structure of the output when the 'jed' format option is used. ```APIDOC ## Jed Format Output Structure ### Description This example shows the structure of the output when the `format` option is set to `'jed'`. ### Method POST ### Endpoint `/api/parse/jed` (Conceptual - actual usage is via library function) ### Request Body ```json { "poBuffer": "PO file content as string or buffer", "options": { "format": "jed", "domain": "app" } } ``` ### Request Example ```javascript const po2json = require('po2json'); const poBuffer = '...'; // Your PO file content const jedOutput = po2json.parse(poBuffer, { format: 'jed', domain: 'app' }); ``` ### Response #### Success Response (200) - **output** (object) - The parsed JSON object in Jed format. #### Response Example ```json { "domain": "app", "locale_data": { "app": { "": { "domain": "app", "plural_forms": "...", "lang": "es" }, "msgid": ["translation"], "msgid": ["singular", "plural1", "plural2"] } } } ``` ``` -------------------------------- ### po2json CLI Fallback to msgid Source: https://context7.com/mikeedwards/po2json/llms.txt Enable fallback to msgid when a translation is missing by using the --fallback-to-msgid flag. ```bash po2json --fallback-to-msgid translation.po translation.json ``` -------------------------------- ### po2json.parseFile(fileName, options, callback) Source: https://context7.com/mikeedwards/po2json/llms.txt Asynchronously reads and parses a PO file from the file system. ```APIDOC ## po2json.parseFile(fileName, options, callback) ### Description Asynchronously reads and parses a PO file to JSON. This method combines file reading with parsing, returning the result through a callback. ### Parameters #### Request Body - **fileName** (String) - Required - The path to the PO file. - **options** (Object) - Optional - Configuration options for parsing. - **callback** (Function) - Required - A function to handle the result, receiving (err, jsonData). ### Response #### Success Response (200) - **jsonData** (Object) - The parsed translation data. ``` -------------------------------- ### po2json CLI Custom Domain for Jed Source: https://context7.com/mikeedwards/po2json/llms.txt Specify a custom domain for Jed format output using the -d flag. ```bash po2json -f jed -d mydomain translation.po translation.json ``` -------------------------------- ### PO to JSON Parsing Options Source: https://context7.com/mikeedwards/po2json/llms.txt The options object controls parsing behavior and output format. These options are shared across all parsing methods (parse, parseFile, parseFileSync) and most are also available as CLI flags. ```APIDOC ## PO to JSON Parsing Options ### Description The options object controls parsing behavior and output format. These options are shared across all parsing methods (parse, parseFile, parseFileSync) and most are also available as CLI flags. ### Parameters #### Request Body - **format** (string) - Optional - Output format: 'raw', 'jed', 'jedold', or 'mf'. - 'raw': Default format with msgid as key, [plural, ...translations] as value. - 'jed': Jed >= 1.1.0 compatible format. - 'jedold': Jed < 1.1.0 compatible format. - 'mf': MessageFormat compatible format. - **domain** (string) - Optional - Domain name for Jed format (default: 'messages'). - **fuzzy** (boolean) - Optional - Include fuzzy translations (default: false). - **stringify** (boolean) - Optional - Return JSON string instead of object (default: false). - **pretty** (boolean) - Optional - Pretty print JSON when stringify is true (default: false). - **fallback-to-msgid** (boolean) - Optional - Use msgid as fallback when translation is empty (default: false). - **fullMF** (boolean) - Optional - Return full MessageFormat output with headers and pluralFunction (default: false). Only applies when format is 'mf'. - **mfOptions** (object) - Optional - Custom options passed to gettext-to-messageformat (for 'mf' format). - **replacements** (array) - Optional - Custom replacement patterns for MessageFormat conversion. - **pattern** (RegExp) - The pattern to match. - **replacement** (string|function) - The replacement string or function. ### Request Example ```javascript const options = { format: 'jed', domain: 'messages', fuzzy: false, stringify: false, pretty: false, 'fallback-to-msgid': false, fullMF: false, mfOptions: { replacements: [ { pattern: /%(\d+)(?:\$\w)?/g, replacement: (_, n) => `{${n - 1}}` } ] } }; ``` ### Response Example ```json { "msgid": "translation" } ``` ``` -------------------------------- ### Convert PO file to JSON via command line Source: https://github.com/mikeedwards/po2json/blob/master/README.md Use the po2json command-line tool to convert a PO file to a JSON file. Specify the Jed format if using Jed >= 1.1.0. ```bash po2json translation.po translation.json ``` ```bash po2json translation.po translation.json -f jed1.x ``` -------------------------------- ### Parse PO to Jed formats Source: https://github.com/mikeedwards/po2json/blob/master/README.md Converts a PO file into formats compatible with different versions of the Jed library. ```javascript var po2json = require('po2json'), Jed = require('jed'); po2json.parseFile('messages.po', { format: 'jed' }, function (err, jsonData) { var i18n = new Jed( jsonData ); }); ``` ```javascript var po2json = require('po2json'), Jed = require('jed'); po2json.parseFile('messages.po', { format: 'jedold' }, function (err, jsonData) { var i18n = new Jed( jsonData ); }); ``` -------------------------------- ### Parse PO file to JSON synchronously Source: https://github.com/mikeedwards/po2json/blob/master/README.md Use the `parseFileSync` method for synchronous PO file parsing. A try-catch block is recommended to handle potential errors. ```javascript var po2json = require('po2json'); var jsonData = ''; try { jsonData = po2json.parseFileSync('messages.po'); // do something interesting ... } catch (e) {} ``` -------------------------------- ### JavaScript API: parseFileSync Source: https://context7.com/mikeedwards/po2json/llms.txt Synchronously reads and parses a PO file to JSON. This method is blocking and suitable for build scripts or initialization where synchronous execution is acceptable. It returns the parsed JSON directly and throws an error if the file cannot be read or parsed. ```APIDOC ## po2json.parseFileSync(fileName, options) ### Description Synchronously reads and parses a PO file to JSON. This blocking method is useful for build scripts and initialization code where synchronous execution is acceptable. It returns the parsed JSON directly and throws an error if the file cannot be read or parsed. ### Parameters #### Path Parameters - **fileName** (string) - Required - The path to the PO file to parse. #### Query Parameters - **options** (object) - Optional - Configuration options for parsing. - **format** (string) - Optional - The output format. Supported values: 'jed' (default), 'jedold', 'mf'. - **fullMF** (boolean) - Optional - When `format` is 'mf', if true, returns headers, translations, and pluralFunction. Defaults to false. - **stringify** (boolean) - Optional - If true, the output will be a JSON string. Defaults to false. - **pretty** (boolean) - Optional - If true and `stringify` is true, the output JSON string will be pretty-printed. Defaults to false. - **fallbackToMsgid** (boolean) - Optional - If true, uses msgid as fallback when a translation is missing. Defaults to false. - **domain** (string) - Optional - Specifies a custom domain for the Jed format. ### Request Example ```javascript const po2json = require('po2json'); // Basic synchronous parsing try { const jsonData = po2json.parseFileSync('locales/pl.po'); console.log('Translations loaded:', Object.keys(jsonData).length, 'entries'); } catch (err) { console.error('Failed to parse PO file:', err.message); } // With Jed format for use with Jed library const Jed = require('jed'); const jedData = po2json.parseFileSync('locales/pl.po', { format: 'jed' }); const i18n = new Jed(jedData); // With MessageFormat for full plural support const MessageFormat = require('messageformat'); const mfData = po2json.parseFileSync('locales/pl.po', { format: 'mf', fullMF: true }); // Build script usage - convert multiple files const fs = require('fs'); const locales = ['en', 'es', 'fr', 'de', 'pl']; locales.forEach(locale => { const input = `locales/${locale}.po`; const output = `dist/locales/${locale}.json`; const json = po2json.parseFileSync(input, { format: 'jed', stringify: true, pretty: true }); fs.writeFileSync(output, json); console.log(`Converted ${input} -> ${output}`); }); ``` ### Response #### Success Response (200) - **jsonData** (object | string) - The parsed JSON data from the PO file. The type depends on the `stringify` option. #### Response Example ```json { "msgid1": "translation1", "msgid2": { "other": "translation2_other", "plural": "translation2_plural" } } ``` ``` -------------------------------- ### Parse PO files asynchronously with po2json.parseFile Source: https://context7.com/mikeedwards/po2json/llms.txt Reads and parses PO files from the file system using a callback or promisified interface. Useful for integrating with i18n libraries like Jed or MessageFormat. ```javascript const po2json = require('po2json'); // Basic async file parsing po2json.parseFile('locales/es.po', function(err, jsonData) { if (err) { console.error('Error parsing PO file:', err); return; } console.log('Parsed translations:', jsonData); }); // With options - Jed format po2json.parseFile('locales/fr.po', { format: 'jed', domain: 'app' }, function(err, jsonData) { if (err) { console.error('Error:', err); return; } // Use with Jed library const Jed = require('jed'); const i18n = new Jed(jsonData); console.log(i18n.gettext('Hello, world!')); }); // With MessageFormat output po2json.parseFile('locales/de.po', { format: 'mf' }, function(err, translations) { if (err) { console.error('Error:', err); return; } const MessageFormat = require('messageformat'); const mf = new MessageFormat('de'); const messages = mf.compile(translations); // Use compiled messages console.log(messages['one item']([5])); // "5 Artikel" }); // Promise wrapper for modern async/await usage const { promisify } = require('util'); const parseFileAsync = promisify(po2json.parseFile); async function loadTranslations() { try { const translations = await parseFileAsync('locales/es.po', { format: 'jed' }); return translations; } catch (err) { console.error('Failed to load translations:', err); throw err; } } ``` -------------------------------- ### Parse PO Data for MessageFormat Output Source: https://context7.com/mikeedwards/po2json/llms.txt Parse PO data using the 'mf' format option for MessageFormat compatibility. This results in msgid keys mapped to their translated string values, with pluralization handled within the string. ```javascript // Example: MessageFormat output structure const mfOutput = po2json.parse(poBuffer, { format: 'mf' }); // { // "msgid": "translation", // "msgid": "{0, plural, one{singular} other{{0} plural}}" // } ``` -------------------------------- ### Parse PO to MessageFormat Source: https://github.com/mikeedwards/po2json/blob/master/README.md Converts a PO file into a format compatible with the messageformat library using fullMF mode. ```javascript 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 PO Data for Raw Format Output Source: https://context7.com/mikeedwards/po2json/llms.txt Parse PO data using the 'raw' format option. This output structure uses msgid as the key and an array containing the translation (or null for headers) as the value. ```javascript // Example: Raw format output structure const rawOutput = po2json.parse(poBuffer, { format: 'raw' }); // { // "": { headers... }, // "msgid": [null, "translation"], // singular // "msgid": ["plural", "sing", "plur1", ...] // plural // } ``` -------------------------------- ### Parse PO file to JSON asynchronously Source: https://github.com/mikeedwards/po2json/blob/master/README.md Use the `parseFile` method for asynchronous PO file parsing. A callback function handles the result or error. ```javascript var po2json = require('po2json'); po2json.parseFile('messages.po', function (err, jsonData) { // do something interesting ... }); ``` -------------------------------- ### Parse PO Data for Full MessageFormat Output Source: https://context7.com/mikeedwards/po2json/llms.txt Parse PO data with the 'mf' format and 'fullMF: true' option. This includes headers, translations, and an extracted pluralFunction, useful for complex pluralization rules. ```javascript // Example: Full MessageFormat output structure const fullMfOutput = po2json.parse(poBuffer, { format: 'mf', fullMF: true }); // { // "headers": { "language": "es", ... }, // "translations": { "": { ... }, "context": { ... } }, // "pluralFunction": function(n) { ... } // } ``` -------------------------------- ### po2json CLI Include Fuzzy Translations Source: https://context7.com/mikeedwards/po2json/llms.txt Use the -F flag to include fuzzy translations in the output. ```bash po2json -F translation.po translation.json ``` -------------------------------- ### Require po2json as a library Source: https://github.com/mikeedwards/po2json/blob/master/README.md Import the po2json module in your Javascript project. ```javascript var po2json = require('po2json'); ``` -------------------------------- ### PO to JSON Conversion API Source: https://github.com/mikeedwards/po2json/blob/master/README.md The po2json library offers methods to parse PO files into JSON. It supports parsing from buffers or files, with options to control the output format, inclusion of fuzzy translations, and stringification. ```APIDOC ## po2json.parse(buf[, options]) ### Description Parses a PO buffer or string into a Javascript object or JSON string. ### Method `po2json.parse ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **buf** (Buffer | string) - Required - The PO file content as a Buffer or unicode string. - **options** (object) - Optional - Configuration options for parsing. - **fuzzy** (boolean) - Optional - Whether to include fuzzy translations. Defaults to `false`. - **stringify** (boolean) - Optional - If `true`, returns a JSON string. Defaults to `false`. - **pretty** (boolean) - Optional - If `true`, pretty-prints the JSON string. Has no effect if `stringify` is `false`. Defaults to `false`. - **format** (string) - Optional - The output format. Options: `raw`, `jed`, `jedold`, `mf`. Defaults to `raw`. - **domain** (string) - Optional - The domain for Jed format. Only effective if `format: 'jed'`. - **fallback-to-msgid** (boolean) - Optional - Use `msgid` as translation if the entry is missing or fuzzy. Defaults to `false`. ### Request Example ```json { "example": "po2json.parse(buffer, { fuzzy: true, stringify: true, format: 'jed' })" } ``` ### Response #### Success Response (200) - **jsonData** (object | string) - The parsed JSON data, either as a Javascript object or a JSON string, depending on the `stringify` option. #### Response Example ```json { "example": "{ \"translations\": { \"hello\": \"world\" } }" } ``` ```APIDOC ## po2json.parseFile(fileName[, options], cb) ### Description Parses a PO file directly from the file system asynchronously. ### Method `po2json.parseFile ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **fileName** (string) - Required - The path to the PO file. - **options** (object) - Optional - Configuration options, same as for `po2json.parse`. - **cb** (function) - Required - A callback function that receives `err` and `jsonData`. ### Request Example ```javascript po2json.parseFile('messages.po', { format: 'mf' }, function (err, jsonData) { // handle result }); ``` ### Response #### Success Response (200) - **jsonData** (object | string) - The parsed JSON data. #### Response Example ```json { "example": "{ \"translations\": { \"hello\": \"world\" } }" } ``` ```APIDOC ## po2json.parseFileSync(fileName[, options]) ### Description Parses a PO file directly from the file system synchronously. ### Method `po2json.parseFileSync ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **fileName** (string) - Required - The path to the PO file. - **options** (object) - Optional - Configuration options, same as for `po2json.parse`. ### Request Example ```javascript try { var jsonData = po2json.parseFileSync('messages.po'); } catch (e) {} ``` ### Response #### Success Response (200) - **jsonData** (object | string) - The parsed JSON data. #### Response Example ```json { "example": "{ \"translations\": { \"hello\": \"world\" } }" } ``` -------------------------------- ### Parse PO Data for Jed Format Output Source: https://context7.com/mikeedwards/po2json/llms.txt Parse PO data with the 'jed' format option. This structure is compatible with Jed version 1.1.0 and later, including domain, locale data, and plural forms. ```javascript // Example: Jed format output structure const jedOutput = po2json.parse(poBuffer, { format: 'jed', domain: 'app' }); // { // "domain": "app", // "locale_data": { // "app": { // "": { "domain": "app", "plural_forms": "...", "lang": "es" }, // "msgid": ["translation"], // "msgid": ["singular", "plural1", "plural2"] // } // } // } ``` -------------------------------- ### po2json.parse(buffer, options) Source: https://context7.com/mikeedwards/po2json/llms.txt Parses a PO buffer or string directly into a JavaScript object or JSON string. ```APIDOC ## po2json.parse(buffer, options) ### Description Parses a PO buffer or string directly to JSON. This function accepts PO data as a Buffer or unicode string and returns a JavaScript object or JSON string based on the provided options. ### Parameters #### Request Body - **buffer** (Buffer/String) - Required - The PO file content to parse. - **options** (Object) - Optional - Configuration options including 'format' (raw, jed, mf), 'stringify', 'pretty', 'fuzzy', 'domain', and 'fallback-to-msgid'. ### Response #### Success Response (200) - **result** (Object/String) - The parsed translation data in the requested format. ``` -------------------------------- ### Parse PO data to JSON with po2json.parse Source: https://context7.com/mikeedwards/po2json/llms.txt Converts PO strings or buffers into JSON objects or strings. Supports multiple output formats including raw, Jed, and MessageFormat, with options for fuzzy translations and fallback behavior. ```javascript const po2json = require('po2json'); const fs = require('fs'); // Sample PO content const poContent = ` msgid "" msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" "Language: es\n" msgid "Hello, world!" msgstr "¡Hola, mundo!" msgid "one item" msgid_plural "%d items" msgstr[0] "un artículo" msgstr[1] "%d artículos" `; // Basic parsing - returns raw JSON object const rawResult = po2json.parse(poContent); console.log(rawResult); // Output: // { // "": { "content-type": "text/plain; charset=UTF-8", "plural-forms": "nplurals=2; plural=(n != 1);", "language": "es" }, // "Hello, world!": [null, "¡Hola, mundo!"], // "one item": ["%d items", "un artículo", "%d artículos"] // } // Parse with options - stringify and pretty print const jsonString = po2json.parse(poContent, { stringify: true, pretty: true }); console.log(jsonString); // Output: formatted JSON string // Parse for Jed >= 1.1.0 compatibility const jedResult = po2json.parse(poContent, { format: 'jed', domain: 'messages' }); console.log(jedResult); // Output: // { // "domain": "messages", // "locale_data": { // "messages": { // "": { "domain": "messages", "plural_forms": "nplurals=2; plural=(n != 1);", "lang": "es" }, // "Hello, world!": ["¡Hola, mundo!"], // "one item": ["un artículo", "%d artículos"] // } // } // } // Parse for MessageFormat compatibility const mfResult = po2json.parse(poContent, { format: 'mf' }); console.log(mfResult); // Output: // { // "Hello, world!": "¡Hola, mundo!", // "one item": "{0, plural, one{un artículo} other{{0} artículos}}" // } // Include fuzzy translations const fuzzyResult = po2json.parse(poContent, { fuzzy: true }); // Fallback to msgid when translation is missing const fallbackResult = po2json.parse(poContent, { 'fallback-to-msgid': true }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.