Vite Plugin
@lingui/vite-plugin
is a Vite plugin, which compiles Lingui catalogs on the fly and provides additional required configuration for Vite.
Installation
Install @lingui/vite-plugin
as a development dependency:
- npm
- Yarn
- pnpm
npm install --save-dev @lingui/vite-plugin
yarn add --dev @lingui/vite-plugin
pnpm add --save-dev @lingui/vite-plugin
For a complete installation guide, see Installation and Setup.
Usage
Simply add @lingui/vite-plugin
inside your vite.config.ts
:
vite.config.ts
import { UserConfig } from "vite";
import { lingui } from "@lingui/vite-plugin";
const config: UserConfig = {
plugins: [lingui()],
};
Then in your code all you need is to use dynamic imports to load only necessary catalog. Extension is mandatory.
export async function dynamicActivate(locale: string) {
const { messages } = await import(`./locales/${locale}.po`);
i18n.load(locale, messages);
i18n.activate(locale);
}
tip
If you are using a format that has a different extension than *.po
, you need to specify the ?lingui
suffix:
const { messages } = await import(`./locales/${language}.json?lingui`);