-
Notifications
You must be signed in to change notification settings - Fork 394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): Extract - Flatten ICU messages #1431
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import extract, { ExtractedMessage } from "./extractors" | |
import { CliExtractOptions } from "../lingui-extract" | ||
import { CliExtractTemplateOptions } from "../lingui-extract-template" | ||
import { CompiledCatalogNamespace } from "./compile" | ||
import { flattenMessage } from "@lingui/core/flatten" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why it's in |
||
import { prettyOrigin } from "./utils" | ||
import chalk from "chalk" | ||
|
||
|
@@ -177,33 +178,38 @@ export class Catalog { | |
const fileSuccess = await extract( | ||
filename, | ||
(next: ExtractedMessage) => { | ||
if (!messages[next.id]) { | ||
messages[next.id] = { | ||
message: next.message, | ||
const nextId = options.flatten ? flattenMessage(next.id) : next.id | ||
const nextMessage = options.flatten | ||
? flattenMessage(next.message) | ||
: next.message | ||
|
||
if (!messages[nextId]) { | ||
messages[nextId] = { | ||
message: nextMessage, | ||
extractedComments: [], | ||
origin: [], | ||
} | ||
} | ||
|
||
const prev = messages[next.id] | ||
const prev = messages[nextId] | ||
|
||
const filename = path | ||
.relative(this.config.rootDir, next.origin[0]) | ||
.replace(/\\/g, "/") | ||
|
||
const origin: MessageOrigin = [filename, next.origin[1]] | ||
|
||
if (prev.message && next.message && prev.message !== next.message) { | ||
if (prev.message && nextMessage && prev.message !== nextMessage) { | ||
throw new Error( | ||
`Encountered different default translations for message ${chalk.yellow( | ||
next.id | ||
nextId | ||
)}` + | ||
`\n${chalk.yellow(prettyOrigin(prev.origin))} ${prev.message}` + | ||
`\n${chalk.yellow(prettyOrigin([origin]))} ${next.message}` | ||
`\n${chalk.yellow(prettyOrigin([origin]))} ${nextMessage}` | ||
) | ||
} | ||
|
||
messages[next.id] = { | ||
messages[nextId] = { | ||
...prev, | ||
extractedComments: next.comment | ||
? [...prev.extractedComments, next.comment] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/*i18n*/ i18n._("This is my {count, plural, one {#st} two {#nd} other {#rd}} cat.", { count }) | ||
/*i18n*/ i18n._("{gender, select, female {She} male {He} other {They}} added a new image to the system.", { gender }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* this is Rollup Entry | ||
*/ | ||
export * from "./src/compile" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* This entry is for old runtimes which do not support `exports` field in package.json | ||
* https://github.com/facebook/metro/issues/670 | ||
*/ | ||
module.exports = require("./build/cjs/flatten.js") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { mockConsole } from "@lingui/jest-mocks" | ||
import { flattenMessage as flatten } from "./flattenMessage" | ||
|
||
describe("flatten", () => { | ||
it("should flatten selectordinal", () => { | ||
const message = flatten( | ||
"It's my dog's {year, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} birthday!" | ||
) | ||
expect(message).toEqual( | ||
"{year, selectordinal, one {It's my dog's #st birthday!} two {It's my dog's #nd birthday!} few {It's my dog's #rd birthday!} other {It's my dog's #th birthday!}}" | ||
) | ||
}) | ||
|
||
it("should flatten plural", () => { | ||
const message = flatten( | ||
"I have {value, plural, one {{value} book} other {# books}}" | ||
) | ||
expect(message).toEqual( | ||
"{value, plural, one {I have {value} book} other {I have # books}}" | ||
) | ||
}) | ||
|
||
it("should flatten select with a placeholder in a previous sentence", () => { | ||
const message = flatten( | ||
"Hello, Your friend {friend} is now online. {gender, select, female {She} male {He} other {They}} added a new image!" | ||
) | ||
expect(message).toEqual( | ||
"{gender, select, female {Hello, Your friend {friend} is now online. She added a new image!} male {Hello, Your friend {friend} is now online. He added a new image!} other {Hello, Your friend {friend} is now online. They added a new image!}}" | ||
) | ||
}) | ||
|
||
it("should flatten plural with number", () => { | ||
const message = flatten( | ||
"You have {count, plural, one {{count, number} dog} other {{count, number} dogs}}" | ||
) | ||
expect(message).toEqual( | ||
"{count, plural, one {You have {count, number} dog} other {You have {count, number} dogs}}" | ||
) | ||
}) | ||
|
||
it("should throw error for invalid ICU message", () => { | ||
mockConsole((console) => { | ||
flatten("{foo, plural, =a{e1} other{baz}}") | ||
|
||
expect(console.error).toBeCalledWith( | ||
expect.stringContaining(`invalid syntax`) | ||
) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this, integration test become absolutely the same to the "regular" tests. The idea was to check that code works from ./build instead of ./src
What was wrong with that?