Quick Start

Fastify

This page demonstrates the minimal setup of Intor in a Fastify project.


Installation

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

The Fastify integration of Intor is built on fastify-plugin.
Install it first:

npm install fastify-plugin
yarn add fastify-plugin
pnpm add fastify-plugin
bun add fastify-plugin

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 Fastify application and register intorFastifyPlugin.

index.ts
U
import Fastify from "fastify";
import { intorFastifyPlugin } from "intor/fastify";
import { intorConfig } from "./intor-config";

const fastify = Fastify();

fastify.register(intorFastifyPlugin, { config: intorConfig });

fastify.listen({ port: 3000 }, function (err, address) {
  console.log(`Server is now listening on ${address}`);
});

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/fastify";

// ...

fastify.get("/", async function (request, reply) {
  const { t, tRich } = await getTranslator(intorConfig, request);

  reply.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 request, though type inference is limited in this mode.

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