Domphy

API Reference

createI18n

createI18n<TLocale extends string, TMessages>(options): I18nInstance

Options

OptionTypeDescription
globalKeystringUnique key on globalThis — deduplicates instance across Vite chunks
namespacestringi18next resource namespace
localesRecord<TLocale, TMessages>Translation objects keyed by locale code
defaultLocaleTLocaleFallback locale

Returns: I18nInstance

MemberSignatureDescription
t(key, opts?) → stringStatic translation
t(listener, key, opts?) → stringReactive translation — re-renders on setLocale
initI18n(locale?) → Promise<void>Initialize i18next with the given locale (defaults to defaultLocale)
setLocale(locale) → Promise<void>Switch locale and trigger reactive re-renders
getLocale() → TLocaleGet current locale
detectLocale(opts?) → TLocaleDetect locale from URL prefix or navigator.language

detectLocale options

detectLocale({
  fromUrl?: boolean     // check URL prefix (/en/..., /vi/...)
  fromBrowser?: boolean // check navigator.language
})

Type safety

Pass your translation object as a generic parameter to get fully typed keys:

const en = {
  nav: { home: "Home", about: "About" },
  button: { save: "Save", cancel: "Cancel" },
} as const

const { t } = createI18n<"en" | "fr", typeof en>({ ... })

t("nav.home")     // ✓
t("button.save")  // ✓
t("nav.missing")  // ✗ TypeScript error