11import { parse as parseIcu , Select , SelectCase } from "@messageformat/parser"
22import pluralsCldr from "plurals-cldr"
3- import PO , { Item as POItem } from "pofile-ts"
3+ import { parsePo , stringifyPo , createItem , type PoItem } from "pofile-ts"
44import gettextPlurals from "node-gettext/lib/plurals"
55
66import type { CatalogFormatter , CatalogType , MessageType } from "@lingui/conf"
@@ -50,12 +50,12 @@ const LINE_ENDINGS = /\r?\n/g
5050const DEFAULT_CTX_PREFIX = "js-lingui:"
5151
5252function serializePlurals (
53- item : POItem ,
53+ item : PoItem ,
5454 message : MessageType ,
5555 id : string ,
5656 isGeneratedId : boolean ,
5757 options : PoGettextFormatterOptions
58- ) : POItem {
58+ ) : PoItem {
5959 // Depending on whether custom ids are used by the developer, the (potential plural) "original", untranslated ICU
6060 // message can be found in `message.message` or in the item's `key` itself.
6161 const icuMessage = message . message
@@ -223,7 +223,7 @@ function parsePluralFormsFn(pluralFormsHeader: string): GettextPluralsInfo {
223223}
224224
225225const convertPluralsToICU = (
226- item : POItem ,
226+ item : PoItem ,
227227 pluralForms : string [ ] ,
228228 lang : string ,
229229 ctxPrefix : string = DEFAULT_CTX_PREFIX
@@ -304,7 +304,7 @@ const convertPluralsToICU = (
304304}
305305
306306const updateContextComment = (
307- item : POItem ,
307+ item : PoItem ,
308308 contextComment : string ,
309309 ctxPrefix : string
310310) => {
@@ -367,11 +367,11 @@ function getContextFromComments(
367367 * This happens when plural calls have identical strings but different variables
368368 */
369369function mergeDuplicatePluralEntries (
370- items : POItem [ ] ,
370+ items : PoItem [ ] ,
371371 options : PoGettextFormatterOptions
372- ) : POItem [ ] {
372+ ) : PoItem [ ] {
373373 const ctxPrefix = options . customICUPrefix || DEFAULT_CTX_PREFIX
374- const itemMap = new Map < string , POItem [ ] > ( )
374+ const itemMap = new Map < string , PoItem [ ] > ( )
375375
376376 // Group items by msgid + msgid_plural combination
377377 for ( const item of items ) {
@@ -385,7 +385,7 @@ function mergeDuplicatePluralEntries(
385385 }
386386 }
387387
388- const mergedItems : POItem [ ] = [ ]
388+ const mergedItems : PoItem [ ] = [ ]
389389
390390 for ( const duplicateItems of itemMap . values ( ) ) {
391391 if ( duplicateItems . length === 1 ) {
@@ -439,11 +439,11 @@ function replaceArgInIcu(icu: string, oldVar: string, newVar: string) {
439439 * This ensures all original message IDs are available in the compiled catalog
440440 */
441441function expandMergedPluralEntries (
442- items : POItem [ ] ,
442+ items : PoItem [ ] ,
443443 options : PoGettextFormatterOptions
444- ) : POItem [ ] {
444+ ) : PoItem [ ] {
445445 const ctxPrefix = options . customICUPrefix || DEFAULT_CTX_PREFIX
446- const expandedItems : POItem [ ] = [ ]
446+ const expandedItems : PoItem [ ] = [ ]
447447
448448 for ( const item of items ) {
449449 if ( ! item . msgid_plural ) {
@@ -473,7 +473,7 @@ function expandMergedPluralEntries(
473473
474474 // Create a new item for each variable after first
475475 for ( const variable of variableList ) {
476- const newItem = new PO . Item ( )
476+ const newItem = createItem ( )
477477
478478 // Set the msgid to the original ICU message
479479 newItem . msgid = item . msgid
@@ -519,7 +519,7 @@ export function formatter(
519519 templateExtension : ".pot" ,
520520
521521 parse ( content , ctx ) : CatalogType {
522- const po = PO . parse ( content )
522+ const po = parsePo ( content )
523523
524524 if ( options . mergePlurals ) {
525525 // Expand merged entries back to individual catalog entries BEFORE ICU conversion
@@ -543,11 +543,11 @@ export function formatter(
543543 )
544544 } )
545545
546- return formatter . parse ( po . toString ( ) , ctx ) as CatalogType
546+ return formatter . parse ( stringifyPo ( po ) , ctx ) as CatalogType
547547 } ,
548548
549549 serialize ( catalog , ctx ) : string {
550- const po = PO . parse ( formatter . serialize ( catalog , ctx ) as string )
550+ const po = parsePo ( formatter . serialize ( catalog , ctx ) as string )
551551
552552 po . items = po . items . map ( ( item ) => {
553553 const isGeneratedId = ! item . extractedComments . includes (
@@ -564,7 +564,7 @@ export function formatter(
564564 // Merge duplicate entries that have the same msgid and msgid_plural
565565 const mergedPlurals = mergeDuplicatePluralEntries ( po . items , options )
566566 const newItems = [ ]
567- const processed = new Set < POItem > ( )
567+ const processed = new Set < PoItem > ( )
568568
569569 // adding it this way versus just adding all mergedPlurals preserves order of translations
570570
@@ -587,7 +587,7 @@ export function formatter(
587587 po . items = newItems
588588 }
589589
590- return po . toString ( )
590+ return stringifyPo ( po )
591591 } ,
592592 }
593593}
0 commit comments