Quick Start

Express

This page demonstrates the minimal setup of Intor in an Express project.


Installation

This guide assumes that a Express project environment is already set up.
For initial project setup, see: Express

Install Intor:

npm install intor
yarn add intor
pnpm add intor
bun add intor

Project Structure

index.json
U

index.json
U

index.ts
U
intor-config.ts
U

Integration Steps

♯1 Messages

Create a messages directory in your project, and add a subdirectory for each locale.
Each locale provides an index.json file.

index.json
U

index.json
U
{
  "hello": "Hello, {name}",
  "rich": "A <tag>text</tag>."
}
{
  "hello": "Bonjour, {name}",
  "rich": "Un <tag>texte</tag>."
}

♯2 Config

Create intor-config.ts under src/.
This example uses the most basic translation loading approach by defining messages directly in the config.

For more details, see: Message Loading

intor-config.ts
U
import { defineIntorConfig } from "intor";
import EN from "../messages/en/index.json";
import FR from "../messages/fr/index.json";

export const intorConfig = defineIntorConfig({
  defaultLocale: "en",
  supportedLocales: ["en", "fr"],
  messages: {
    en: EN,
    fr: FR,
  },
});

♯3 Initialization

Create index.ts under src/.
Initialize the Express application and register Intor’s request handler.

index.ts
U
import express from "express";
import { createIntorHandler } from "intor/express";
import { intorConfig } from "./intor-config";

const app = express();

app.use(createIntorHandler(intorConfig));

app.listen(3000, () => {
  console.log(`Server is running on http://localhost:3000`);
});

Usage Example

Use getTranslator to access translation capabilities:

  • t: returns the resolved text
  • tRich: renders structured messages with semantic tags
index.ts
M
import { getTranslator } from "intor/express";

// ...

app.get("/", async function (req, res) {
  const { t, tRich } = await getTranslator(intorConfig, req);

  res.send({
    hello: t("hello", { name: "Intor" }),
    rich: tRich("rich", { tag: (children) => `<b>${children}</b>` }),
  });
});

// ...

As a convenience option, translation helpers are also available directly on req, though type inference is limited in this mode.

app.get("/", async function (req, res) {
  res.send({
    hello: req.t("hello", { name: "Intor" }),
    rich: req.tRich("rich", { tag: (children) => `<b>${children}</b>` }),
  });
});