| 1 |
gettext-parser [](https://github.com/smhg/gettext-parser/actions/workflows/ci.yml) |
| 2 |
============== |
| 3 |
|
| 4 |
Parse and compile gettext *po* and *mo* files with node.js, nothing more, nothing less. |
| 5 |
|
| 6 |
> *Please note:* starting with version 3 only latest LTS and latest stable node versions are supported. **Use version 2 with older node versions.** |
| 7 |
|
| 8 |
## Usage |
| 9 |
|
| 10 |
Include the library: |
| 11 |
|
| 12 |
import gettextParser from "gettext-parser"; |
| 13 |
|
| 14 |
### Parse PO files |
| 15 |
|
| 16 |
Parse a PO file with |
| 17 |
|
| 18 |
gettextParser.po.parse(input[, options]) → Object |
| 19 |
|
| 20 |
Where |
| 21 |
|
| 22 |
* **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 |
| 23 |
* **options** is an optional object with the following optional properties: |
| 24 |
* **defaultCharset** is the charset to use if charset is not defined or is the default `"CHARSET"` (applies only if *input* is a Buffer) |
| 25 |
* **validation** is a flag to turn on PO source file validation. The validation makes sure that: |
| 26 |
|
| 27 |
* there is exactly zero or one `msgid_plural` definition per translation entry; a `Multiple msgid_plural error` error gets thrown otherwise. |
| 28 |
* there are no duplicate entries with exact `msgid` values; a `Duplicate msgid error` error gets thrown otherwise. |
| 29 |
* the number of plural forms matches exactly the number from `nplurals` defined in `Plural-Forms` header for entries that have plural forms; a `Plural forms range error` error gets thrown otherwise. |
| 30 |
* the number of `msgstr` matches exacty the one (if `msgid_plural` is not defined) or the number from `nplurals` (if `msgid_plural` is defined); a `Translation string range error` error gets thrown otherwise. |
| 31 |
|
| 32 |
Method returns gettext-parser specific translation object (see below) |
| 33 |
|
| 34 |
**Example** |
| 35 |
|
| 36 |
```javascript |
| 37 |
import { readFileSync } from 'node:fs'; |
| 38 |
var input = readFileSync('en.po'); |
| 39 |
var po = gettextParser.po.parse(input); |
| 40 |
console.log(po.translations['']); // output translations for the default context |
| 41 |
``` |
| 42 |
|
| 43 |
### Parse PO as a Stream |
| 44 |
|
| 45 |
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. |
| 46 |
|
| 47 |
gettextParser.po.createParseStream([options][, transformOptions]) → Transform Stream |
| 48 |
|
| 49 |
Where |
| 50 |
|
| 51 |
* **options** is an optional object, same as in `parse`. See [Parse PO files](#parse-po-files) section for details. |
| 52 |
* **transformOptions** are the standard stream options. |
| 53 |
|
| 54 |
**Example** |
| 55 |
|
| 56 |
```javascript |
| 57 |
import { createReadStream } from 'node:fs'; |
| 58 |
var input = createReadStream('en.po'); |
| 59 |
var po = gettextParser.po.createParseStream(); |
| 60 |
input.pipe(po); |
| 61 |
po.on('data', function(data){ |
| 62 |
console.log(data.translations['']); // output translations for the default context |
| 63 |
}); |
| 64 |
``` |
| 65 |
|
| 66 |
### Compile PO from a translation object |
| 67 |
|
| 68 |
If you have a translation object you can convert this to a valid PO file with |
| 69 |
|
| 70 |
gettextParser.po.compile(data[, options]) → Buffer |
| 71 |
|
| 72 |
Where |
| 73 |
|
| 74 |
* **data** is a translation object either got from parsing a PO/MO file or composed by other means |
| 75 |
* **options** is a configuration object with possible values |
| 76 |
* **foldLength** is the length at which to fold message strings into newlines (default: 76). Set to 0 or false to disable folding. |
| 77 |
* **sort** (boolean|Function) - (default `false`) if `true`, entries will be sorted by msgid in the resulting .po(.pot) file. |
| 78 |
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 to `Array.sort()`: return `0` 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. |
| 79 |
* **escapeCharacters** (boolean) - (default `true`) if `false`, will skip escape newlines and quotes characters functionality. |
| 80 |
|
| 81 |
**Example** |
| 82 |
|
| 83 |
```javascript |
| 84 |
import { writeFileSync } from 'node:fs'; |
| 85 |
var data = { |
| 86 |
... |
| 87 |
}; |
| 88 |
var output = gettextParser.po.compile(data); |
| 89 |
writeFileSync('filename.po', output); |
| 90 |
``` |
| 91 |
|
| 92 |
### |
| 93 |
|
| 94 |
### Parse MO files |
| 95 |
|
| 96 |
Parse a MO file with |
| 97 |
|
| 98 |
gettextParser.mo.parse(input[, defaultCharset]) → Object |
| 99 |
|
| 100 |
Where |
| 101 |
|
| 102 |
* **input** is a *mo* file as a Buffer |
| 103 |
* **defaultCharset** is the charset to use if charset is not defined or is the default `"CHARSET"` |
| 104 |
|
| 105 |
Method returns gettext-parser specific translation object (see below) |
| 106 |
|
| 107 |
**Example** |
| 108 |
|
| 109 |
```javascript |
| 110 |
import { readFileSync } from 'node:fs'; |
| 111 |
var input = readFileSync('en.mo'); |
| 112 |
var mo = gettextParser.mo.parse(input); |
| 113 |
console.log(mo.translations['']); // output translations for the default context |
| 114 |
``` |
| 115 |
|
| 116 |
### Compile MO from a translation object |
| 117 |
|
| 118 |
If you have a translation object you can convert this to a valid MO file with |
| 119 |
|
| 120 |
gettextParser.mo.compile(data) → Buffer |
| 121 |
|
| 122 |
Where |
| 123 |
|
| 124 |
* **data** is a translation object either got from parsing a PO/MO file or composed by other means |
| 125 |
|
| 126 |
**Example** |
| 127 |
|
| 128 |
```javascript |
| 129 |
import { writeFileSync } from 'node:fs'; |
| 130 |
var data = { |
| 131 |
... |
| 132 |
}; |
| 133 |
var output = gettextParser.mo.compile(data); |
| 134 |
writeFileSync('filename.mo', output); |
| 135 |
``` |
| 136 |
|
| 137 |
### Notes |
| 138 |
|
| 139 |
#### Overriding charset |
| 140 |
|
| 141 |
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). |
| 142 |
|
| 143 |
```javascript |
| 144 |
var obj = gettextParser.po.parse(inputBuf); |
| 145 |
obj.charset = "windows-1257"; |
| 146 |
outputBuf = gettextParser.po.compile(obj); |
| 147 |
``` |
| 148 |
|
| 149 |
Headers for the output are modified to match the updated charset. |
| 150 |
|
| 151 |
#### ICONV support |
| 152 |
|
| 153 |
By default *gettext-parser* uses pure JS [iconv-lite](https://github.com/ashtuchkin/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](https://github.com/bnoordhuis/node-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*). |
| 154 |
|
| 155 |
## Data structure of parsed mo/po files |
| 156 |
|
| 157 |
### Character set |
| 158 |
|
| 159 |
Parsed data is always in unicode but the original charset of the file can |
| 160 |
be found from the `charset` property. This value is also used when compiling translations |
| 161 |
to a *mo* or *po* file. |
| 162 |
|
| 163 |
### Headers |
| 164 |
|
| 165 |
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. |
| 166 |
|
| 167 |
### Translations |
| 168 |
|
| 169 |
Translations can be found from the `translations` object which in turn holds context objects for `msgctxt`. Default context can be found from `translations[""]`. |
| 170 |
|
| 171 |
Context objects include all the translations, where `msgid` value is the key. The value is an object with the following possible properties: |
| 172 |
|
| 173 |
* **msgctxt** context for this translation, if not present the default context applies |
| 174 |
* **msgid** string to be translated |
| 175 |
* **msgid_plural** the plural form of the original string (might not be present) |
| 176 |
* **msgstr** an array of translations |
| 177 |
* **comments** an object with the following properties: `translator`, `reference`, `extracted`, `flag`, `previous`. |
| 178 |
|
| 179 |
Example |
| 180 |
|
| 181 |
```json |
| 182 |
{ |
| 183 |
"charset": "iso-8859-1", |
| 184 |
|
| 185 |
"headers": { |
| 186 |
"content-type": "text/plain; charset=iso-8859-1", |
| 187 |
"plural-forms": "nplurals=2; plural=(n!=1);" |
| 188 |
}, |
| 189 |
|
| 190 |
"translations": { |
| 191 |
"": { |
| 192 |
"": { |
| 193 |
"msgid": "", |
| 194 |
"msgstr": ["Content-Type: text/plain; charset=iso-8859-1\n..."] |
| 195 |
} |
| 196 |
}, |
| 197 |
"another context": { |
| 198 |
"%s example": { |
| 199 |
"msgctxt": "another context", |
| 200 |
"msgid": "%s example", |
| 201 |
"msgid_plural": "%s examples", |
| 202 |
"msgstr": ["% näide", "%s näidet"], |
| 203 |
"comments": { |
| 204 |
"translator": "This is regular comment", |
| 205 |
"reference": "/path/to/file:123" |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
``` |
| 212 |
|
| 213 |
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. |
| 214 |
|
| 215 |
If you need to convert *gettext-parser* formatted translation object to something else, eg. for *jed*, check out [po2json](https://github.com/mikeedwards/po2json). |
| 216 |
|
| 217 |
## License |
| 218 |
|
| 219 |
**MIT** |