@@ -22,7 +22,7 @@ object destructuring and arrow functions, you'll want to use a transpiler for th
2222
2323``` js
2424const { parsePo , parseMo } = require (' gettext-to-messageformat' )
25- const { headers , translations } = parsePo (`
25+ const { headers , pluralFunction , translations } = parsePo (`
2626# Examples from http://pology.nedohodnik.net/doc/user/en_US/ch-poformat.html
2727# Note that the given plural-form is incomplete
2828msgid ""
@@ -48,7 +48,7 @@ msgstr "Nema zvezde po imenu %(starname)s."
4848` )
4949
5050const MessageFormat = require (' messageformat' )
51- const mf = new MessageFormat (headers .language )
51+ const mf = new MessageFormat ({ [ headers .language ] : pluralFunction } )
5252const messages = mf .compile (translations)
5353
5454messages[' Time: %1 second' ]([1 ])
@@ -80,20 +80,32 @@ the parser, including the following fields:
8080 If no context is set, by default this top-level key is not included unless
8181 ` forceContext ` is set to ` true ` .
8282
83- - ` pluralCategories ` (array of strings) – If the Language header is not set in
84- the input, or if its Plural-Forms ` nplurals ` value is not 1, 2, or 6, this
85- needs to be set to the pluralization category names to be used for the input
86- enumerated categories if any message includes a plural form.
83+ - ` pluralFunction ` (function) – If your input file does not include a Plural-Forms
84+ header, or if for whatever reason you'd prefer to use your own, set this to be
85+ a stringifiable function that takes in a single variable, and returns the
86+ appropriate pluralisation category. Following the model used internally in
87+ [ messageformat] , the function variable should also include ` cardinal ` as a
88+ member array of its possible categories, in the order corresponding to the
89+ gettext pluralisation categories. This is relevant if you'd like to avoid the
90+ ` new Function ` constructor otherwise used to generate ` pluralFunction ` , or to
91+ allow for more fine-tuned categories than gettext allows, e.g. differentiating
92+ between the categories of ` '1.0' ` and ` '1' ` .
8793
8894- ` verbose ` (boolean, default ` false ` ) – If set to ` true ` , missing translations
8995 will cause warnings.
9096
9197For more options, take a look at the [ source] ( ./index.js ) .
9298
93- Both functions return an object ` { headers, translations } ` where ` headers `
94- contains the raw contents of the input file's headers, with keys lower-cased, and
95- ` translations ` is an object containing the MessageFormat strings keyed by their
96- ` msgid ` and if used, ` msgctxt ` .
99+ Both functions return an object containing the following fields:
100+
101+ - ` headers ` (object) – The raw contents of the input file's headers, with keys
102+ lower-cased
103+ - ` pluralFunction ` (function) – An appropriate pluralisation function to use for
104+ the output translations, suitable to be used directly by [ messageformat] . May
105+ be ` null ` if none was set in ` options ` and if the input did not include a
106+ Plural-Forms header.
107+ - ` translations ` (object) – An object containing the MessageFormat strings keyed
108+ by their ` msgid ` and if used, ` msgctxt ` .
97109
98110[ messageformat ] : https://messageformat.github.io/
99111[ gettext-parser ] : https://github.com/smhg/gettext-parser
0 commit comments