DictionaryTranslationOptions
API reference for the DictionaryTranslationOptions type
Overview
The DictionaryTranslationOptions type is used to pass variables to dictionary entries and specify their render behaviour.
It is used with useTranslations and getTranslations to pass variables to dictionary entries.
Build-time translation:
Variables are not translated with useTranslations and getTranslations—only the original string is.
See tx for translating strings with dynamic content.
Reference
Parameters
Prop
Type
Description
| Prop | Description |
|---|---|
variables | An object whose 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 with the following syntax: {username}.
username is the name of the variable.
const dictionary = {
greeting: {
hello: 'Hello, {username}!',
},
};
export default dictionary;Next, we reference the variable:
import { useTranslations } from 'gt-next';
const Component = () => {
const d = useTranslations();
return <div>{d('greeting.hello', { username: 'Brian123' })}</div>;
};Using ICU message format
gt-next supports the ICU message format, which also lets you format your variables.
const dictionary = {
account: {
balance: 'Your account balance: {dollars, number, ::currency/USD}!',
},
};
export default dictionary;Next, we reference the variable:
import { useTranslations } from 'gt-next';
const Component = () => {
const d = useTranslations();
return <div>
{ d(
'account.balance',
{
"dollars" : 1000000,
}
) }
</div>;
};Notes
- The
variablesobject passes values to a dictionary entry. - The
variablesOptionsobject defines the behaviour of the variables.
Next steps
- See dictionaries for more information on dictionaries and common practices.
- See
useTranslationsorgetTranslationsfor more information on the dictionaries interface. - See
ICU message formatfor more information on formatting options.
How is this guide?