compiler@1.3.0
Overview
The GT compiler now supports auto JSX injection for client-side React SPAs. It automatically wraps translatable JSX text in <T> components at build time, so you don't have to.
Previously, every translatable string needed a manual <T> wrapper. With enableAutoJsxInjection, the compiler handles this for you. Write plain JSX; the compiler does the rest.
| Package | Version |
|---|---|
@generaltranslation/compiler | 1.3.0 |
gt-react | 10.17.0 |
gt-next | 6.15.2 |
Setup
Auto JSX injection builds on the bootstrap-based SPA architecture introduced with the t() function. You need to set up the bootstrap entry point first, then add the compiler plugin.
1. Bootstrap entry point
Create a bootstrap.tsx that loads translations before your app renders:
// bootstrap.tsx (new entry point)
import gtConfig from '../gt.config.json';
import { bootstrap } from 'gt-react/browser';
async function loadTranslations(locale: string) {
return (await import(`./_gt/${locale}.json`)).default;
}
await bootstrap({
...gtConfig,
loadTranslations,
});
await import('./main.tsx');Update your index.html to point to bootstrap.tsx instead of main.tsx.
2. Install the compiler
npm install @generaltranslation/compiler3. Vite
// vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import gtConfig from './gt.config.json'
import { vite as gtCompiler } from '@generaltranslation/compiler'
export default defineConfig({
plugins: [react(), gtCompiler({gtConfig})],
})4. gt.config.json
Enable auto JSX injection in your config:
{
"files": {
"gt": {
"output": "public/_gt/[locale].json",
"parsingFlags": {
"enableAutoJsxInjection": true
}
}
}
}What it does
Before — you had to wrap every translatable string manually:
import { T } from "gt-react/browser";
export default function Hero() {
return (
<T>
<h1>Welcome to our app</h1>
</T>
);
}After — just write JSX:
export default function Hero() {
return <h1>Welcome to our app</h1>;
}The compiler injects the <T> wrappers at build time. No imports, no IDs, no boilerplate.
Variable injection
Dynamic expressions are automatically wrapped in <Var> too:
// You write:
export default function Greeting({ name }) {
return <p>Hello, {name}!</p>;
}
// The compiler produces:
import { T, Var } from "gt-react/browser";
export default function Greeting({ name }) {
return (
<T>
<p>Hello, <Var>{name}</Var>!</p>
</T>
);
}Links
- Compiler concepts
- compiler v1.0.0 devlog — original compiler release for React
- PR #1158