# General Translation React SDKs (gt-react, gt-next): useGT URL: https://generaltranslation.com/it/docs/react/reference/hooks/use-gt.mdx --- title: useGT description: Ottieni una funzione per tradurre stringhe inline con General Translation gt-react. Riferimento API per useGT. --- L'hook `useGT` restituisce una funzione che traduce le stringhe nell'impostazione regionale attiva. Usalo per etichette, placeholder e altre stringhe indipendenti, nei casi in cui [``](/docs/react/reference/components/t) non sia adatto. *Disponibile in `gt-react`, `gt-next`, `gt-tanstack-start` e `gt-react-native`. Gli esempi importano da `gt-react`; importa invece dal pacchetto del framework che utilizzi.* ## Panoramica [#overview] Chiama `useGT` per ottenere la funzione di traduzione, poi passale una stringa. La funzione viene restituita direttamente. ```tsx const gt = useGT();

{gt('This text will be translated')}

; ``` *Nota: in `gt-react`, usa `useGT` all'interno di [``](/docs/react/reference/components/gt-provider). In `gt-next`, funziona anche nei componenti server sincroni. Scrivi `const gt = useGT()` — l'hook restituisce direttamente la funzione, non un oggetto.* ## Come funziona [#how-it-works] * **Traduzione in fase di build.** In produzione, le stringhe passate a `gt` vengono tradotte durante la build, prima della distribuzione. Solo i contenuti noti al momento della build possono essere tradotti. In caso di traduzioni mancanti, viene usada la stringa originale come fallback. * **Traduzione su richiesta in sviluppo.** Con una chiave API di sviluppo, `gt` traduce su richiesta, così puoi visualizzare in anteprima le impostazioni regionali, con un leggero ritardo che non si verifica in produzione. * **Le variabili non vengono tradotte.** I valori interpolati vengono inseriti nella stringa tradotta, ma non vengono mai tradotti. ## Parametri [#parameters] `useGT` non accetta parametri. ## Restituisce [#returns] **Tipo** `(content: string, options?: InlineTranslationOptions) => string` Una funzione che traduce la stringa fornita nell'impostazione regionale attiva. | Nome | Descrizione | Tipo | Facoltativo | | --------- | --------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | ----------- | | `content` | La stringa da tradurre. | `string` | No | | `options` | Variabili di interpolazione e opzioni come `$context`, `$id` e `$maxChars`. | [`InlineTranslationOptions`](/docs/react/reference/types/inline-translation-options) | Sì | ## Esempi [#examples] ```tsx import { useGT } from 'gt-react'; export default function TranslateGreeting() { const gt = useGT(); return

{gt('Hello, Alice!')}

; } // "Hello, Alice!" viene tradotto nell'impostazione regionale dell'utente. ``` ```tsx import { useGT } from 'gt-react'; export default function TranslateGreeting() { const gt = useGT(); return

{gt('Hello, {name}!', { name: 'Alice' })}

; } // "Alice" è una variabile e non viene tradotta. ``` ```tsx import { useGT } from 'gt-react'; export default function ItemCount() { const gt = useGT(); return (

{gt( 'There are {count, plural, =0 {no items} =1 {one item} other {{count} items}} in the cart', { count: 10 } )}

); } ``` `gt-react` supporta [ICU message format](https://unicode-org.github.io/icu/userguide/format_parse/messages/) per formattare le variabili. ## Note [#notes] * `useGT` traduce le stringhe; le traduzioni avvengono in fase di build (o traduzione su richiesta in fase di sviluppo). * In un componente async dell'App Router, usa invece [`getGT`](/docs/react/nextjs/reference/functions/get-gt). * Per i lookup nel dizionario tramite id, usa [`useTranslations`](/docs/react/reference/hooks/use-translations); per registrare stringhe a livello di modulo, usa [`msg`](/docs/react/reference/functions/msg) con [`useMessages`](/docs/react/reference/hooks/use-messages).