Parse and compile gettext po and mo files, nothing more, nothing less
Installations
npm install gettext-parser
Developer
smhg
Developer Guide
Module System
ESM
Min. Node Version
>=18
Typescript Support
No
Node Version
20.11.0
NPM Version
10.4.0
Statistics
160 Stars
218 Commits
44 Forks
9 Watching
5 Branches
22 Contributors
Updated on 16 Sept 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
90,056,438
Last day
-2.4%
113,686
Compared to previous day
Last week
1.5%
546,895
Compared to previous week
Last month
22.7%
2,265,767
Compared to previous month
Last year
27.4%
22,299,685
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
gettext-parser
Parse and compile gettext po and mo files with node.js, nothing more, nothing less.
Please note: starting with version 3 only latest LTS and latest stable node versions are supported. Use version 2 with older node versions.
Usage
Include the library:
import gettextParser from "gettext-parser";
Parse PO files
Parse a PO file with
gettextParser.po.parse(input[, options]) → Object
Where
- input is a po file as a Buffer or an unicode string. Charset is converted to unicode from other encodings only if the input is a Buffer, otherwise the charset information is discarded
- options is an optional object with the following optional properties:
-
defaultCharset is the charset to use if charset is not defined or is the default
"CHARSET"
(applies only if input is a Buffer) -
validation is a flag to turn on PO source file validation. The validation makes sure that:
- there is exactly zero or one
msgid_plural
definition per translation entry; aMultiple msgid_plural error
error gets thrown otherwise. - there are no duplicate entries with exact
msgid
values; aDuplicate msgid error
error gets thrown otherwise. - the number of plural forms matches exactly the number from
nplurals
defined inPlural-Forms
header for entries that have plural forms; aPlural forms range error
error gets thrown otherwise. - the number of
msgstr
matches exacty the one (ifmsgid_plural
is not defined) or the number fromnplurals
(ifmsgid_plural
is defined); aTranslation string range error
error gets thrown otherwise.
- there is exactly zero or one
-
Method returns gettext-parser specific translation object (see below)
Example
1import { readFileSync } from 'node:fs'; 2var input = readFileSync('en.po'); 3var po = gettextParser.po.parse(input); 4console.log(po.translations['']); // output translations for the default context
Parse PO as a Stream
PO files can also be parsed from a stream source. After all input is processed the parser emits a single 'data' event which contains the parsed translation object.
gettextParser.po.createParseStream([options][, transformOptions]) → Transform Stream
Where
- options is an optional object, same as in
parse
. See Parse PO files section for details. - transformOptions are the standard stream options.
Example
1import { createReadStream } from 'node:fs'; 2var input = createReadStream('en.po'); 3var po = gettextParser.po.createParseStream(); 4input.pipe(po); 5po.on('data', function(data){ 6 console.log(data.translations['']); // output translations for the default context 7});
Compile PO from a translation object
If you have a translation object you can convert this to a valid PO file with
gettextParser.po.compile(data[, options]) → Buffer
Where
- data is a translation object either got from parsing a PO/MO file or composed by other means
- options is a configuration object with possible values
- foldLength is the length at which to fold message strings into newlines (default: 76). Set to 0 or false to disable folding.
- sort (boolean|Function) - (default
false
) iftrue
, entries will be sorted by msgid in the resulting .po(.pot) file. If a comparator function is provided, that function will be used to sort entries in the output. The function is called with two arguments, each of which is a single message entry with the structure described below. The function should follow the standard rules for functions passed toArray.sort()
: return0
if the entries are interchangeable in sort order; return a number less than 0 if the first entry should come before the second one; and return a number greater than 0 if the second entry should come before the first one. - escapeCharacters (boolean) - (default
true
) iffalse
, will skip escape newlines and quotes characters functionality.
Example
1import { writeFileSync } from 'node:fs'; 2var data = { 3 ... 4}; 5var output = gettextParser.po.compile(data); 6writeFileSync('filename.po', output);
Parse MO files
Parse a MO file with
gettextParser.mo.parse(input[, defaultCharset]) → Object
Where
- input is a mo file as a Buffer
- defaultCharset is the charset to use if charset is not defined or is the default
"CHARSET"
Method returns gettext-parser specific translation object (see below)
Example
1import { readFileSync } from 'node:fs'; 2var input = readFileSync('en.mo'); 3var mo = gettextParser.mo.parse(input); 4console.log(mo.translations['']); // output translations for the default context
Compile MO from a translation object
If you have a translation object you can convert this to a valid MO file with
gettextParser.mo.compile(data) → Buffer
Where
- data is a translation object either got from parsing a PO/MO file or composed by other means
Example
1import { writeFileSync } from 'node:fs'; 2var data = { 3 ... 4}; 5var output = gettextParser.mo.compile(data); 6writeFileSync('filename.mo', output);
Notes
Overriding charset
If you are compiling a previously parsed translation object, you can override the output charset with the charset
property (applies both for compiling mo and po files).
1var obj = gettextParser.po.parse(inputBuf); 2obj.charset = "windows-1257"; 3outputBuf = gettextParser.po.compile(obj);
Headers for the output are modified to match the updated charset.
ICONV support
By default gettext-parser uses pure JS iconv-lite for encoding and decoding non UTF-8 charsets. If you need to support more complex encodings that are not supported by iconv-lite, you need to add iconv as an additional dependency for your project (gettext-parser will detect if it is available and tries to use it instead of iconv-lite).
Data structure of parsed mo/po files
Character set
Parsed data is always in unicode but the original charset of the file can
be found from the charset
property. This value is also used when compiling translations
to a mo or po file.
Headers
Headers can be found from the headers
object, all keys are lowercase and the value for a key is a string. This value will also be used when compiling.
Translations
Translations can be found from the translations
object which in turn holds context objects for msgctxt
. Default context can be found from translations[""]
.
Context objects include all the translations, where msgid
value is the key. The value is an object with the following possible properties:
- msgctxt context for this translation, if not present the default context applies
- msgid string to be translated
- msgid_plural the plural form of the original string (might not be present)
- msgstr an array of translations
- comments an object with the following properties:
translator
,reference
,extracted
,flag
,previous
.
Example
1{ 2 "charset": "iso-8859-1", 3 4 "headers": { 5 "content-type": "text/plain; charset=iso-8859-1", 6 "plural-forms": "nplurals=2; plural=(n!=1);" 7 }, 8 9 "translations": { 10 "": { 11 "": { 12 "msgid": "", 13 "msgstr": ["Content-Type: text/plain; charset=iso-8859-1\n..."] 14 } 15 }, 16 "another context": { 17 "%s example": { 18 "msgctxt": "another context", 19 "msgid": "%s example", 20 "msgid_plural": "%s examples", 21 "msgstr": ["% näide", "%s näidet"], 22 "comments": { 23 "translator": "This is regular comment", 24 "reference": "/path/to/file:123" 25 } 26 } 27 } 28 } 29}
Notice that the structure has both a headers
object and a ""
translation with the header string. When compiling the structure to a mo or a po file, the headers
object is used to define the header. Header string in the ""
translation is just for reference (includes the original unmodified data) but will not be used when compiling. So if you need to add or alter header values, use only the headers
object.
If you need to convert gettext-parser formatted translation object to something else, eg. for jed, check out po2json.
License
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/smhg/gettext-parser/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/smhg/gettext-parser/ci.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:24
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 5 are checked with a SAST tool
Score
3.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More