The five languages#
English is built in and is the fallback. Import the catalog you need from its own path, and only the catalogs you import are bundled. Have a native speaker review the Swedish, Finnish and Norwegian texts you use before you launch.
| Code | Language | Import |
|---|---|---|
sv | Svenska | @kvirn-ui/i18n/sv |
fi | Suomi | @kvirn-ui/i18n/fi |
nb | Norsk bokmål | @kvirn-ui/i18n/nb |
nn | Norsk nynorsk | @kvirn-ui/i18n/nn |
en | English | @kvirn-ui/i18n/en |
Use a language#
Pass the catalog as messages and the matching locale to the KvirnProvider. Without a catalog, English strings are shown under a non-English lang, and in development a warning names each key.
import { KvirnProvider } from '@kvirn-ui/react'
import { sv } from '@kvirn-ui/i18n/sv'
<KvirnProvider locale="sv-SE" messages={sv}>Set <html lang> to the same language yourself: the provider renders no element to carry it.
Change the text#
The first match wins, from the most specific to the built-in text:
- The children of a text part, such as
<Link.NewTabNotice>(new window)</Link.NewTabNotice>. - The component's own
messagesprop (per instance). - The nearest provider's
messages, then its ancestors'. - Built-in English.
For the whole app or a section
defineMessages builds an adjusted catalog once, with typed keys. A nested provider with a partial object changes only its section. A misspelt key or namespace is a type error.
import { defineMessages } from '@kvirn-ui/i18n'
import { sv } from '@kvirn-ui/i18n/sv'
const messages = defineMessages(sv, {
link: { newTabNotice: '(öppnas i nytt fönster)' }, // Swedish for "(opens in a new window)"
})<KvirnProvider locale="sv-SE" messages={messages}>
{/* Swedish for "(external service, new tab)" */}
<KvirnProvider messages={{ link: { newTabNotice: '(extern tjänst, ny flik)' } }}>
{/* only this section */}
</KvirnProvider>
</KvirnProvider>For one component
Pass messages to the component. It takes the keys of that component only.
import { Link } from '@kvirn-ui/react'
import { useLocalesTexts } from './texts.ts'
export function InstanceMessages() {
const { texts } = useLocalesTexts()
return (
<p>
<Link.Root
href="https://www.digg.se/"
target="_blank"
messages={{ newTabNotice: texts.newTabNotice }}
>
{texts.newTab} <Link.NewTabNotice />
</Link.Root>
</p>
)
}Warning: Don't forget
An empty or whitespace-only text is ignored, with a development warning, and the next level is used, so a name is never empty. When you change a string that is part of a name, keep it consistent with the visible label (WCAG 2.5.3).
Your own translation system#
Any key can be a function, so the text can come from your translation system. The provider re-renders when you pass new messages, for example after a language change.
const messages = defineMessages(sv, {
link: { newTabNotice: () => t('kvirn.link.newTabNotice') },
})A key with parameters is always a function. It receives its values and a format helper for the active locale, the same one useFormat() returns.
resultCount: ({ count }, format) =>
// Swedish for "1 result" and "<count> results"
format.plural(count, { one: '1 träff', other: `${format.number(count)} träffar` })Parts in another language#
Put lang on any text that is in another language than the page, so a screen reader switches voice (WCAG 3.1.2). Inside a provider, spread useLocale().localeProps on the element that starts the section: it holds the provider's lang and dir. A nested provider that changes language also needs that language's catalog.
const { localeProps } = useLocale() // { lang: 'fi-FI', dir: 'ltr' }
<section {...localeProps}>…</section>This site does the same: an example whose text fell back to English renders in a nested English provider, so nothing sets lang on a component.
Numbers, dates and lists#
useFormat() writes numbers, dates, lists and plurals the way the nearest provider's locale does. You build no Intl object and keep no locale map. It is the same format that messages receive, so a number reads the same in a message and in a table cell.
- Fee
- SEK 1,250.50
- Decision date
- October 14, 2026
- Documents
- passport, invoice, and certificate
import { DefinitionList, useFormat } from '@kvirn-ui/react'
import { useLocalesTexts } from './texts.ts'
export function Formatting() {
const { texts } = useLocalesTexts()
const format = useFormat()
return (
<DefinitionList.Root>
<DefinitionList.Row>
<DefinitionList.Term>{texts.amount}</DefinitionList.Term>
<DefinitionList.Description>
{format.number(1250.5, { style: 'currency', currency: 'SEK' })}
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.date}</DefinitionList.Term>
<DefinitionList.Description>
{format.date('2026-10-14', { dateStyle: 'long' })}
</DefinitionList.Description>
</DefinitionList.Row>
<DefinitionList.Row>
<DefinitionList.Term>{texts.list}</DefinitionList.Term>
<DefinitionList.Description>{format.list(texts.documents)}</DefinitionList.Description>
</DefinitionList.Row>
</DefinitionList.Root>
)
}import { useFormat } from '@kvirn-ui/react'
const format = useFormat()
format.date(date, { dateStyle: 'long' })
format.number(amount, { style: 'currency', currency: 'SEK' })| Method | Formats |
|---|---|
format.number(value, options?) | Intl.NumberFormat: decimals, percent, currency ({ style: "currency", currency: "SEK" }) and units. |
format.date(value, options?) | Intl.DateTimeFormat: { dateStyle: "long" }, { timeStyle: "short" }, { month: "long" }. |
format.list(items, options?) | Intl.ListFormat: "sv, fi and en", or { type: "disjunction" } for "or". |
format.plural(count, forms) | The form for the locale's plural rules: { one, other }, and zero for exactly 0. other is required. |
- A
Dateor milliseconds is an instant, shown in the provider'stimeZone, or in UTC when there is none.options.timeZonewins. Set the zone on the provider so the server and the browser agree. - A string written
YYYY-MM-DDis a calendar date, such as a date of birth: it is shown on that day in every zone. Any other string throws aRangeError, so check a missing value yourself. - The
formatobject stays the same until the locale or the time zone changes, so it is safe in a dependency list. Without a provider it usesen.
In a server component a hook can't run. Build the same object with createMessageFormat from @kvirn-ui/react/server. See Rendering: server and client.
import { createMessageFormat } from '@kvirn-ui/react/server'
const format = createMessageFormat({ locale: 'sv-SE', timeZone: 'Europe/Stockholm' })