Skip to content

Commit

Permalink
refactor(cli/catalog): decouple catalog type from Po message shape
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Mar 22, 2023
1 parent 94a7e3c commit 3e2f749
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 319 deletions.
400 changes: 235 additions & 165 deletions packages/cli/src/api/__snapshots__/catalog.test.ts.snap

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ describe("Catalog", () => {
expect(messages).toMatchInlineSnapshot(`
{
xDAtGP: {
comments: [],
context: undefined,
extractedComments: [],
message: Message,
origin: [
[
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/api/catalog/extractFromFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function extractFromFiles(
messages[next.id] = {
message: next.message,
context: next.context,
extractedComments: [],
comments: [],
origin: [],
}
}
Expand All @@ -50,9 +50,9 @@ export async function extractFromFiles(

messages[next.id] = {
...prev,
extractedComments: next.comment
? [...prev.extractedComments, next.comment]
: prev.extractedComments,
comments: next.comment
? [...prev.comments, next.comment]
: prev.comments,
origin: [...prev.origin, [filename, next.origin[1]]],
}
},
Expand Down
20 changes: 12 additions & 8 deletions packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,28 @@ export type ExtractorCtx = {
linguiConfig: LinguiConfigNormalized
}

type CatalogExtra = Record<string, unknown>
export type MessageOrigin = [filename: string, line?: number]
export type ExtractedMessageType = {
export type ExtractedMessageType<Extra = CatalogExtra> = {
message?: string
origin?: MessageOrigin[]
comments?: string[]
extractedComments?: string[]
obsolete?: boolean
flags?: string[]
context?: string
/**
* the generic field where
* formatters can store additional data
*/
extra?: Extra
}
export type MessageType = ExtractedMessageType & {
export type MessageType<Extra = CatalogExtra> = ExtractedMessageType<Extra> & {
translation: string
}
export type ExtractedCatalogType = {
[msgId: string]: ExtractedMessageType
export type ExtractedCatalogType<Extra = CatalogExtra> = {
[msgId: string]: ExtractedMessageType<Extra>
}
export type CatalogType = {
[msgId: string]: MessageType
export type CatalogType<Extra = CatalogExtra> = {
[msgId: string]: MessageType<Extra>
}

export type ExtractorType = {
Expand Down
10 changes: 1 addition & 9 deletions packages/format-json/src/__snapshots__/json.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,18 @@ exports[`json format style: lingui should write catalog in lingui format 1`] = `
},
"withDescription": {
"translation": "Message with description",
"extractedComments": [
"comments": [
"Description is comment from developers to translators"
]
},
"withComments": {
"comments": [
"Translator comment",
"This one might come from developer"
],
"translation": "Support translator comments separately"
},
"obsolete": {
"translation": "Obsolete message",
"obsolete": true
},
"withFlags": {
"flags": [
"fuzzy",
"otherFlag"
],
"translation": "Keeps any flags that are defined"
},
"veryLongString": {
Expand Down
18 changes: 2 additions & 16 deletions packages/format-json/src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,16 @@ describe("json format", () => {
},
withDescription: {
translation: "Message with description",
extractedComments: [
"Description is comment from developers to translators",
],
comments: ["Description is comment from developers to translators"],
},
withComments: {
comments: [
"Translator comment",
"This one might come from developer",
],
translation: "Support translator comments separately",
},
obsolete: {
translation: "Obsolete message",
obsolete: true,
},
withFlags: {
flags: ["fuzzy", "otherFlag"],
translation: "Keeps any flags that are defined",
},
veryLongString: {
Expand Down Expand Up @@ -186,23 +179,16 @@ describe("json format", () => {
},
withDescription: {
translation: "Message with description",
extractedComments: [
"Description is comment from developers to translators",
],
comments: ["Description is comment from developers to translators"],
},
withComments: {
comments: [
"Translator comment",
"This one might come from developer",
],
translation: "Support translator comments separately",
},
obsolete: {
translation: "Obsolete message",
obsolete: true,
},
withFlags: {
flags: ["fuzzy", "otherFlag"],
translation: "Keeps any flags that are defined",
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ exports[`po-gettext format convertPluralsToIco handle correctly locales with 4-l
WGI12K: {
comments: [],
context: null,
extractedComments: [],
flags: [],
extra: {
flags: [],
translatorComments: [],
},
message: {anotherCount, plural, one {Singular case} other {Case number {anotherCount}}},
obsolete: false,
origin: [],
Expand All @@ -15,8 +17,10 @@ exports[`po-gettext format convertPluralsToIco handle correctly locales with 4-l
jO/SBZ: {
comments: [],
context: null,
extractedComments: [],
flags: [],
extra: {
flags: [],
translatorComments: [],
},
message: {count, plural, one {Singular} other {Plural}},
obsolete: false,
origin: [],
Expand All @@ -25,23 +29,27 @@ exports[`po-gettext format convertPluralsToIco handle correctly locales with 4-l
message_with_id: {
comments: [],
context: null,
extractedComments: [],
flags: [
explicit-id,
],
extra: {
flags: [
explicit-id,
],
translatorComments: [],
},
obsolete: false,
origin: [],
translation: {someCount, plural, one {Singular case} other {Case number {someCount}}},
},
message_with_id_but_without_translation: {
comments: [],
context: null,
extractedComments: [
comments: [
Comment made by the developers.,
],
flags: [
explicit-id,
],
context: null,
extra: {
flags: [
explicit-id,
],
translatorComments: [],
},
obsolete: false,
origin: [],
translation: ,
Expand Down Expand Up @@ -106,8 +114,10 @@ exports[`po-gettext format should convert gettext plurals to ICU plural messages
WGI12K: {
comments: [],
context: null,
extractedComments: [],
flags: [],
extra: {
flags: [],
translatorComments: [],
},
message: {anotherCount, plural, one {Singular case} other {Case number {anotherCount}}},
obsolete: false,
origin: [],
Expand All @@ -116,8 +126,10 @@ exports[`po-gettext format should convert gettext plurals to ICU plural messages
jO/SBZ: {
comments: [],
context: null,
extractedComments: [],
flags: [],
extra: {
flags: [],
translatorComments: [],
},
message: {count, plural, one {Singular} other {Plural}},
obsolete: false,
origin: [],
Expand All @@ -126,23 +138,27 @@ exports[`po-gettext format should convert gettext plurals to ICU plural messages
message_with_id: {
comments: [],
context: null,
extractedComments: [],
flags: [
explicit-id,
],
extra: {
flags: [
explicit-id,
],
translatorComments: [],
},
obsolete: false,
origin: [],
translation: {someCount, plural, one {Singular case} other {Case number {someCount}}},
},
message_with_id_but_without_translation: {
comments: [],
context: null,
extractedComments: [
comments: [
Comment made by the developers.,
],
flags: [
explicit-id,
],
context: null,
extra: {
flags: [
explicit-id,
],
translatorComments: [],
},
obsolete: false,
origin: [],
translation: ,
Expand Down
32 changes: 18 additions & 14 deletions packages/format-po-gettext/src/po-gettext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("po-gettext format", () => {
and linebreak} other {Case number {someCount} with id}}",
translation:
"{someCount, plural, one {Singular case with id} other {Case number {someCount} with id}}",
extractedComments: [
comments: [
"This is a comment by the developers about how the content must be localized.",
],
},
Expand Down Expand Up @@ -131,19 +131,23 @@ msgstr[2] "# dní"

const parsed = format.parse(po, defaultParseCtx)

expect(parsed).toEqual({
Y8Xw2Y: {
// Note that the last case must be `other` (the 4th CLDR case name) instead of `many` (the 3rd CLDR case name).
translation: "{#, plural, one {# den} few {# dny} other {# dní}}",
message: "{#, plural, one {day} other {days}}",
extractedComments: [],
context: null,
comments: [],
obsolete: false,
origin: [],
flags: [],
},
})
// Note that the last case must be `other` (the 4th CLDR case name) instead of `many` (the 3rd CLDR case name).
expect(parsed).toMatchInlineSnapshot(`
{
Y8Xw2Y: {
comments: [],
context: null,
extra: {
flags: [],
translatorComments: [],
},
message: {#, plural, one {day} other {days}},
obsolete: false,
origin: [],
translation: {#, plural, one {# den} few {# dny} other {# dní}},
},
}
`)
})

describe("when using 'select' format", () => {
Expand Down
Loading

0 comments on commit 3e2f749

Please sign in to comment.