useMessages
API reference for the useMessages() string translation function
Overview
The useMessages function is a hook for translating encoded strings from msg at build time.
const m = useMessages();
<p>{ m(encodedString) }</p>;Build-time translation:
useMessages translations occur at build time, before your app is deployed.
You can pass encoded strings from msg, and they will be translated into the user's preferred language.
Reference
Parameters
None
Returns
A callback function, m, that translates the supplied encoded content from msg.
(encodedContent: string, options?: Record<string, any>) => string| Name | Type | Description |
|---|---|---|
encodedContent | string | The encoded string content from msg to be translated. |
options? | Record<string, any> | Optional parameters to override variables in the encoded string. |
Behaviour
Production
During the CD process, any content inside a msg function will be translated before your application is deployed.
This ensures fast load times for all locales, but it can only translate content known at build time.
Once generated, translations are either (1) stored on the CDN or (2) included in your app’s build output, according to your configuration. From there, the translated content is served to your users. If a translation isn’t found, it will fall back to the original content.
Make sure to follow the deployment guide here.
Development
During development, the m function will translate content on demand.
This is useful for prototyping how your app will look in different languages.
Remember to add a Dev API key to your environment to enable this behaviour.
You may see a delay when translating on demand in development.
This won’t occur in production builds unless content is explicitly being translated on demand,
i.e., using tx or <Tx>.
Example
Basic usage
You can use useMessages to translate encoded strings from msg.
import { msg, useMessages } from 'gt-react';
const encodedGreeting = msg('Hello, Alice!');
export default function TranslateGreeting() {
const m = useMessages();
return (
<p>
{m(encodedGreeting)}
</p>
);
}Note: "Alice" will be translated into the user's preferred language.
Using variables
You can override variables in encoded strings.
import { msg, useMessages } from 'gt-react';
const encodedGreeting = msg('Hello, {name}!', { name: 'Alice' });
export default function TranslateGreeting() {
const m = useMessages();
return (
<p>
{m(encodedGreeting, { name: 'Bob' })}
</p>
);
}Note: This will display "Hello, Bob!" — the variable is overridden at render time.
Using the ICU message format
gt-react supports the ICU message format, enabling you to format your variables as well.
import { msg, useMessages } from 'gt-react';
const encodedMessage = msg('There are {count, plural, =0 {no items} =1 {one item} other {{count} items}} in the basket', { count: 10 });
export default function TranslateGreeting() {
const m = useMessages();
return (
<p>
{m(encodedMessage)}
</p>
);
}The ICU MessageFormat is a powerful way to format your variables. For more information, see the ICU MessageFormat documentation.
Notes
- The
useMessagesfunction is a hook that translates encoded strings frommsg. - Translations with
useMessagesare resolved before runtime, during the build process (unless in development).
Next steps
- See
getMessagesfor async string translations from encoded strings at build time. - See
msgfor encoding strings for translation. - For runtime translations, see
txand<Tx>.
How is this guide?