@@ -22,7 +22,7 @@ object destructuring and arrow functions, you'll want to use a transpiler for th
22
22
23
23
``` js
24
24
const { parsePo , parseMo } = require (' gettext-to-messageformat' )
25
- const { headers , translations } = parsePo (`
25
+ const { headers , pluralFunction , translations } = parsePo (`
26
26
# Examples from http://pology.nedohodnik.net/doc/user/en_US/ch-poformat.html
27
27
# Note that the given plural-form is incomplete
28
28
msgid ""
@@ -48,7 +48,7 @@ msgstr "Nema zvezde po imenu %(starname)s."
48
48
` )
49
49
50
50
const MessageFormat = require (' messageformat' )
51
- const mf = new MessageFormat (headers .language )
51
+ const mf = new MessageFormat ({ [ headers .language ] : pluralFunction } )
52
52
const messages = mf .compile (translations)
53
53
54
54
messages[' Time: %1 second' ]([1 ])
@@ -80,20 +80,32 @@ the parser, including the following fields:
80
80
If no context is set, by default this top-level key is not included unless
81
81
` forceContext ` is set to ` true ` .
82
82
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' ` .
87
93
88
94
- ` verbose ` (boolean, default ` false ` ) – If set to ` true ` , missing translations
89
95
will cause warnings.
90
96
91
97
For more options, take a look at the [ source] ( ./index.js ) .
92
98
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 ` .
97
109
98
110
[ messageformat ] : https://messageformat.github.io/
99
111
[ gettext-parser ] : https://github.com/smhg/gettext-parser
0 commit comments