# gt-node: General Translation Node.js SDK: Traduzione delle stringhe
URL: https://generaltranslation.com/it/docs/node/guides/translating-strings.mdx
Docs index: https://generaltranslation.com/llms.txt
Description: Come tradurre le stringhe negli handler, i messaggi registrati e il contenuto dinamico con General Translation in gt-node.

`gt-node` offre tre modi per tradurre una stringa. La scelta dipende da dove si trova la stringa e dal fatto che sia nota in anticipo oppure no.

*Nota: tutti e tre vengono eseguiti all&#39;interno di un contesto [`withGT`](/docs/node/guides/detecting-locale), così possono conoscere l&#39;impostazione regionale della richiesta.*

## Traduci le stringhe dell&#39;handler con `getGT` [#get-gt]

Per le stringhe scritte direttamente in un handler, usa `await` con [`getGT`](/docs/node/reference/functions/get-gt) per ottenere una funzione di traduzione, quindi chiamala. Interpola i valori con i segnaposto ICU.

```ts
import { getGT } from 'gt-node';

app.get('/api/greeting', async (req, res) => {
  const gt = await getGT();
  res.json({
    message: gt('Hello, world!'),
    welcome: gt('Welcome, {name}!', { name: 'Alice' }),
  });
});
```

Questa è la scelta predefinita per le stringhe una tantum. Aggiungi `$context` per disambiguare un termine.

## Riutilizza i messaggi registrati con `msg` [#msg]

Per le stringhe definite al di fuori di un handler — costanti condivise, messaggi di errore, enum — registrale con [`msg`](/docs/node/reference/functions/msg) nell&#39;ambito del modulo, quindi recuperale con [`getMessages`](/docs/node/reference/functions/get-messages).

```ts
import { msg, getMessages } from 'gt-node';

const NOT_FOUND = msg('Resource not found.');

app.use(async (req, res) => {
  const m = await getMessages();
  res.status(404).json({ error: m(NOT_FOUND) });
});
```

## Traduci il contenuto dinamico con `tx` [#tx]

Per i contenuti che non sono noti in anticipo e non vengono pregenerati, usa [`tx`](/docs/node/reference/functions/tx) per tradurre a runtime. Effettua una richiesta di rete in caso di cache miss, quindi riservalo alle stringhe davvero dinamiche.

```ts
import { tx } from 'gt-node';

const translated = await tx(`Status: ${status}`);
```

*Nota: [`tx`](/docs/node/reference/functions/tx) non usa segnaposto ICU: inserisci i valori con un template literal prima di chiamarlo.*

## Scegli un approccio [#choose]

* Stringhe one-off negli handler → [`getGT`](/docs/node/reference/functions/get-gt).
* Costanti ed errori condivisi → [`msg`](/docs/node/reference/functions/msg) con [`getMessages`](/docs/node/reference/functions/get-messages).
* Contenuti dinamici e imprevedibili → [`tx`](/docs/node/reference/functions/tx).
* Testi centralizzati basati su chiavi → un dizionario con [`getTranslations`](/docs/node/reference/functions/get-translations).

## Next steps

- /docs/node/guides/detecting-locale
- /docs/node/guides/storing-translations
- /docs/node/guides/configuring

## Sitemap

See the full [sitemap](https://generaltranslation.com/sitemap.md) for all pages.
