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-pluginyarn add fastify-pluginpnpm add fastify-pluginbun add fastify-plugin
Install Intor:
npm install intoryarn add intorpnpm add intorbun add intor
Project Structure
index.jsonUindex.jsonUindex.tsUintor-config.tsU
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.jsonUindex.jsonU
{ "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.tsU
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.tsU
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 texttRich: renders structured messages with semantic tags
index.tsM
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>` }), }); });