RuntimeTranslationOptions
API reference for the RuntimeTranslationOptions type
Overview
The RuntimeTranslationOptions type is used to pass variables to inline translations and specify their render behaviour.
You can also add a locale to specify a different language for the translation.
This is used with the tx function.
Runtime Translation:
To translate a variable on demand, include it directly in the string passed to tx.
Variables passed to tx via the options object are not translated.
Reference
Parameters
Prop
Type
Description
| Prop | Description |
|---|---|
variables | An object where the keys identify where each value is mapped in the string. |
$locale | Optionally include $locale as a variable in the variables object to specify a locale for the translation. By default, this uses the browser’s highest‑priority locale that’s supported by your app. |
Examples
Passing variables
To add a variable to the string, use the {variable-name} syntax, where curly braces enclose the variable name.
import { tx } from 'gt-next/server';
const Component = () => {
return <div>
{tx(`Hello, {username}!`,{ username: 'Brian123' })}
</div>;
};Using the ICU message format
gt-next supports the ICU message format, which also lets you format your variables.
import { tx } from 'gt-next/server';
const Component = () => {
return <div>
{ tx(
'Your account balance: {dollars, number, ::currency/USD}!',
{
"dollars" : 1000000,
}
) }
</div>;
};Translating variables
To translate a variable, include it directly in the string passed to tx.
import { tx } from 'gt-next/server';
const Component = ({ hairColor }: { hairColor: string }) => {
return <div>{tx(
`Hello, {username}! Your hair colour is ${hairColor}`,
{ username: 'Brian123' }
)}</div>;
};Note that the variable hairColor is translated, but username is not.
Specifying a locale
You can specify a locale to use for the translation.
import { tx } from 'gt-next/server';
const Component = () => {
return <div>{tx('Hello, world!', { $locale: 'fr' })}</div>;
};This will always be translated into French.
Notes
RuntimeTranslationOptionsis used for string translations at runtime.- The
variablesobject passes values into the text. - The
variablesOptionsobject defines the behaviour of the variables.
Next steps
- See
txfor more information on inline string translations. - See the
ICU message formatfor more details on formatting options.
How is this guide?