# node: getMessages URL: https://generaltranslation.com/it/docs/node/api/get-messages.mdx --- title: getMessages description: Documentazione di riferimento dell'API per la funzione getMessages --- ## Panoramica La funzione `getMessages` è asincrona e restituisce un resolver per i messaggi preregistrati. Usala insieme a [`msg`](/docs/node/api/strings/msg) (importato da `gt-node`) per registrare le stringhe in fase di build e risolverne le traduzioni a runtime. ```js import { msg, getMessages } from 'gt-node'; // Registra alla fase di build (o nell'ambito del modulo) const greeting = msg('Hello, world!'); // Risolvi a runtime const m = await getMessages(); const translated = m(greeting); ``` **Contesto della richiesta obbligatorio:** `getMessages` deve essere chiamato all'interno di una callback [`withGT`](/docs/node/api/with-gt) in modo che sappia quale impostazione regionale usare. ## Riferimento ### Parametri Nessuno. ### Restituisce Una promise che restituisce la funzione di risoluzione dei messaggi `m`: ```ts Promise<(encodedMsg: string | null | undefined, options?: InlineResolveOptions) => string | null | undefined> ``` | Nome | Tipo | Descrizione | | ------------ | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | | `encodedMsg` | `string \| null \| undefined` | Una stringa di messaggio codificata restituita da `msg()`. Restituisce `null`/`undefined` se il valore passato è `null`/`undefined`. | | `options?` | `InlineResolveOptions` | Valori variabili facoltativi da interpolare nel messaggio risolto. | *** ## Esempi ### Uso di base ```js title="messages.js" import { msg } from 'gt-node'; // Registra i messaggi nell'ambito del modulo export const GREETING = msg('Hello, world!'); export const WELCOME = msg('Welcome, {name}!'); ``` ```js title="handler.js" import { withGT, getMessages } from 'gt-node'; import { GREETING, WELCOME } from './messages'; function handleRequest(locale: string) { return withGT(locale, async () => { const m = await getMessages(); return { greeting: m(GREETING), welcome: m(WELCOME, { name: 'Alice' }), }; }); } ``` ### Con variabili Passa le variabili come secondo argomento per inserire i valori: ```js title="handler.js" import { msg, getMessages, withGT } from 'gt-node'; const ORDER_STATUS = msg('Order {orderId} is {status}.'); function getOrderMessage(locale: string, orderId: string, status: string) { return withGT(locale, async () => { const m = await getMessages(); return m(ORDER_STATUS, { orderId, status }); }); } ``` *** ## Note * `msg` registra una stringa per la traduzione in fase di build. `getMessages` la risolve a runtime. * Questo pattern è utile per le stringhe definite nell'ambito del modulo (costanti, enum, messaggi di errore) che devono essere tradotte per-request. * Se a `m` viene passato `null` o `undefined`, restituisce rispettivamente `null` o `undefined`. ## Passaggi successivi * Vedi [`getGT`](/docs/node/api/get-gt) per tradurre direttamente le stringhe inline. * Vedi [`withGT`](/docs/node/api/with-gt) per configurare il contesto relativo all'impostazione regionale.