Translate React component strings using `useTranslate`
Translate visible strings in your components by replacing hardcoded values with translation keys. The useTranslate hook lets you access translations dynamically, with support for metadata-based values, runtime state, and variable interpolation.
This tutorial shows how to integrate useTranslate in a React component and explains the required configuration to support it.
Prerequisites
Before using useTranslate, make sure your application supports internationalization. These are shared prerequisites with Translate your application strings to different languages:
- Declare locales in the backend using
Localeentries in{package}/seed/Locale/*.json. - Declare the same locales in the frontend config at
{package}/config/UiSdlConfig/default.json, using thei18n.localesfield. - Add translations in
{package}/metadata/Translation/{locale}.csv, mapping keys to localized values. - Ensure the platform runtime includes an initialized
react-intlprovider and Redux store. This is configured automatically by the UI Framework.
Once these are in place, useTranslate will be able to resolve translations correctly in your component.
Use the useTranslate hook
Import and call the useTranslate hook inside your component to get a translation function:
import useTranslate from '@c3/sdl-react/hooks/useTranslate';
const MyComponent = () => {
const translate = useTranslate();
return <h2>{translate({ key: 'myPackage.component.title', defaultValue: 'Welcome' })}</h2>;
};You can pass the following options:
key: A string identifier for the translationdefaultValue: A fallback if no value is foundspec: AUiSdlDynamicValueSpecfor metadata-based resolutionvariables: An object to interpolate dynamic valuesformats: Optional formatting configuration
Replace hardcoded strings
Remove all hardcoded strings in your component and replace them with calls to translate().
✅ Do this:
translate({ key: 'myPackage.button.submit', defaultValue: 'Submit' })🚫 Avoid:
<button>Submit</button>This ensures your UI can adapt to locale changes and use platform-driven overrides.
Interpolate variables
Pass a second argument to interpolate dynamic values:
translate({ key: 'myPackage.alert.welcome' }, { name: 'Alice' });In your translation CSV:
myPackage.alert.welcome,Welcome, {name}!The output becomes:
Welcome, Alice!Use dynamic translation specs
Use spec when receiving a translation key from metadata or when a component is configured dynamically through SDL:
const CustomChart = ({ title }: CustomChartProps) => {
const translate = useTranslate();
return <h1>{translate({ spec: title })}</h1>;
};This resolves the value using the Redux state, component props, and metadata context.
Best practices
- Use clear and scoped translation keys:
{package}.{component}.{intent} - Include
defaultValuein all calls to support graceful fallback - Interpolate values instead of concatenating strings
- Keep one CSV file per locale, and update them in sync with new features
- Avoid translation key duplication by starting with a shared
en.csv