Skip to content

Commit

Permalink
fix: stripping origin if line numbers already strippped (#1143)
Browse files Browse the repository at this point in the history
  • Loading branch information
luludan authored Oct 4, 2021
1 parent 5c5c8a7 commit 82a3265
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const LOCALE = "{locale}"
const LOCALE_SUFFIX_RE = /\{locale\}.*$/
const PATHSEP = "/" // force posix everywhere

type MessageOrigin = [string, number]
type MessageOrigin = [string, number?]

export type ExtractedMessageType = {
message?: string
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/formats/po-gettext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const serialize = (items: CatalogType, options) =>

if (options.origins !== false) {
if (message.origin && options.lineNumbers === false) {
item.references = message.origin.map(msg => msg.slice(0, -1)).map(joinOrigin)
item.references = message.origin.map(([path]) => path);
} else {
item.references = message.origin ? message.origin.map(joinOrigin) : []
}
Expand Down
56 changes: 56 additions & 0 deletions packages/cli/src/api/formats/po.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,60 @@ describe("pofile format", () => {
`)
})

it("should not include lineNumbers if lineNumbers option is false and already excluded", () => {
mockFs({
locale: {
en: mockFs.directory(),
},
})

const filename = path.join("locale", "en", "messages.po")
const catalog: CatalogType = {
static: {
translation: "Static message",
},
withOrigin: {
translation: "Message with origin",
origin: [["src/App.js"]],
},
withMultipleOrigins: {
translation: "Message with multiple origin",
origin: [
["src/App.js"],
["src/Component.js"],
],
},
}
format.write(filename, catalog, {
origins: true,
lineNumbers: false,
locale: "en",
})
const pofile = fs.readFileSync(filename).toString()
mockFs.restore()
expect(pofile).toMatchInlineSnapshot(`
msgid ""
msgstr ""
"POT-Creation-Date: ${formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx")}\\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"
msgid "static"
msgstr "Static message"
#: src/App.js
msgid "withOrigin"
msgstr "Message with origin"
#: src/App.js
#: src/Component.js
msgid "withMultipleOrigins"
msgstr "Message with multiple origin"
`)
})
})
2 changes: 1 addition & 1 deletion packages/cli/src/api/formats/po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const serialize = (items: CatalogType, options) =>
item.extractedComments = message.extractedComments || []
if (options.origins !== false) {
if (message.origin && options.lineNumbers === false) {
item.references = message.origin.map(msg => msg.slice(0, -1)).map(joinOrigin)
item.references = message.origin.map(([path]) => path);
} else {
item.references = message.origin ? message.origin.map(joinOrigin) : []
}
Expand Down

1 comment on commit 82a3265

@vercel
Copy link

@vercel vercel bot commented on 82a3265 Oct 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.