svelte-i18n-lingui icon indicating copy to clipboard operation
svelte-i18n-lingui copied to clipboard

Review of the plugin

Open thekip opened this issue 8 months ago • 5 comments

Hi @HenryLie, thanks for taking time to bring support for Svelte into Lingui. I quickly reviewed the docs and code and have some questions / suggestions:

It provides a macro that work for JS files to make the syntax much more succinct, but it doesn't work with modern Svelte projects that uses Vite as the bundler, since both Vite and Sveltekit expects libraries in ESM format. Lingui's macro depends on babel-plugin-macros to work, which doesn't seem to work well with Vite.

Svelte and Vite don't use Babel to transpile code, so even if babel-plugin-macros work we'll need to add an extra tool to do extra transpilation when compiling.

To use macro it indeed requires an additional compilation step. However there no issue with using macro with Vite using babel-macro or SWC. We have a couple working examples in the repo.

Using macro has few main advantages over "native" approach:

  1. Allowing to write a single message without splitting it into a chunks, therefore provide translator with more context.
  2. Allowing named placeholders in the messages: t`Hello {name}`
  3. Automatically drop compile time properties such as context, defaultMessage and generate id.
  4. Expand recursively other macro's in the message, such as plural.

So it's very beneficial to make macro work with svelte. I'm not particularly familiar with Svelte, but i can offer my help to achieve this from compiler / ast point of view.

I quickly checked svelte compiler options, and see that it supports "preprocess" param. I believe that could be useful to implement full macro support into Svelte code and template blocks.

thekip avatar Oct 16 '23 09:10 thekip