I18n
This page discusses how to setup internationalization using the vanilla browser.i18n
APIs and mentions some alternatives if you want to use something else.
Usage
Add
default_locale
to your manifest:tsexport default defineConfig({ manifest: { default_locale: 'en', }, });
Create
messages.json
files in thepublic/
directory:html📂 {srcDir}/ 📂 public/ 📂 _locales/ 📂 en/ 📄 messages.json 📂 de/ 📄 messages.json 📂 ko/ 📄 messages.json
jsonc// public/_locales/en/messages.json { "helloWorld": { "message": "Hello world!", }, }
Get the translation:
tsbrowser.i18n.getMessage('helloWorld');
Optional: Add translations for extension name and description:
json
{
"extName": {
"message": "..."
},
"extDescription": {
"message": "..."
},
"helloWorld": {
"message": "Hello world!"
}
}
ts
export default defineConfig({
manifest: {
name: '__MSG_extName__',
description: '__MSG_extDescription__',
default_locale: 'en',
},
});
Alternatives
The vanilla API has very few features, which is why you may want to consider using third-party NPM packages like i18next
, react-i18n
, vue-i18n
, etc.
However, it is recommended you stick with the vanilla API (or a package based on top of the vanilla API, like @wxt-dev/i18n
), because:
- They can localize text in your manifest and CSS files
- Translations are loaded synchronously
- Translations are not bundled multiple times, keeping your extension small
- Zero configuration
Here are some examples of how to setup a third party i18n library: