# How do I translate a Node.js app?

50.6 s · Narrator: Frederick Surrey

Install gt-node, call initializeGT once at startup with your locales and a translation loader, wrap each request in withGT, and translate strings in your handlers with getGT. Then list your server files in gt.config.json and run npx gt translate to write a translation file for each language.

| Time | Voice | Line | On screen |
| --- | --- | --- | --- |
| 0:00.88 | Narrator | In Seville, a Spanish browser gets an English greeting from your server. | An app page draws in the middle of the frame. On "Spanish" the language code `es` appears over its top right corner: the language the reader's browser asks for. The heading "A Spanish browser gets an English greeting" rises on "a", and on "gets" the page's greeting "Hello, Alice!" rises over bars of copy. On "English" the greeting turns blue, and on "server" a white box draws around it. |
| 0:05.68 | Narrator | The handler returns a fixed English string for every request. | On "The" the page slides to the right, and on "handler" a code panel opens with `server.js`: a plain Express server whose `/api/greeting` route answers `res.json({ message: 'Hello, Alice!' })` and which listens on port 3000. On "fixed" the blue moves from the page to `'Hello, Alice!'` in the code, on "string" a line joins that row to the page's greeting, and on "request" a white box draws around the route. |
| 0:10.14 | Narrator | General Translation lets you translate strings into each request's locale with gt-node. | The answer card cuts in on "General": the answer alone rises as it is spoken, in two lines, "General Translation lets you translate strings" then "into each request's locale with gt-node", and the General Translation mark sits small in the lower left corner. |
| 0:16.10 | Narrator | Call initializeGT once, with your languages and a loader for the translation files. | The heading "Call initializeGT once", and the code panel opens again on the top of `server.js`. On "initializeGT" `import { readFile } from 'node:fs/promises';` and `import { initializeGT } from 'gt-node';` open, and on "once" an `initializeGT({ ... })` call opens. On "with" a file panel opens on the right with `package.json` and `server.js`. On "languages" `defaultLocale: 'en'` and `locales: ['en', 'es', 'fr', 'ja']` open inside the call, on "loader" a `loadTranslations` function opens that reads `./translations/${locale}.json` with `readFile` and parses it, and on "files" a white box draws around the file path. A blue bar marks each changed line. |
| 0:22.02 | Narrator | Wrap each request in withGT, using the language from its Accept-Language header. | The heading "Wrap each request in withGT", and the code panel shows `server.js` at `const app = express();`. On "each" `app.use((req, res, next) =>` opens under it, on "withGT" `withGT(getRequestLocale(req), () => next())` opens inside it, and on "using" `import { withGT, getRequestLocale } from 'gt-node';` opens above. On "Accept-Language" a white box draws around the `withGT` line. A blue bar marks each changed line. |
| 0:27.68 | Narrator | In a handler, await getGT, and pass each string to gt. | The heading "Await getGT in each handler", and the code panel shows the route in `server.js`. On "handler" `async` is typed into it, on "await" `const gt = await getGT();` opens, and on "getGT" `import { getGT } from 'gt-node';` opens above. On "pass" `res.json({ message: 'Hello, Alice!' });` is struck out and closes, and on "string" `res.json({ message: gt('Hello, {name}!', { name: 'Alice' }) });` opens in its place, over three lines. On the last "gt" a white box draws around the `gt(...)` line. A blue bar marks each changed line. |
| 0:32.74 | Narrator | List server.js in gt.config.json, sign in, and run npx gt translate. | The heading "List server.js in gt.config.json", and the code panel shows a new file, `gt.config.json`: `"defaultLocale": "en"`, `"locales": ["es", "fr", "ja"]`, `"src": ["server.js"]` and `"output": "translations/[locale].json"` under `files.gt`. On "server.js" `"src": ["server.js"]` turns blue, on "gt.config.json" the file joins the file panel, and a white box draws around the `"src"` line. On "sign in" the panel shows a terminal where `npx gt login` is typed; on "and" `export GT_PROJECT_ID="your-project-id"` appears; on "npx" `npx gt translate` is typed. On "translate" a line draws from the command to the file panel, where `translations/` appears with `es.json`, `fr.json` and `ja.json`. |
| 0:40.64 | Narrator | Now Japanese, French and Spanish browsers each get their own greeting. | The app page cuts back in the middle of the frame for a Japanese browser: the language code `ja` and the greeting in Japanese. On "French" it cuts to a French browser's page, with `fr` and the greeting in French, and on "Spanish" to the Spanish browser's, with `es` and the greeting in Spanish. On "greeting" the greeting turns blue, in the place where the English greeting turned blue at the start. |
| 0:45.42 | No voice |  | The end card: the General Translation logo with generaltranslation.com/docs/node/quickstart under it, held to the last frame while the music ends on a held chord. |

Times are where each line starts in the final, read from the film's own audio.
