Repository navigation
feat(vue): add @lingui/vue, Vue 3 bindings with template macros - #2732
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
andrii-bodnar
left a comment
There was a problem hiding this comment.
@yves-riot thanks a lot for this, it’s a big and very complete piece of work, and running it in production for a month is a good sign. We’d like to have Vue support in Lingui itself!
It’s behind main now and has conflicts, so it needs a rebase. Two more minor comments. After that I’ll go through it in more detail.
947c714 to
c85bc1e
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2732 +/- ##
===========================================
+ Coverage 77.05% 90.05% +12.99%
===========================================
Files 84 147 +63
Lines 2157 5267 +3110
Branches 555 1624 +1069
===========================================
+ Hits 1662 4743 +3081
- Misses 382 468 +86
+ Partials 113 56 -57 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
andrii-bodnar
left a comment
There was a problem hiding this comment.
@yves-riot thanks again, I went through the whole package with the first release in mind. The code side looks ready. #2729 just landed, so this needs a rebase first, and a few things in the docs need fixing before we can merge:
- Conflicts in
extractor-vue. Keep both sides: the newextractoroption next totemplateandcompilerOptions, andextractTargetshould call the configured extractor (Babel by default) instead of the imported one. - The docs site fails on broken links.
- The "equivalent" config under
defineConfiginref/vue.mdis missingjsxRuntime: "vue", so anyone copying it gets broken template components.
Thinking out loud: with the new extractor option, someone may pass the SWC extractor in a @lingui/vue project. The native macro doesn't know jsxRuntime: "vue" yet, so that combination will not work today. Not something for this PR, we can handle it on the native-tools side.
Add `@lingui/vue`, a Vue 3 binding with the same features as `@lingui/react` and `@lingui/solid`: - `I18nProvider`, `useLingui`, `Trans`, `TransNoContext` and a `createLingui` app plugin - `@lingui/vue/macro`: `Trans`, `Plural`, `Select`, `SelectOrdinal` and `useLingui` macros for `<script setup>` and JSX/TSX - `@lingui/vue/compiler`: a Vue compiler node transform that compiles the macros inside `<template>` blocks (`<Trans>`, `<Plural>`, `<Select>`, `<SelectOrdinal>`, and `t`, `plural`, `select`, `selectOrdinal`, `ph` in expressions), with named Vue slots as element placeholders - `@lingui/vue/vite`: registers the template transform in `@vitejs/plugin-vue` and runs the Babel macro on `.vue`, `.js`, `.ts`, `.jsx` and `.tsx` modules - `@lingui/vue/config`: `defineConfig` preset for Vue projects Vue 3.4+ is required. The package structure, tests and docs follow the Solid integration (lingui#2510), adapted to Vue (`shallowRef` + `triggerRef` instead of signals) and VNodes. The template transform is the Vue-specific addition: templates are not Babel input, so the macros need a compiler-level transform to reach them. Bare `Trans` renders always produce a Fragment so server-rendered translations hydrate next to sibling text without mismatches. Changes outside `@lingui/vue`: - `@lingui/conf`: `macro.jsxRuntime` accepts `"vue"`. - `@lingui/babel-plugin-lingui-macro`: - when `jsxRuntime` is `"vue"`, non-JSX references of macro components are redirected to the runtime `Trans`, so `@lingui/vue/macro` is the single import for templates. - `useLingui` destructuring is resolved at module scope when there is no enclosing function (raw `<script setup>` code during extraction). - `@lingui/extractor-vue`: - applies the template transform so messages in `<template>` are extracted with the same IDs as at runtime. Template compile errors now fail extraction; previously they were silently ignored and extraction succeeded with a missing or hashed ID. - new `template` and `compilerOptions` options; `lang="tsx"` blocks are parsed as TypeScript; template placeholders drop the `_ctx.`/`$setup.` prefix. - `reactivityTransform` defaults to `true` when `macro.jsxRuntime` is `"vue"`, so destructured props get the same message IDs as at runtime. - `@lingui/vue` is an optional peer dependency, needed only for the template macros (which also need `@vue/compiler-sfc` 3.4+, checked at extraction). Without it, templates are compiled without the transform, so Vue 3.2 and 3.3 projects keep working. - `@lingui/vite-plugin`: the native `macroTransform` fails early with `macro.jsxRuntime: "vue"`, as it already does for `"solid"` (lingui#2708). Vue projects transform macros with the Babel plugin through `@lingui/vue/vite`. - `README.md`: mentions `@lingui/vue`; `package.json`: size-limit entry for `@lingui/vue`. Includes tests, docs and a `vite-project-vue` example app.
- vite-plugin: only reject macro.jsxRuntime "vue" in the native macro transform, Solid is now supported (lingui#2731) - extractor-vue: keep remapping <script setup> message positions to the .vue file (lingui#2728) through the extractTarget helper - docs: limit the native transform caution to Vue
Use the same placeholder grammar as @lingui/react, which follows the macro charset since lingui#2676, so `<terms_link>` extracted by the macro also renders.
- Add `jsxRuntime: "vue"` to the manual equivalent of `defineConfig` - Point the macro plugin link to the Babel setup in Installation - Fix the `macro.jsxPlaceholder*` and `msg` anchors - Format the Vue docs, keeping the template examples as written
|
@andrii-bodnar Thanks! It should be good now |
Co-authored-by: Andrii Bodnar <29282228+andrii-bodnar@users.noreply.github.com>
|
@yves-riot thank you! Could you please also bump the package version to 6.10.1? |
Done! |
andrii-bodnar
left a comment
There was a problem hiding this comment.
@yves-riot thank you for the contribution! 🚀
|
@yves-riot I just released the package - https://www.npmjs.com/package/@lingui/vue It does not have provenance yet, but it should work. The next version will be released through the regular pipeline and will have trusted publishing and provenance. |
Thank you! |
|
Very cool, thank you! Having this feature implemented, I recommended Lingui for Slidev i18n feature: slidevjs/slidev#1125 (comment) |
Following #1730
Description
Add
@lingui/vue, a Vue 3 binding with the same features as@lingui/reactand@lingui/solid:I18nProvider,useLingui,Trans,TransNoContextand acreateLinguiapp plugin@lingui/vue/macro:Trans,Plural,Select,SelectOrdinalanduseLinguimacros for<script setup>and JSX/TSX@lingui/vue/compiler: a Vue compiler node transform that compiles the macros inside<template>blocks (<Trans>,<Plural>,<Select>,<SelectOrdinal>, andt,plural,select,selectOrdinal,phin expressions), with named Vue slots as element placeholders@lingui/vue/vite: registers the template transform in@vitejs/plugin-vueand runs the Babel macro on.vue,.js,.ts,.jsxand.tsxmodules@lingui/vue/config:defineConfigpreset for Vue projectsVue 3.4+ is required.
The package structure, tests and docs follow the Solid integration (#2510), adapted to Vue (
shallowRef+triggerRefinstead of signals) and VNodes. The template transform is the Vue-specific addition: templates are not Babel input, so the macros need a compiler-level transform to reach them.Bare
Transrenders always produce a Fragment so server-rendered translations hydrate next to sibling text without mismatches.Changes outside
@lingui/vue:@lingui/conf:macro.jsxRuntimeaccepts"vue".@lingui/babel-plugin-lingui-macro:jsxRuntimeis"vue", non-JSX references of macro components are redirected to the runtimeTrans, so@lingui/vue/macrois the single import for templates.useLinguidestructuring is resolved at module scope when there is no enclosing function (raw<script setup>code during extraction).@lingui/extractor-vue:<template>are extracted with the same IDs as at runtime. Template compile errors now fail extraction; previously they were silently ignored and extraction succeeded with a missing or hashed ID.templateandcompilerOptionsoptions;lang="tsx"blocks are parsed as TypeScript; template placeholders drop the_ctx./$setup.prefix.reactivityTransformdefaults totruewhenmacro.jsxRuntimeis"vue", so destructured props get the same message IDs as at runtime.@lingui/vueis an optional peer dependency, needed only for the template macros (which also need@vue/compiler-sfc3.4+, checked at extraction). Without it, templates are compiled without the transform, so Vue 3.2 and 3.3 projects keep working.@lingui/vite-plugin: the nativemacroTransformfails early withmacro.jsxRuntime: "vue", as it already does for"solid"(fix(vite-plugin): fail early when native transform is used with Solid #2708). Vue projects transform macros with the Babel plugin through@lingui/vue/vite.README.md: mentions@lingui/vue;package.json: size-limit entry for@lingui/vue.Includes tests, docs and a
vite-project-vueexample app.Types of changes
Checklist