1.9 KiB
Installation
With WXT
-
Install
@wxt-dev/i18nvia your package manager:pnpm i @wxt-dev/i18n -
Add the WXT module to your
wxt.config.tsfile and setup a default locale:export default defineConfig({ modules: ['@wxt-dev/i18n/module'], manifest: { default_locale: 'en', }, }); -
Create a localization file at
<srcDir>/locales/<default_locale>.ymlor move your existing localization files there.# <srcDir>/locales/en.yml helloWorld: Hello world!:::tip
@wxt-dev/i18nsupports the standard messages format, so if you already have localization files at<srcDir>/public/_locale/<lang>/messages.json, you don't need to convert them to YAML or refactor them - just move them to<srcDir>/locales/<lang>.jsonand they'll just work out of the box! ::: -
To get a translation, use the auto-imported
i18nobject or import it manually:import { i18n } from '#i18n'; i18n.t('helloWorld'); // "Hello world!"
And you're done! Using WXT, you get type-safety out of the box.
Without WXT
-
Install
@wxt-dev/i18nvia your package manager:pnpm i @wxt-dev/i18n -
Create a messages file at
_locales/<lang>/messages.jsonor move your existing translations there:{ "helloWorld": { "message": "Hello world!" } }:::info For the best DX, you should to integrate
@wxt-dev/i18ninto your build process. This enables:- Plural forms
- Simple messages file format
- Type safety
See Build Integrations to set it up. :::
-
Create the
i18nobject, export it, and use it wherever you want!import { createI18n } from '@wxt-dev/i18n'; export const i18n = createI18n(); i18n.t('helloWorld'); // "Hello world!";