Skip to content
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

Message files splitting #503

Closed
3 tasks
tricoder42 opened this issue May 17, 2019 · 17 comments
Closed
3 tasks

Message files splitting #503

tricoder42 opened this issue May 17, 2019 · 17 comments

Comments

@tricoder42
Copy link
Contributor

tricoder42 commented May 17, 2019

It seems that one unsolved problem in JavaScript internationalization is automatic splitting of message files, similar to code splitting.

I would like to tackle this problem in v3 and figure out, what API changes we need to make to get it done.

Related issues: #474 #362


Code splitting creates main bundle with loader and then smaller chunks, which can be loaded separately. We know, what modules are included in each chunk (webpack stats data) and we know, what messages are included in each module (origin of message in lingui catalogs). Therefore we can determine what messages are included in each chunk and generate message files for each chunk.

As with code splitting, even locales need to have main bundle, which will include locale specific data like plural rules, date/number formats, etc.

The only question is, how to load chunks of message files.

Webpack handles loading of code chunks automagically (== I have no idea how it works). When we type import "module" and the module isn't loaded yet, webpack automatically loads the correct chunk.

We can't do that with locales. Usually, we would write import "locale/en.json", but that would load whole catalog. In fact, we need just import "locale/en.acf422.json", but that filename is generated automatically. We probably need to hook into webpack require and when chunk xyz is loaded, then locale locale/en.xyz.json must be loaded as well.

Finally, the last piece in puzzle - once we have locale chunks loaded automatically, we need to load them into Lingui object.


Notes

While trying to figure out how to load message catalog automatically with code chunk, I discovered next-client-pages-loader from Next.js, which injects some extra code to generated chunk.

We need to either override default webpack require or call another require before/after chunk is loaded, so adding extra code to generted bundle might be related.


Checklist:

  • Generate locale files for each chunk
  • Figure out, how to hook into webpack's require and load locale chunk along with code chunks
  • Figure out how to feed loaded locale chunks into Lingui
@theKashey
Copy link
Contributor

  1. ✅Extract messages from the source code.
  2. ✅Build the app
  3. Add plugin, tap on the compilation emit, to analyze how modules are distributed in chunks ( stat.json)
  4. Form the same "chunks" from translated messages, these translations-per-chunks could be added into webpack emit as well.
  5. Add another plugin, to hook into __webpack_require__.e(requireEnsure) (like mini-css-extract-plugin does) to load translation the translation chunk next to the code chunk.

@theKashey
Copy link
Contributor

theKashey commented Nov 20, 2019

  • selected for development :)

8 months later... 🐌 🐢

@stale
Copy link

stale bot commented Oct 29, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Oct 29, 2020
@renchap
Copy link
Contributor

renchap commented Oct 29, 2020

Still valid I think :)

@stale stale bot removed the wontfix label Oct 29, 2020
@semoal
Copy link
Contributor

semoal commented Oct 29, 2020

Probably this feature could be a nice one on a 3.1.0 of lingui, @tricoder42 if you have some time to open other issue to write a little roadmap of 3.1.0, I could help as much as I can 👍🏻

@tricoder42
Copy link
Contributor Author

@semoal I've just created a milestone and added two new possible features.

This issue isn't trivial because it depends on the framework. I would start with Next.js because it's closer to me. Just clarify — we're talking about splitting the compiled catalog, not the source one. We can already split the source catalog using catalogs attribute and, to be hones, even one large source catalog isn't an issue. The problem is when you send such large catalog to the client, which potentially might explode the bundle size.

It would be great, if we could split the catalog based on generated chunks, but I have no idea how to hook into Webpack. @ScriptedAlchemy mentioned module federation yesterday.

@hectorcoronado
Copy link

hectorcoronado commented Oct 30, 2020

  • Open another issue

@semoal
Copy link
Contributor

semoal commented Nov 15, 2020

Hi @theKashey , since we have right now version 3.X.X. really clean, we released some interesting new features and some hot-fixes so probably the next milestones are reasonably clean.

If you can provide some details of what you got, I'm open to help you or start working on it. This feature will be amazing.

@semoal
Copy link
Contributor

semoal commented Nov 15, 2020

There's something I'm not getting here, clearly, and I'm wondering if you could please provide some insight?

Feel free to open another issue if you found something not working correctly 👍🏻

@theKashey
Copy link
Contributor

@semoal - all I've got is in the comment above. The general idea is to extract "translations" in the same way some CSS-in-JS do a static extraction, producing CSS files from js ones.
The only difference that such files have to pull translation from the other source and (optional) support runtime language selection.

@micahbule
Copy link

Any particular update on this if we can already do this or if there's any existing workaround?

@JSteunou
Copy link
Contributor

JSteunou commented Mar 16, 2021

until having it all automatic hooked on build split feature from webpack or other builders, could it be possible to add this feature through a naive implementation with namespace concept. On every call to Trans / t adding a namespace parameter which will be used to split the catalogue files.

To avoid boilerplate maybe some configuration could be added to automatically set this namespace for all files of a folder / folders / glob pattern.

Loading the appropriate catalogue would still be manual though but nothing too difficult nor painful...

What do you think?

@semoal
Copy link
Contributor

semoal commented Mar 18, 2021

It's a good idea if you want to collaborate @JSteunou, we're open to collaborations.

@JSteunou
Copy link
Contributor

My hands are full but if at some point we are in the need of this feature at work I might sell it to be able to work on it on work time. But that could take years because the todo list is already huge so if someone want to tackle it before, go.

@SanichKotikov
Copy link

Hi,

Is there any way to extract messages separately for different pages(folders)? For example, I have next project structure:

/src
  /_common
  /page1
  /page2
  /...

and I want to get next jsons (with messages):

/locales
  /_common
    /en.json
    /fr.json
  /page1
    /en.json
    /fr.json
  /page2
    /en.json
    /fr.json

@stale
Copy link

stale bot commented Jun 19, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 19, 2021
@stale stale bot closed this as completed Jun 26, 2021
@JSteunou
Copy link
Contributor

nooo stale bot please keep it up keep it up :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants