Repository navigation
Replies: 2 comments 1 reply
|
I like the idea, i've been thinking about that for some time. Glad that you were able to implement this for your project. But i'm not sure i understood how the Hot Locale Switching would work, could you elaborate on this more?
This based on assumption that you have a 1 global i18n object. That might be true for SPAs, but not always. I personally would love to see some PoC with this solution, because for me here a lot of gaps in understanding. |
|
Thanks for the detailed write-up, I like the idea too and the approach sounds reasonable. On where it should live, I'd rather keep it as an opt-in mode inside On the manifest and the core hook I don't have a strong opinion yet. Like Timofei said, a PoC would help a lot here, especially for how the dynamic switching would work in practice. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Motivation
When scaling a Lingui app, think: 20k messages across 80+ locales and 50+ code-split routes, teams hit a wall. Today, you have to pick your poison:
Keep one monolithic catalog (
locales/{locale}/messages.po)Manually split catalogs by directory/route (
billing.po,checkout.po)billing/tocheckout/,lingui extractdeletes the string frombilling.poand creates a brand-new untranslated string incheckout.po. Unless someone manually migrates.poentries, translations are silently lost on every refactor.(not implemented) RFC: Baking translations into bundle (zero runtime overhead) #2519
Proposal
We propose a Vite plugin that lets developers keep a single monolithic
.pocatalog in source control, while automatically splitting compiled messages 1:1 with Vite/Rollup output chunks at build time.How It Works Under the Hood
transform): During build, the plugin records which source files reference which Lingui message IDs.generateBundle): Once Rollup/Rolldown finalizes the chunk graph, the plugin inspectschunk.moduleIdsfor each JS chunk and emits a mini-catalog per locale containing only the messages used by that chunk.i18n.load()before the chunk renders.Runtime Loading
Because the generated chunk catalogs don't exist in source code, how does the browser load the right locale file at runtime?
Crucially, 95% of the plugin logic (AST tracking, chunk slicing, emitting catalog files) is identical. The only difference is the 2-line import header injected at the top of each chunk. By exposing a
strategyoption, we can support three distinct deployment models:1.
dynamic-import(Default: Best for Static SPAs & Hot Locale Switching)Injects a top-level
awaitcall that fetches the active locale's chunk catalog on demand:2.
import-map(Best for Modern SSR & Zero Waterfalls)Injects a bare import specifier resolved by an SSR-rendered
<script type="importmap">:modulepreloadfetches code and strings in parallel) and 100% immutable CDN caching (max-age=31536000, immutable). Requires page reload to switch languages.3.
virtual-path(Best for Legacy Browsers & Edge Routers)Injects a standard root-relative ESM import containing the chunk's content hash:
The server, load balancer, or CDN Edge Worker intercepts requests to
/__i18n/catalogs/*and serves the right locale file based on the request'sCookieorAccept-Languageheader.<script type="importmap">or top-levelawait, and avoids import-map collisions with tools like Module Federation. Including the.3c4d.jshash guarantees automatic cache-busting on every deploy.(Implementation note:
virtual-pathis less than 5 extra lines of code on top ofimport-map, as both simply emit an external static import with a different string prefix).Duplicated Strings Across Chunks
If we split a monolithic catalog 1:1 by chunk, common UI strings like
"Cancel","Save", or"Submit"will be repeated in multiple chunk catalogs. Is that a problem?We ran a benchmark using Vite 8 (Rolldown) simulating a 20k-message app across 50 chunks and 80 locales, where 40 common strings are repeated inside every single chunk:
Takeaway:
commoncatalog chunk in the future, the benchmark proves we don't need to overcomplicate the initial MVP: pure 1:1 chunk splitting is simple, fast, and overwhelmingly positive.Proposed Config API
Questions for Maintainers
@lingui/vite-plugin(e.g.,lingui({ splitByChunk: true })), or as a separate package (@lingui/vite-plugin-chunk-split)?import-mapandvirtual-path, should the plugin emit a standardlingui-manifest.jsonmapping(chunkName, locale) -> assetPathso SSR servers and edge routers can wire up preloading and routing automatically?dynamic-import, should@lingui/coreexpose a small helper (e.g.,i18n.registerChunkLoader(...)) so loaded chunks can automatically fetch their new locale catalog wheni18n.activate(newLocale)is called at runtime?Note: We have an internal working vite plugin that does the
import-mappart. We figured this could be of interest to others :)All reactions