Dynamic Content
A brief overview of working with Dynamic Content in GT.
Overview
Dynamic content is any content that can change based on the user, context, environment, etc. This contrasts with static content, which remains the same regardless of the user, context, environment, etc.
"To Tx or not to Tx"
Sometimes, we want to translate dynamic content, but at other times we want it to remain the same.
A good example would be a user's email address or name. Another example might be a bank account balance or a user's SSN. Such items are (1) unlikely to need translation when your app is rendered in a different language and (2) can vary (in this case between each user).
Example
import { T, Var } from 'gt-next'
export default function Greeting(name) {
return (
<T id='greeting'>
Hello, <Var>{name}</Var>!
</T>
);
}As far as translation goes, this has two benefits:
- You do not have to create a translation for every possible name.
- Using
<Var>, we only generate one translation that would essentially look like this:¡Hola, ${name}!
- If we do not use
<Var>, we would have to perform an on-demand translation for every unique name:- "¡Hola, Alice!", "¡Hola, Bob!", "¡Hola, Charlie!", "¡Hola, David!", ...
- Using
- You also don’t have to worry about the names themselves changing into translated forms of the names (i.e. "¡Hola, Alicia!", "¡Hola, Roberto!", ...).
As you can see, the <Var> component should be used to wrap any content that should remain the same regardless of locale.
This way, we avoid the need to create translations for every possible value of the dynamic content.
By wrapping private information in a <Var> component, you can ensure that the information is not sent to the General Translations API.
Exceptions
The exceptions to the statement above are (1) in the case of a nested <T> component used inside a <Var> component (i.e., the children of the nested <T> component will be translated)
or (2) when data is passed intentionally to our API via some other means within a child of the <Var> component (i.e., a fetch call).
However, this is not the intended use of the <Var> component or the General Translations API, and doing so can harm load times and performance.
How is this guide?