Internationalization (i18n)
Multi-language UI text with next-intl — msg() in configs, locale per user, formatting and zod messages
Every text the library emits (buttons, search, empty states, pagination, dialogs, validation,
queue dashboard) comes from translated messages in pt-BR (default), en and es, with a
locale per user and runtime switching. The engine is next-intl, a
required peer dependency.
The 3 rules
| Where | How |
|---|---|
config.tsx (plain object, no hooks) | title: msg('users.title') |
| Client Component | const t = useTranslations('users'); t('title') |
| Server Component | const t = await getTranslations('users'); t('title') |
A new key goes into all three messages/{pt-BR,en,es}.json files of the app. Everything
i18n-related lives in the @telamonjs/ui/i18n entry point.
Import next-intl directly
The library never re-exports next-intl. With optimizePackageImports a re-export would
create a second copy of the context — the same failure class as "No QueryClient set".
App setup
Apps generated by the CLI already start this way.
export const locales = ['pt-BR', 'en', 'es'] as const
export type AppLocale = (typeof locales)[number]
export const defaultLocale: AppLocale = 'pt-BR'
export const timeZone = 'America/Sao_Paulo'
export const currency = 'BRL'The locale is resolved per request: NEXT_LOCALE cookie → Accept-Language → default.
pickLocale below stands for that lookup; the CLI template ships a complete one.
import { getRequestConfig } from 'next-intl/server'
import { cookies, headers } from 'next/headers'
import { telamonjsMessages } from '@telamonjs/ui/i18n'
import { timeZone } from './config'
export default getRequestConfig(async () => {
const locale = pickLocale(
(await cookies()).get('NEXT_LOCALE')?.value,
(await headers()).get('accept-language')
)
const app = (await import(`../../messages/${locale}.json`)).default
// Namespaces never collide (library = `telamonjs`, app = per module)
return { locale, timeZone, messages: { ...telamonjsMessages[locale], ...app } }
})Apps using the library's AppProviders pass everything through config.i18n:
<AppProviders config={{ i18n: { locale, messages, timeZone, currency: 'BRL', onLocaleChange } }}>
{children}
</AppProviders>Without config.i18n the app runs in pt-BR with the library's own messages. Without
messages, only pt-BR is in the client bundle: en/es load on demand and the screen shows pt-BR
until they arrive, so an app in another locale should pass messages from the server
(getMessages()).
Apps mounting providers by hand wrap the tree once:
import { NextIntlClientProvider } from 'next-intl'
import { TelamonjsI18nProvider, telamonjsMessages } from '@telamonjs/ui/i18n'
<NextIntlClientProvider locale='pt-BR' messages={telamonjsMessages['pt-BR']} timeZone='America/Sao_Paulo'>
<TelamonjsI18nProvider>{children}</TelamonjsI18nProvider>
</NextIntlClientProvider>An app that already has its own NextIntlClientProvider must merge telamonjsMessages[locale]
into its message tree, otherwise library text renders as raw keys (telamonjs.common.save).
Typed keys: the app declares next-intl's AppConfig, in a global.d.ts. The library does
not ship this augmentation, because it would win over the app's under skipLibCheck.
import type { TelamonjsMessages } from '@telamonjs/ui/i18n'
import type { AppLocale } from './src/i18n/config'
import type ptBR from './messages/pt-BR.json'
declare module 'next-intl' {
interface AppConfig {
Locale: AppLocale
Messages: TelamonjsMessages & typeof ptBR
}
}Switching language at runtime
<LocaleSwitcher /> writes the NEXT_LOCALE cookie (1 year, SameSite=Lax), calls
onLocaleChange?.(locale) — use it to persist to the user's profile — and fires
router.refresh(): Server Components re-render in the new locale, with no reload and no client
locale state. AdminShell already shows it in the user menu when there is more than one locale
(turn it off with localeSwitcher={false}).
Formatting
useFormat() returns functions bound to the provider's locale, timeZone and currency:
const fmt = useFormat()
fmt.date(value) // 'short' (default), 'medium' or 'long'
fmt.date(value, 'long')
fmt.dateTime(value, { seconds: true })
fmt.time(value)
fmt.currency(1234.5) // the provider's currency, not the language's
fmt.currency(1234.5, 'USD')
fmt.number(value)
fmt.percent(value)
fmt.relative(value)Never call toLocaleDateString('pt-BR') in a component. For react-day-picker/date-fns, use
useDateFnsLocale(). Currency and time zone do not derive from the language.
Validation (zod)
setupZodLocale(locale) installs an error map that translates common issues. Schemas without a
message come out translated; a custom message stays in control. Refinements tag the issue:
z.string().refine(isValidCpf, { params: { i18n: 'invalidCpf' } })A key without a dot is relative to telamonjs.validation (the library's validators). A key
with a dot is absolute and resolves against the library's and the app's messages, passed as
the second argument:
setupZodLocale(locale, appMessages) // appMessages = the JSON of messages/<locale>.json
z.string().refine(isPlate, { params: { i18n: 'assets.validation.plate', values: { min: 7 } } })Config slots that accept msg()
Text slots are typed I18nText = string | MessageRef: literal strings keep working, and functions
of the item/row may return msg().
| Slot | Note |
|---|---|
headerConfig.stats.items[].label/value | value: v => msg('assets.total', { count: v.n }) |
headerConfig.title/subtitle(item) | |
confirmDialog.title/description | static or row => msg(...); BulkAction likewise with rows |
ActionConfig.successMessage/errorMessage | |
list.toolbar.counterLabel | a message using {count} receives it and drops the numeric prefix: '{count, plural, one {# order} other {# orders}}' |
IOption.label, StatusOption.label | resolved when the options load |
AdminHeader.title/description, PageHeaderConfig, PortalItem.description | |
crudConfig.title/description/emptyTitle/emptyDescription | header and empty state of the sub-CRUD |
CrudColumnDef.meta.label | the column name where a header function does not render (export, column selector, filters, mobile card) |
columns: [
{
accessorKey: 'plate',
header: () => <Msg k='assets.fields.plate' />,
meta: { label: msg('assets.fields.plate') },
},
]Traps
msg()only in config objects. In a component, useuseTranslations().- Code that reads a config text slot must resolve it first:
const text = useText(), thentext(item.title). Otherwise amsg()renders as[object Object]. - Do not translate domain identifiers, API keys, ids, persisted enum values or
data-testid. - Do not concatenate UI strings — use ICU:
{count, plural, one {# item} other {# items}}. - Date placeholders are day-first in every locale, because the mask parser is day-first.