React langsys-js-react

A thin React binding over langsys-js-typescript, built on hooks. The phrase in your code is both the lookup key and the base-language default — no keys file, no extraction step.

Using an AI coding agent? Give it this prompt
Add Langsys translation to this React app.

1. Read https://docs.langsys.dev/llms.txt, then follow
   https://docs.langsys.dev/learn/sdk/react/ for the install and setup.
2. The English text in the code is the translation key. Wrap user-facing
   strings as they are: no keys file, no extraction step, and no
   hand-written plural or gender branches (Langsys generates those).
3. Read the Langsys project id and API key from environment variables.
   The write key is for development and servers only; anything shipped
   to the browser gets the read-only key.
4. Keep brand names and user-generated content out of translation.

Install

npm install langsys-js-react

No project or keys yet? The Quickstart gets you both in two minutes.

Initialize

import { LangsysApp, createLocaleStore } from 'langsys-js-react';

const locale = createLocaleStore('en-US');

await LangsysApp.init({
  projectid: import.meta.env.VITE_LANGSYS_PROJECT_ID,
  key: import.meta.env.VITE_LANGSYS_API_KEY, // read-only in the browser
  UserLocaleStore: locale,
});

Reading signals

Need the locale store — or any base-SDK signal, like a store you share outside React — read reactively? useSignal(store) subscribes and re-renders your component on change. (Svelte needs no equivalent: the signals natively satisfy its store contract.)

import { useSignal, useCurrentLocale } from 'langsys-js-react';

// React re-renders only on React state — it can't watch a base-SDK
// signal by itself. useSignal(signal) subscribes and re-renders on change.
function LocaleBadge() {
  const selected = useSignal(locale);   // your createLocaleStore(...)
  const loaded = useCurrentLocale();    // shortcut for the loaded locale

  return <footer>selected {selected} · loaded {loaded}</footer>;
}

Interactive playground

Edit the phrase and category, set the params as fields or raw JSON, then switch locale and watch the real SDK translate live.

Examples
Params
Output

Hello, Sarah! You have 3 new messages.

const t = useT();
t("Hello, {name}! You have {count} new messages.", "Greetings", {"name":"Sarah","count":3})
t() — inline string, in a component View source ↗
import { useT } from 'langsys-js-react';

function Greeting({ name }) {
  const t = useT();
  return <p>{t('Hello, {name}!', 'Greetings', { name })}</p>;
}

Which one do I use?

You’re translating… Use Why
A plain string in your code — a label, a title, a message. Params included; write counts flat and Langsys generates the plural forms. useT() Returns a string. The phrase in your code is both the lookup key and the base-language default.
One sentence containing inline markup or dynamic values, kept whole. <Phrase> Inline elements become neutral tokens, so translators reorder the full sentence — markup position included — and the real elements are reconstituted at render.
A region of markup with several phrases — a hero, a footer, a card. <Translate> One tag registers the region as a single content block: every text run is extracted as its own phrase and the markup structure is preserved.
Something that must never translate — brand names, code, IDs. <DontTranslate> The tokenizer and renderer skip the subtree; it stays verbatim while the sentence around it translates.

Rule of thumb: string → useT() · sentence with markup → <Phrase> · region of markup → <Translate> · untouchable → <DontTranslate>.

Each content component rendered live on the left; the code that produces it on the right.

Watch The demo cuts for these primitives are on the Svelte page for now — the surface is identical, only the syntax differs: <Translate> (1:04) · <Phrase> (1:06) · <DontTranslate> (0:34).

<Translate> — content block

Welcome to our store

Browse the catalog in your language.

<Translate> — content block View source ↗
import { Translate } from 'langsys-js-react';

<Translate category="Home">
  <h3>Welcome to our store</h3>
  <p>Browse the catalog in your language.</p>
</Translate>

<Phrase> — params & markup

Hi %name%, you have %count% items in your cart.
<Phrase> — params & markup View source ↗
import { Phrase } from 'langsys-js-react';

<Phrase category="Cart" params={{ name: 'Sarah', count: 3 }}>
  Hi %name%, you have %count% items in your cart.
</Phrase>

<DontTranslate> — never translated

Welcome! This sentence always stays in English. Thanks for visiting!

<DontTranslate> View source ↗
import { DontTranslate, Translate } from 'langsys-js-react';

// Inside a translated region, the marked span alone stays verbatim.
<Translate category="Tour">
  <p>Welcome! <DontTranslate>This sentence always stays in English.</DontTranslate> Thanks for visiting!</p>
</Translate>

Gender agreement

Spanish, French, or Russian inflect {username} has been invited for the person it’s about. Langsys generates those branches during translation — the same way it generates plurals — and the argument is named after the variable, with _gender appended: male, female, or other. Pass it whenever you know it. How it works, and how to find the phrases that need it →

Passing the gender argument
import { useT } from 'langsys-js-react';

function LikeNotice({ user }) {
  const t = useT();
  // Pass the agreement argument alongside the value it inflects.
  return <p>{t('{username} has been invited', 'Team', {
    username: user.name,
    username_gender: user.gender, // 'male' | 'female' | 'other'
  })}</p>;
}

Don’t know the gender? Pass 'other' explicitly — it’s the neutral branch. If it is missing anyway, the SDK takes the other branch for you (langsys-js-typescript 0.6.4 and later), so the sentence still reads correctly. TypeScript currently rejects username_gender in an inline params object (error TS2353; a fix is in review). Until it ships, build the params object first and pass the variable.

Server-side rendering

Fetch the catalog on the server and hand it to the client, so the SDK starts without fetching it again. The SDK translates in the browser, so the server-rendered HTML is in the base language until it starts.

// The server fetches the catalog; the client init starts from it
// instead of fetching it again, and translates once it runs.
LangsysApp.init({
  projectid, key,
  UserLocaleStore: locale,
  initialTranslations,        // fetched on the server
  initialTranslationsLocale,  // fetched on the server
  ssrTokenStrategy: 'client',
});
Search the docs & Learning Center