useDict()
API Reference for the useDict hook
Overview
useDict()
is used to access string translations from the translation dictionary for client-side components.
It must be used within a component wrapped by a <GTProvider>
.
For server-side translations, see getDict()
.
getDict()
and useDict()
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
Parameters
Prop | Type | Default |
---|---|---|
id? | string | undefined |
Description
Prop | Description |
---|---|
id | An optional prefix to prepend to all translation keys. This is useful for working with nested dictionary values. |
Returns
A translation function d()
that, given an id, will return the translated version of the corresponding entry
Name | Type | Description |
---|---|---|
id | string | The id of the entry to be translated |
options? | DictionaryTranslationOptions | Translation options to customize the behavior of d() . |
Examples
Basic Usages
Every entry in your dictionary gets translated.
When we want to access these entries (on the client side), we call useDict()
.
This returns a function that accepts the key of a translation from the dictionary.
Using variables
In order to pass values, you must (1) assign an identifier and (2) reference the identifier when calling the d()
function.
In this example, we use {}
to pass variables to the translations.
In the dictionary, we assign identifier {userName}
.
Using prefixes
We can use prefixes to only translate a subset of the dictionary.
Because we added the value 'prefix1.prefix2'
to the useDict
hook, all of the keys are prefixed with prefix1.prefix2
:
Notes
- The
useDict()
function allows you to access dictionary translations on the client side. - The
useDict()
hook can only be used within a component wrapped by a<GTProvider>
component.
Next Steps
- For server-side translations, see
getDict()
. - Learn more about using dictionaries in the dictionaries reference.
- See DictionaryTranslationOptions for more information on translation options.