-
Notifications
You must be signed in to change notification settings - Fork 390
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(format): initial support for external formatters #1511
feat(format): initial support for external formatters #1511
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
size-limit report 📦
|
e3a2261
to
39b9a2d
Compare
39b9a2d
to
86cce94
Compare
86cce94
to
294d3de
Compare
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## next #1511 +/- ##
==========================================
+ Coverage 74.55% 75.75% +1.20%
==========================================
Files 76 79 +3
Lines 1965 1980 +15
Branches 515 516 +1
==========================================
+ Hits 1465 1500 +35
+ Misses 389 360 -29
- Partials 111 120 +9
... and 1 file with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
@thekip thank you! How do you see the implementation of new formats? It would be great if it allow the creation of separate shareable packages for different formats that can be installed via npm and then used by Lingui. In that way, every developer will be able to implement their own formatter and share it with other developers. |
Exactly like you described. Even more, i'm going to extract all existing formats from The only This is slightly reduce installation size, because if you don't use CSV formatter you don't have to install papaparse. It also will help to gather some kinda statistics which packages people uses and which not. For migration period there would be a warning // lingui.config.js
{
format: 'minimal'
}
Something like this. Also if you want to specify some formatter parameters you have to do it in the factory function instead of // lingui.config.js
import formatter from "@lingui/formatter-minimal"
{
locales: ['en', 'pl'],
format: formatter({myAwesomeOption: true})
} |
Sounds great! I think formatters should have access to as much contextual information about the string as possible. If needed, it will allow the generation of quality localization files with all the possible context for translators |
Made formatters work in the async context. (check last commit) Also reanimated and fixed tests for webpack loader, which was ignored. |
26e20ef
to
f1626e7
Compare
Looking into current formatters api i'm wondering about correctness and responsibilities. Currently, the api is It means that each formatter is responsible for two things:
I think it might be incorrect. Responsibility of formatter should be only first point from the list. So API should looks like as This way all heavy lifting related to work with a FS would be out of the formatters reponsibilities. @andrii-bodnar what do you think? |
But there are some tricky logic in // po formatter
async write(filename, catalog, ctx) {
let po: PO
const raw = await readFile(filename)
if (raw) {
po = PO.parse(raw)
} else {
po = new PO()
po.headers = getCreateHeaders(ctx.locale)
if (ctx.locale === undefined) {
delete po.headers.Language
}
// accessing private property
;(po as any).headerOrder = Object.keys(po.headers)
}
po.items = serialize(catalog, options)
await writeFileIfChanged(filename, po.toString())
}, or const messages = serialize(catalog)
let file = await readFile(filename)
const shouldUseTrailingNewline = file === null || file?.endsWith("\n")
const trailingNewLine = shouldUseTrailingNewline ? "\n" : ""
await writeFile(
filename,
`${JSON.stringify(messages, null, 2)}${trailingNewLine}`
) Anyway, i'm done with this PR. |
@thekip I also think that working with FS definitely is not formatter's responsibility. Ideally, the formatter should be responsible only for parsing and serializing, and reading/writing should happen outside. This will also reduce code duplication and the developer of the custom formatter will not care about the FS, only for the parsing and building of the file. |
d53b62b
to
ebaaf0c
Compare
@andrii-bodnar please, merge |
Finally, was able to make it (see last commit):
|
I'd like to review it in more detail, will merge it tomorrow |
test suite should be rerun |
kinda flaky test, looks like depending on something in host system glob may return results in one order or another. That will produce slightly different cli output which is not matched to snapshot |
fixed flaky test |
Description
Preparation for external formatters.
Currently, all extractors required in the CLI eagerly. It means no matter do you use CSV or po-gettext their dependency are loaded to the memory and affect cold-start time of CLI command. (and actually all bundler loaders plugins as well)
So the first point was to make them loading lazily.
This quickly revealed a problem with
mock-fs
in tests.If during execution of a command
require()
calls happened, they would fail because FS is mocked and no such file to require found. So it's breaking the code.In this PR i got rid of mock-fs in tests which were affected by this problem.
And add initial support for external formatters.
Notes:
Before we can release "externalFormatters" for public it's important to make formatters work in the async context. That will change their shape, obviously. I'm going to do this in next iteration to keep this PR small.
Docs would be written when final shape of formatter would stabilized.
Types of changes
Fixes # (issue)
Checklist