Rendering i18n messages
The main part of handling internationalization (typically referred to as i18n) in your Next.js app is to provide messages based on the language of the user.
We use the term locale as an identifier that describes the language and formatting preferences of users. Apart from the language, this includes regional information.
Examples:
en-US
: English, specific to the United Statesde-AT
: German, specific to Austriade
: German, without regional specifics
The recommended way to structure your messages is to separate them by locale, e.g. en.json
.
Structuring messages
To group your messages within a locale, it's recommended to use component names as namespaces and embrace them as the primary unit of code organization in your app.
{
"About": {
"title": "About us"
}
}
Now, you can render messages from within a React component via the useTranslations
hook.
import {useTranslations} from 'next-intl';
function About() {
const t = useTranslations('About');
return <h1>{t('title')}</h1>;
}
To retrieve all available messages in a component, you can omit the namespace path:
const t = useTranslations();
t('About.title');
How can I provide more structure for messages?
Optionally, you can structure your messages into nested objects.
{
"auth": {
"SignUp": {
"title": "Sign up",
"form": {
"placeholder": "Please enter your name",
"submit": "Submit"
}
}
}
}
import {useTranslations} from 'next-intl';
function SignUp() {
// Provide the lowest common denominator that contains
// all messages this component needs to consume.
const t = useTranslations('auth.SignUp');
return (
<>
<h1>{t('title')}</h1>
<form>
<input
// The remaining hierarchy can be resolved by
// using `.` to access nested messages.
placeholder={t('form.placeholder')}
/>
<button type="submit">{t('form.submit')}</button>
</form>
</>
);
}
How can I reuse messages?
As your app grows, you'll want to reuse messages among your components. If you use component names as namespaces to structure your messages, you'll automatically benefit from reusable messages by reusing your components.
Examples:
- You're displaying products in your app and often need to resolve a category identifier to a human readable label (e.g.
new
→ "Just in"). To ensure consistency, you can add aProductCategory
component that turns thecategory
into a string. - You commonly need a dialog that displays a "confirm" and "cancel" button. In this case, consider adding a
ConfirmDialog
component to reuse the messages along with the functionality.
There might be cases where you want to use the same message in different components, but there's no reasonable opportunity for sharing the message via a component. This might be symptom that these components should use separate messages, even if they happen to be the same for the time being. By using separate labels, you gain the flexibility to use more specific labels (e.g. "not now" instead of "cancel").
How can I use translations outside of components?
next-intl
only offers an API to use translations from within React components.
This may seem like an unnecessary limitation, but this is intentional and aims to encourage the use of proven patterns that avoid potential issues—especially if they are easy to overlook.
If you're interested to dive deeper into this topic, there's a blog post that discusses the background of this design decision: How (not) to use translations outside of React components.
Providing messages
You can provide page-specific messages via data fetching methods of Next.js (opens in a new tab) for individual pages:
export async function getStaticProps(context) {
return {
props: {
// You can get the messages from anywhere you like. The recommended
// pattern is to put them in JSON files separated by locale and read
// the desired one based on the `locale` received from Next.js.
messages: (await import(`../../messages/${context.locale}.json`)).default
}
};
}
If you want to provide only the minimum amount of messages per page, you can filter your messages accordingly:
import pick from 'lodash/pick';
const namespaces = ['Index'];
export async function getStaticProps(context) {
return {
props: {
messages: pick(
(await import(`../../messages/${context.locale}.json`)).default,
namespaces
)
}
};
}
How can I know all the namespaces needed for a page?
You can assemble the namespaces
dynamically based on the used components on a page. The advanced
example (opens in a new tab) outlines this technique.
This is the gist:
import pick from 'lodash/pick';
import {useTranslations} from 'next-intl';
import PageLayout from 'components/PageLayout';
export default function Index() {
const t = useTranslations('Index');
return <PageLayout title={t('title')}>{t('description')}</PageLayout>;
}
// The namespaces can be generated based on used components. `PageLayout` in
// turn requires messages for `Navigation` and therefore a recursive list of
// namespaces is created dynamically, where the owner of a component doesn't
// have to know which nested components are rendered. Note that this approach
// is limited to components which are not lazy loaded.
Index.messages = ['Index', ...PageLayout.messages];
export async function getStaticProps({locale}) {
return {
props: {
messages: pick(
await import(`../../messages/${locale}.json`),
Index.messages
)
}
};
}
An automatic way to tree-shake messages is being evaluated in issue #1 (opens in a new tab). Leave a 👍 there if you're interested in this feature!
Credits for this approach go to the Relay fragment composition (opens in a new tab).
Rendering messages
next-intl
uses ICU message syntax that allows you to express language nuances and separates state handling within messages from your app code.
You can interpolate values from your components into your messages with a special placeholder syntax.
{
"static": "Hello world!",
"interpolation": "Hello {name}!",
"plural": "You have {numMessages, plural, =0 {no messages} =1 {one message} other {# messages}}.",
"select": "{gender, select, female {She} male {He} other {They}} is online.",
"selectordinal": "It's your {year, selectordinal, one {#st} two {#nd} few {#rd} other {#th}} birthday!",
"escaped": "Escape curly braces with single quotes (e.g. '{name'})"
}
const t = useTranslations();
t('static');
t('interpolation', {name: 'Jane'});
t('plural', {numMessages: 3});
t('select', {gender: 'female'});
t('selectordinal', {year: 11});
t('escaped');
To work with ICU messages, it can be helpful to use an editor that supports this syntax. E.g. the Crowdin Editor (opens in a new tab) can be used by translators to work on translations without having to change app code.
Rich text
You can format rich text with custom tags and map them to React components.
{
"richText": "This is <important><very>very</very> important</important>"
}
t.rich('richText', {
important: (chunks) => <b>{chunks}</b>,
very: (chunks) => <i>{chunks}</i>
});
If you want to use the same tag multiple times, you can configure it via the default translation values.
Arrays of messages
If you need to render a list of messages, the recommended approach is to map an array of keys to the corresponding messages.
{
"Benefits": {
"zero-config": "Works with zero config",
"customizable": "Easy to customize",
"fast": "Blazingly fast"
}
}
import {useTranslations} from 'next-intl';
function Benefits() {
const t = useTranslations('Benefits');
return (
<ul>
{['zero-config', 'customizable', 'fast'].map((key) => (
<li key={key}>{t(key)}</li>
))}
</ul>
);
}
If the number of items varies between locales, you can solve this by using rich text.
{
"Benefits": {
"items": "<item>Works with zero config</item><item>Easy to customize</item><item>Blazingly fast</item>"
}
}
import {useTranslations} from 'next-intl';
function Benefits() {
const t = useTranslations('Benefits');
return (
<ul>
{t.rich('items', {
item: (chunks) => <li>{chunks}</li>
})}
</ul>
);
}
Why can't I just use arrays in my messages?
The advantage of this approach over supporting arrays in messages is that this way you can use the formatting capabilities, e.g. to interpolate values into individual messages.
{
"Benefits": {
"zero-config": "Works with <b>zero config</b>",
"customizable": "Easy to <b>customize</b>",
"fast": "Blazingly <b>fast</b>"
}
}
import {useTranslations} from 'next-intl';
function Benefits() {
const t = useTranslations('Benefits');
return (
<ul>
{['zero-config', 'customizable', 'fast'].map((key, index, arr) => {
const color = `hsl(${(index / (arr.length - 1)) * 360}deg 70% 50%)`;
return (
<li key={key}>
{t(key, {b: (chunks) => <b style={{color}}>{chunks}</b>})}
</li>
);
})}
</ul>
);
}
Raw messages
Messages are always parsed and therefore e.g. for rich text you need to supply the necessary tags. If you want to avoid the parsing, e.g. because you have raw HTML stored in a message, there's a separate API for this use case.
{
"content": "<h1>Headline<h1><p>This is raw HTML</p>"
}
<div dangerouslySetInnerHTML={{__html: t.raw('content')}} />
Important: You should always sanitize the content that you pass to
dangerouslySetInnerHTML
(opens in a new tab)
to avoid cross-site scripting attacks.
The value of a raw message can be any valid JSON value: strings, booleans, objects and arrays.