The API for localizing your extension.
/* See the locales/en.default.json tab for the translation keys and values for this example */
import {
reactExtension,
Text,
useTranslate,
} from '@shopify/ui-extensions-react/customer-account';
export default reactExtension(
'customer-account.order-status.block.render',
() => <Extension />,
);
function Extension() {
const translate = useTranslate();
return (
<Text>{translate('welcomeMessage')}</Text>
);
}
/* See the locales/en.default.json tab for the translation keys and values for this example */
import {extension} from '@shopify/ui-extensions/customer-account';
export default extension(
'customer-account.order-status.block.render',
(root, {i18n}) => {
const welcomeMsg = i18n.translate(
'welcomeMessage',
);
root.appendChild(root.createText(welcomeMsg));
},
);
{
"welcomeMessage": "Welcome to our store!"
}
The base API object provided to this and other `customer-account` extension targets.
Utilities for translating content and formatting values according to the current `localization` of the user.
Details about the language of the buyer.
Returns a localized currency value. This function behaves like the standard `Intl.NumberFormat()` with a style of `currency` applied. It uses the buyer's locale by default.
Returns a localized date value. This function behaves like the standard `Intl.DateTimeFormatOptions()` and uses the buyer's locale by default. Formatting options can be passed in as options.
Returns a localized number. This function behaves like the standard `Intl.NumberFormat()` with a style of `decimal` applied. It uses the buyer's locale by default.
Returns translated content in the buyer's locale, as supported by the extension. - `options.count` is a special numeric value used in pluralization. - The other option keys and values are treated as replacements for interpolation. - If the replacements are all primitives, then `translate()` returns a single string. - If replacements contain UI components, then `translate()` returns an array of elements.
This defines the i18n.translate() signature.
export interface I18nTranslate { /** * This returns a translated string matching a key in a locale file. * * @example translate("banner.title") */ <ReplacementType = string>( key: string, options?: {[placeholderKey: string]: ReplacementType | string | number}, ): ReplacementType extends string | number ? string : (string | ReplacementType)[]; }
This is the buyer's language, as supported by the extension. If the buyer's actual language is not supported by the extension, this is the fallback locale used for translations. For example, if the buyer's language is 'fr-CA' but your extension only supports translations for 'fr', then the `isoCode` for this language is 'fr'. If your extension does not provide french translations at all, this value is the default locale for your extension (that is, the one matching your .default.json file).
The language the buyer sees in the customer account hub.
The BCP-47 language tag. It may contain a dash followed by an ISO 3166-1 alpha-2 region code.
Returns the buyer's language, as supported by the extension. If the buyer's actual language is not supported by the extension, it will return the fallback locale used for translations.
Returns the current language of the checkout, and automatically re-renders your component if the language changes.
export function useLanguage< Target extends RenderExtensionTarget = RenderExtensionTarget, >(): Language { const {localization} = useApi<Target>(); return useSubscription(localization.language); }
The BCP-47 language tag. It may contain a dash followed by an ISO 3166-1 alpha-2 region code.
Returns the language the buyer sees in the customer account hub.
Returns the buyer's language, as supported by the extension.
export function useExtensionLanguage< Target extends RenderExtensionTarget = RenderExtensionTarget, >(): Language { const {localization} = useApi<Target>(); return useSubscription(localization.extensionLanguage); }
The BCP-47 language tag. It may contain a dash followed by an ISO 3166-1 alpha-2 region code.
Returns utilities for translating content and formatting values according to the current localization of the user.
Returns utilities for translating content and formatting values according to the current localization of the user.
export function useI18n< Target extends RenderCustomerAccountExtensionTarget = RenderCustomerAccountExtensionTarget, >(): I18n { return useApi<Target>().i18n; }
Returns a localized currency value. This function behaves like the standard `Intl.NumberFormat()` with a style of `currency` applied. It uses the buyer's locale by default.
Returns a localized date value. This function behaves like the standard `Intl.DateTimeFormatOptions()` and uses the buyer's locale by default. Formatting options can be passed in as options.
Returns a localized number. This function behaves like the standard `Intl.NumberFormat()` with a style of `decimal` applied. It uses the buyer's locale by default.
Returns translated content in the buyer's locale, as supported by the extension. - `options.count` is a special numeric value used in pluralization. - The other option keys and values are treated as replacements for interpolation. - If the replacements are all primitives, then `translate()` returns a single string. - If replacements contain UI components, then `translate()` returns an array of elements.
This defines the i18n.translate() signature.
export interface I18nTranslate { /** * This returns a translated string matching a key in a locale file. * * @example translate("banner.title") */ <ReplacementType = string>( key: string, options?: {[placeholderKey: string]: ReplacementType | string | number}, ): ReplacementType extends string | number ? string : (string | ReplacementType)[]; }
Returns the `I18nTranslate` interface used to translate strings.
Returns the `I18nTranslate` interface used to translate strings.
export function useTranslate< Target extends RenderExtensionTarget = RenderExtensionTarget, >(): I18nTranslate { const {i18n} = useApi<Target>(); const translate = useCallback<I18nTranslate>( (...args) => { const translation = i18n.translate(...args); if (!Array.isArray(translation)) { return translation as any; } return translation.map((part, index) => { if (isValidElement(part)) { // eslint-disable-next-line react/no-array-index-key return cloneElement(part as RemoteComponentType<any>, {key: index}); } return part; }); }, [i18n], ); return translate; }
This defines the i18n.translate() signature.
export interface I18nTranslate { /** * This returns a translated string matching a key in a locale file. * * @example translate("banner.title") */ <ReplacementType = string>( key: string, options?: {[placeholderKey: string]: ReplacementType | string | number}, ): ReplacementType extends string | number ? string : (string | ReplacementType)[]; }
The API for localizing your extension.
You can use the count option to get a pluralized string based on the current locale.
/* See the locales/en.default.json tab for the translation keys and values for this example */
import {
reactExtension,
Banner,
useApi,
useTranslate,
} from '@shopify/ui-extensions-react/customer-account';
export default reactExtension(
'customer-account.order-status.block.render',
() => <Extension />,
);
function Extension() {
const {i18n} = useApi();
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} />;
}
/* See the locales/en.default.json tab for the translation keys and values for this example */
import {
extension,
Banner,
} from '@shopify/ui-extensions/customer-account';
export default extension(
'customer-account.order-status.block.render',
(root, {i18n}) => {
const points = 10000;
const formattedPoints =
i18n.formatNumber(points);
// Translate the loyalty points message, using pluralization to differentiate messages
const loyaltyPointsMsg = i18n.translate(
'loyaltyPoints',
{
count: points,
formattedPoints,
},
);
const app = root.createComponent(Banner, {
title: loyaltyPointsMsg,
});
root.appendChild(app);
},
);
{
"loyaltyPoints": {
"one": "You have {{formattedPoints}} loyalty point",
"other": "You have {{formattedPoints}} loyalty points"
}
}
/* See the locales/en.default.json tab for the translation keys and values for this example */
import {
reactExtension,
Banner,
useApi,
useTranslate,
} from '@shopify/ui-extensions-react/customer-account';
export default reactExtension(
'customer-account.order-status.block.render',
() => <Extension />,
);
function Extension() {
const {i18n} = useApi();
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} />;
}
/* See the locales/en.default.json tab for the translation keys and values for this example */
import {
extension,
Banner,
} from '@shopify/ui-extensions/customer-account';
export default extension(
'customer-account.order-status.block.render',
(root, {i18n}) => {
const points = 10000;
const formattedPoints =
i18n.formatNumber(points);
// Translate the loyalty points message, using pluralization to differentiate messages
const loyaltyPointsMsg = i18n.translate(
'loyaltyPoints',
{
count: points,
formattedPoints,
},
);
const app = root.createComponent(Banner, {
title: loyaltyPointsMsg,
});
root.appendChild(app);
},
);
{
"loyaltyPoints": {
"one": "You have {{formattedPoints}} loyalty point",
"other": "You have {{formattedPoints}} loyalty points"
}
}