Types

DictionaryTranslationOptions

API reference for the DictionaryTranslationOptions type

Overview

The DictionaryTranslationOptions type is used to pass variables to dictionary entries and specify how they are rendered. It is used with useTranslations to pass variables to dictionary entries.

Build-time translation: useTranslations translations occur at build time; however, variables are never translated. Instead, they are inserted into the translation with formatting. Make sure to follow the deployment guide here.

Reference

Parameters

Prop

Type

Description

PropDescription
variablesAn object where the keys indicate where each value is mapped in the dictionary entry.

Examples

Passing variables

To pass a variable to the dictionary entry, we need to do two things: (1) add a variable to the entry and (2) reference that variable in the d invocation.

First, add a variable to the dictionary entry using the following syntax: {username}. username is the name of the variable.

dictionary.ts
const dictionary = {
  greeting: {
    hello: 'Hello, {username}!',
  },
};

export default dictionary;

Next, we reference the variable:

Component.tsx
import { useTranslations } from 'gt-react';

const Component = () => {
  const d = useTranslations();
  return <div>{d('greeting.hello', { username : 'Brian123' })}</div>;
};

Using ICU message format

gt-react supports the ICU message format, which also lets you format your variables.

dictionary.ts
const dictionary = {
  account: {
    balance: 'Your account balance: {dollars, number, ::currency/USD}!',
  },
};

export default dictionary;

Next, we reference the variable:

Component.tsx
import { useTranslations } from 'gt-react';

const Component = () => {
  const d = useTranslations();
  return <div>
    { d(
      'account.balance',
      {
        "dollars" : 1000000,
      }
    ) }
  </div>;
};

Notes

  • The variables object passes values to a dictionary entry.
  • The variablesOptions object defines the behaviour of the variables.

Next steps

How is this guide?

DictionaryTranslationOptions