General Translation  
Next.jsDictionary

getGT()

API Reference for the getGT server-side translation function

Overview

getGT() is used to get translations from the translation dictionary for server-side components.

const t = await getGT(); // Get the translation function
t('greeting.hello'); // pass the id to get a translation

getGT() supports:

  • Translation of string and jsx content.
  • Variable insertion and conditional logic within translations.
  • Optional id prefixing.

For client-side translations, see useGT().

getGT(), useGT(), and useElement() use a dictionary to store all content for translation. This is different from using the <T> component for translation. If you are interested in only using <T> components for translation, then this document is not relevant.

Reference

Props

PropTypeDefault
id?
string
undefined

Description

PropDescription
idAn optional prefix to prepend to all translation keys. This is useful for working with nested dictionary values.

Returns

A promise that resolves to the t() translation function with the following signature:

(id: string, options?: Record<string, any>) => React.ReactNode
  • id: The identifier of the translation to fetch.
  • options: An optional object to provide dynamic variables or branching content.
  • Returns: The translated value as a React.ReactNode.

Examples

Basic Usage

Every entry in your dictionary gets translated. Entries can be either JSX or a string.

dictionary.jsx
const dictionary = {
  greeting1: <>Hello, Alice!</>, 
  greeting2: "Hello, Bob!", 
};
export default dictionary;

When we want to access these entries (on the server side), we call getGT(). This returns a function that accepts the key of a translation from the dictionary.

translateGreeting.jsx
import { getGT } from 'gt-next/server';
 
export default async function translateGreeting() {
  
  const t = await getGT(); 
 
  return (
    <p>
      {t('greeting1')}
      {t('greeting2')}
    </p>
  );
}

Using variables

You can pass variables to dictionary translations. For jsx content, just use a variable component. For string content, add curley braces {} to the string.

Whenever you have a variable in your jsx translations, NEVER insert variables directly into the jsx. ALWAYS use a variable component: <Currency>, <DateTime>, <Num>, or <Var>.

In order to pass values, you must (1) assign an identifier and (2) reference the identifier when calling the t() function. In this example, we use <Var> and {} to pass variables to the translations. In the dictionary, we assign identifiers name="userName" and {userName}.

dictionary.jsx
const dictionary = {
  greeting1: <>Hello, <Var name="userName"/> !</>, 
  greeting2: "Hello, {userName}!", 
};
export default dictionary;

In the component, we pass the values userName: "Alice" and userName: "Bob".

src/server/translateGreeting.jsx
import { getGT } from 'gt-next/server';
 
export default async function translateGreeting() {
  const t = await getGT();
  
  // Hello Alice!
  const greetingAlice = t('greeting1', { userName: "Alice" }); 
  // Hello Bob!
  const greetingBob = t('greeting2', { userName: "Bob" }); 
 
  return (
    <p>
      {greetingAlice}
      {greetingBob}
    </p>
  );
}

Using branches

Branching components, such as <Branch> and <Plural>, can be used to handle control logic in a translation.

In order to pass values, you must (1) assign an identifier and (2) reference the identifier when calling the t() function. In this example, we see that hairColor is passed to both <Branch> and <Var> components.

dictionary.jsx
import { Branch, Var } from 'gt-next';
 
const dictionary = {
  hairColor: <>
    <Branch name="hairColor"
      black={<>Their hair is dark.</>}
      brown={<>Their hair is in the middle.</>}
      blonde={<>Their hair is light.</>}
      >
        {/* fallback incase no branch is matched */}
        <p>Unhandled hair color: <Var name="hairColor"/></p>
      </Branch>
  </>,
};
HairColor.jsx
import { getGT } from 'gt-next/server';
 
export default function HairColor() {
  const t = await getGT();
  return (
    <div>
      { t('hairColor', { hairColor: 'brown' }) }
    </div>
  );
}

Using prefixes

We can use prefixes to only translate a subset of the dictionary.

dictionary.jsx
const dictionary = {
  prefix1: { 
    prefix2: { 
      greeting1: <>Hello, Alice</>,
      greeting2: "Hello, Bob",
    }
  }
};
export default dictionary;

Because we added the value 'prefix1.prefix2' to the getGT method, all of the keys are prefixed with prefix1.prefix2:

UserDetails.jsx
import { getGT } from 'gt-next';
 
export default function UserDetails() {
  const t = await getGT('prefix1.prefix2'); 
  return (
    <div>
      <p>{t('greeting1')}</p> {/* greeting1 => prefix1.prefix2.greeting1 */}
      <p>{t('greeting2')}</p> {/* greeting2 => prefix1.prefix2.greeting2 */}
    </div>
  );
}

Notes

  • The getGT() function allows you to access dictionary translations on the server side.
  • Supports dynamic behavior and variable insertion with Branching and Variable Components respectively.

Next Steps

On this page