Skip to content

Commit 98d205b

Browse files
committed
chore: upgrade pofile-ts to v3.0.0
BREAKING CHANGE: pofile-ts v3.0.0 uses a functional API instead of classes Migration: - PO.parse() β†’ parsePo() - po.toString() β†’ stringifyPo(po) - new PO() β†’ createPoFile() - new PO.Item() β†’ createItem() - PO class β†’ PoFile interface - Item class β†’ PoItem interface
1 parent bad96d6 commit 98d205b

File tree

7 files changed

+60
-45
lines changed

7 files changed

+60
-45
lines changed

β€Žpackages/cli/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"normalize-path": "^3.0.0",
8181
"ora": "^5.1.0",
8282
"picocolors": "^1.1.1",
83-
"pofile-ts": "^2.2.0",
83+
"pofile-ts": "^3.0.0",
8484
"pseudolocale": "^2.0.0",
8585
"source-map": "^0.7.6",
8686
"threads": "^1.7.0"

β€Žpackages/cli/src/services/translationIO.tsβ€Ž

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import fs from "fs"
22
import { dirname } from "path"
3-
import PO, { Item as POItem, type Headers as POHeaders } from "pofile-ts"
3+
import {
4+
parsePo,
5+
stringifyPo,
6+
createPoFile,
7+
createItem,
8+
type PoItem,
9+
type Headers as POHeaders,
10+
} from "pofile-ts"
411
import https from "https"
512
import { globSync } from "glob"
613
import { format as formatDate } from "date-fns"
@@ -109,7 +116,7 @@ function init(
109116
// Create segments from source locale PO items
110117
paths[sourceLocale].forEach((path) => {
111118
const raw = fs.readFileSync(path).toString()
112-
const po = PO.parse(raw)
119+
const po = parsePo(raw)
113120

114121
po.items
115122
.filter((item) => !item["obsolete"])
@@ -126,7 +133,7 @@ function init(
126133
targetLocales.forEach((targetLocale) => {
127134
paths[targetLocale].forEach((path) => {
128135
const raw = fs.readFileSync(path).toString()
129-
const po = PO.parse(raw)
136+
const po = parsePo(raw)
130137

131138
po.items
132139
.filter((item) => !item["obsolete"])
@@ -181,7 +188,7 @@ function sync(
181188
// Create segments with correct source
182189
paths[sourceLocale].forEach((path) => {
183190
const raw = fs.readFileSync(path).toString()
184-
const po = PO.parse(raw)
191+
const po = parsePo(raw)
185192

186193
po.items
187194
.filter((item) => !item["obsolete"])
@@ -223,7 +230,7 @@ function sync(
223230
)
224231
}
225232

226-
function createSegmentFromPoItem(item: POItem) {
233+
function createSegmentFromPoItem(item: PoItem) {
227234
const itemHasExplicitId = item.extractedComments.includes(EXPLICIT_ID_FLAG)
228235
const itemHasContext = item.msgctxt != null
229236

@@ -276,7 +283,7 @@ function createPoItemFromSegment(segment: TranslationIoSegment) {
276283
EXPLICIT_ID_AND_CONTEXT_FLAG
277284
)
278285

279-
const item = new PO.Item()
286+
const item = createItem()
280287

281288
if (segmentHasExplicitId || segmentHasExplicitIdAndContext) {
282289
item.msgid = segment.context
@@ -337,10 +344,10 @@ function saveSegmentsToTargetPos(
337344
)
338345
const segments = segmentsPerLocale[targetLocale]
339346

340-
const po = new PO()
347+
const po = createPoFile()
341348
po.headers = getCreateHeaders(targetLocale)
342349

343-
const items: POItem[] = []
350+
const items: PoItem[] = []
344351

345352
segments.forEach((segment: TranslationIoSegment) => {
346353
const item = createPoItemFromSegment(segment)
@@ -362,7 +369,7 @@ function saveSegmentsToTargetPos(
362369
/* istanbul ignore next -- @preserve Integration with external Translation.io service */
363370
fs.promises
364371
.mkdir(dirname(localePath), { recursive: true })
365-
.then(() => fs.promises.writeFile(localePath, po.toString()))
372+
.then(() => fs.promises.writeFile(localePath, stringifyPo(po)))
366373
.catch((err) => {
367374
console.error("Error while saving target PO files:")
368375
console.error(err)

β€Žpackages/format-po-gettext/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"cldr-core": "^45.0.0",
5151
"node-gettext": "^3.0.0",
5252
"plurals-cldr": "^2.0.1",
53-
"pofile-ts": "^2.2.0"
53+
"pofile-ts": "^3.0.0"
5454
},
5555
"devDependencies": {
5656
"@lingui/jest-mocks": "workspace:^",

β€Žpackages/format-po-gettext/src/po-gettext.tsβ€Ž

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse as parseIcu, Select, SelectCase } from "@messageformat/parser"
22
import pluralsCldr from "plurals-cldr"
3-
import PO, { Item as POItem } from "pofile-ts"
3+
import { parsePo, stringifyPo, createItem, type PoItem } from "pofile-ts"
44
import gettextPlurals from "node-gettext/lib/plurals"
55

66
import type { CatalogFormatter, CatalogType, MessageType } from "@lingui/conf"
@@ -50,12 +50,12 @@ const LINE_ENDINGS = /\r?\n/g
5050
const DEFAULT_CTX_PREFIX = "js-lingui:"
5151

5252
function 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

225225
const 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

306306
const 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
*/
369369
function 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
*/
441441
function 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
}

β€Žpackages/format-po/package.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"@lingui/conf": "5.6.1",
4747
"@lingui/message-utils": "5.6.1",
4848
"date-fns": "^3.6.0",
49-
"pofile-ts": "^2.2.0"
49+
"pofile-ts": "^3.0.0"
5050
},
5151
"devDependencies": {
5252
"@lingui/jest-mocks": "workspace:^",

β€Žpackages/format-po/src/po.tsβ€Ž

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { format as formatDate } from "date-fns"
2-
import PO, { Item as POItem, type Headers as POHeaders } from "pofile-ts"
2+
import {
3+
parsePo,
4+
stringifyPo,
5+
createPoFile,
6+
createItem,
7+
type PoFile,
8+
type PoItem,
9+
type Headers as POHeaders,
10+
} from "pofile-ts"
311

412
import { CatalogFormatter, CatalogType, MessageType } from "@lingui/conf"
513
import { generateMessageId } from "@lingui/message-utils/generateMessageId"
@@ -127,7 +135,7 @@ const serialize = (catalog: CatalogType, options: PoFormatterOptions) => {
127135
return Object.keys(catalog).map((id) => {
128136
const message: MessageType<POCatalogExtra> = catalog[id]
129137

130-
const item = new PO.Item()
138+
const item = createItem()
131139

132140
// The extractedComments array may be modified in this method,
133141
// so create a new array with the message's elements.
@@ -215,7 +223,7 @@ const serialize = (catalog: CatalogType, options: PoFormatterOptions) => {
215223
}
216224

217225
function deserialize(
218-
items: POItem[],
226+
items: PoItem[],
219227
options: PoFormatterOptions
220228
): CatalogType {
221229
return items.reduce<CatalogType<POCatalogExtra>>((catalog, item) => {
@@ -260,17 +268,17 @@ export function formatter(options: PoFormatterOptions = {}): CatalogFormatter {
260268
templateExtension: ".pot",
261269

262270
parse(content): CatalogType {
263-
const po = PO.parse(content)
271+
const po = parsePo(content)
264272
return deserialize(po.items, options)
265273
},
266274

267275
serialize(catalog, ctx): string {
268-
let po: PO
276+
let po: PoFile
269277

270278
if (ctx.existing) {
271-
po = PO.parse(ctx.existing)
279+
po = parsePo(ctx.existing)
272280
} else {
273-
po = new PO()
281+
po = createPoFile()
274282
po.headers = getCreateHeaders(
275283
ctx.locale,
276284
options.customHeaderAttributes
@@ -280,7 +288,7 @@ export function formatter(options: PoFormatterOptions = {}): CatalogFormatter {
280288
}
281289

282290
po.items = serialize(catalog, options)
283-
return po.toString()
291+
return stringifyPo(po)
284292
},
285293
}
286294
}

β€Žyarn.lockβ€Ž

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,7 @@ __metadata:
30293029
normalize-path: ^3.0.0
30303030
ora: ^5.1.0
30313031
picocolors: ^1.1.1
3032-
pofile-ts: ^2.2.0
3032+
pofile-ts: ^3.0.0
30333033
pseudolocale: ^2.0.0
30343034
source-map: ^0.7.6
30353035
threads: ^1.7.0
@@ -3135,7 +3135,7 @@ __metadata:
31353135
mockdate: ^3.0.5
31363136
node-gettext: ^3.0.0
31373137
plurals-cldr: ^2.0.1
3138-
pofile-ts: ^2.2.0
3138+
pofile-ts: ^3.0.0
31393139
unbuild: 2.0.0
31403140
languageName: unknown
31413141
linkType: soft
@@ -3149,7 +3149,7 @@ __metadata:
31493149
"@lingui/message-utils": 5.6.1
31503150
date-fns: ^3.6.0
31513151
mockdate: ^3.0.5
3152-
pofile-ts: ^2.2.0
3152+
pofile-ts: ^3.0.0
31533153
unbuild: 2.0.0
31543154
languageName: unknown
31553155
linkType: soft
@@ -13769,10 +13769,10 @@ __metadata:
1376913769
languageName: node
1377013770
linkType: hard
1377113771

13772-
"pofile-ts@npm:^2.2.0":
13773-
version: 2.2.0
13774-
resolution: "pofile-ts@npm:2.2.0"
13775-
checksum: f0d4b6c8b22e2e8ea4be98e8d20794cd0f40969e26dc35b2b0d0668a0a593d467b14145e10b9ebe9abc8b6c969d2b19ab390f26b8e57d79ea23195fa5aa5b958
13772+
"pofile-ts@npm:^3.0.0":
13773+
version: 3.0.0
13774+
resolution: "pofile-ts@npm:3.0.0"
13775+
checksum: 9d4a1737a4dad8e6ae9f7716ba050a9f5c758403f57e09abff50c5613b16095d2c7e13aadfaa888ad06df3d6905153b561a23b4332d18383ee2e59d9ff664237
1377613776
languageName: node
1377713777
linkType: hard
1377813778

0 commit comments

Comments
Β (0)