Skip to content

Commit

Permalink
feat(format-po): configure header attributes in PO file formatter (#1934
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mariarchy committed May 14, 2024
1 parent d01fa96 commit d90f778
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/format-po/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export type PoFormatterOptions = {
* @default false
*/
explicitIdAsDefault?: boolean

/**
* Custom attributes to append to the PO file header
*
* @default {}
*/
customHeaderAttributes?: { [key: string]: string }
}
```
Expand Down
21 changes: 21 additions & 0 deletions packages/format-po/src/po.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,25 @@ describe("pofile format", () => {
`)
})

it("should include custom header attributes", () => {
const format = createFormatter({
customHeaderAttributes: { "X-Custom-Attribute": "custom-value" },
})
const catalog: CatalogType = {}
const actual = format.serialize(catalog, defaultSerializeCtx)

expect(actual).toMatchInlineSnapshot(`
msgid ""
msgstr ""
"POT-Creation-Date: 2018-08-27 10:00+0000\\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=utf-8\\n"
"Content-Transfer-Encoding: 8bit\\n"
"X-Generator: @lingui/cli\\n"
"Language: en\\n"
"X-Custom-Attribute: custom-value\\n"
`)
})
})
17 changes: 15 additions & 2 deletions packages/format-po/src/po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,30 @@ export type PoFormatterOptions = {
* @default false
*/
explicitIdAsDefault?: boolean
/**
* Custom attributes to append to the PO file header
*
* @default {}
*/
customHeaderAttributes?: { [key: string]: string }
}

function isGeneratedId(id: string, message: MessageType): boolean {
return id === generateMessageId(message.message, message.context)
}

function getCreateHeaders(language: string): PO["headers"] {
function getCreateHeaders(
language: string,
customHeaderAttributes: PoFormatterOptions["customHeaderAttributes"]
): PO["headers"] {
return {
"POT-Creation-Date": formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx"),
"MIME-Version": "1.0",
"Content-Type": "text/plain; charset=utf-8",
"Content-Transfer-Encoding": "8bit",
"X-Generator": "@lingui/cli",
...(language ? { Language: language } : {}),
...(customHeaderAttributes ?? {}),
}
}

Expand Down Expand Up @@ -198,7 +208,10 @@ export function formatter(options: PoFormatterOptions = {}): CatalogFormatter {
po = PO.parse(ctx.existing)
} else {
po = new PO()
po.headers = getCreateHeaders(ctx.locale)
po.headers = getCreateHeaders(
ctx.locale,
options.customHeaderAttributes
)
// accessing private property
;(po as any).headerOrder = Object.keys(po.headers)
}
Expand Down

0 comments on commit d90f778

Please sign in to comment.