Back

compiler@1.3.0

Ernest McCarter avatarErnest McCarter
compilerauto-jsx-injectiongt-reacti18n

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.

PackageVersion
@generaltranslation/compiler1.3.0
gt-react10.17.0
gt-next6.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/compiler

3. 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>
  );
}