Skip to main content

useTranslate
hook

Returns the I18nTranslate interface used to translate strings.

I18nTranslate

Was this section helpful?

Translating strings

/* See the locales/en.default.json tab for the translation keys and values for this example */
import React from 'react';
import {
render,
Text,
useTranslate,
} from '@shopify/checkout-ui-extensions-react';

render('Checkout::Dynamic::Render', () => (
<Extension />
));

function Extension() {
const translate = useTranslate();
return (
<Text>{translate('welcomeMessage')}</Text>
);
}

Anchor to example-translating-strings-with-pluralizationTranslating strings with pluralization

You can use the count option to get a pluralized string based on the current locale. See localizing UI extensions for more information.

Was this section helpful?

Translating strings with pluralization

/* See the locales/en.default.json tab for the translation keys and values for this example */
import React from 'react';
import {
render,
Banner,
useExtensionApi,
useTranslate,
} from '@shopify/checkout-ui-extensions-react';

render('Checkout::Dynamic::Render', () => (
<Extension />
));

function Extension() {
const {i18n} = useExtensionApi();
const translate = useTranslate();

const points = 10000;
const formattedPoints =
i18n.formatNumber(points);
// Translate the loyalty points message, using pluralization to differentiate messages
const loyaltyPointsMsg = translate(
'loyaltyPoints',
{
count: points,
formattedPoints,
},
);

return <Banner title={loyaltyPointsMsg} />;
}