Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Locales and strings

KvirnUI ships its texts in five languages, and every text can be replaced for your whole app, a part of the page or a single component.

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.

CodeLanguageImport
svSvenska@kvirn-ui/i18n/sv
fiSuomi@kvirn-ui/i18n/fi
nbNorsk bokmål@kvirn-ui/i18n/nb
nnNorsk nynorsk@kvirn-ui/i18n/nn
enEnglish@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:

  1. The children of a text part, such as <Link.NewTabNotice>(new window)</Link.NewTabNotice>.
  2. The component's own messages prop (per instance).
  3. The nearest provider's messages, then its ancestors'.
  4. 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.

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 { useFormat } from '@kvirn-ui/react'

const format = useFormat()
format.date(date, { dateStyle: 'long' })
format.number(amount, { style: 'currency', currency: 'SEK' })
MethodFormats
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 Date or milliseconds is an instant, shown in the provider's timeZone, or in UTC when there is none. options.timeZone wins. Set the zone on the provider so the server and the browser agree.
  • A string written YYYY-MM-DD is a calendar date, such as a date of birth: it is shown on that day in every zone. Any other string throws a RangeError, so check a missing value yourself.
  • The format object stays the same until the locale or the time zone changes, so it is safe in a dependency list. Without a provider it uses en.

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' })