Setup with Vite
The Lingui Vite integration:
- Supports both @vitejs/plugin-react and @vitejs/plugin-react-swc
- Compiles .pocatalogs on the fly
Setup with @vitejs/plugin-react
@vitejs/plugin-react uses Babel to transform your code. LinguiJS relies on @lingui/babel-plugin-lingui-macro to compile JSX to ICU Message Format and for automatic ID generation.
- 
Install @lingui/cli,@lingui/vite-plugin,@lingui/babel-plugin-lingui-macroas development dependencies and@lingui/reactas a runtime dependency:- npm
- Yarn
- pnpm
 npm install --save-dev @lingui/cli @lingui/vite-plugin @lingui/babel-plugin-lingui-macro
 npm install --save @lingui/reactyarn add --dev @lingui/cli @lingui/vite-plugin @lingui/babel-plugin-lingui-macro
 yarn add @lingui/reactpnpm add --save-dev @lingui/cli @lingui/vite-plugin @lingui/babel-plugin-lingui-macro
 pnpm add @lingui/react
- 
Setup Lingui in vite.config.ts:info@vitejs/plugin-reactdoes not use babel config (e.g.babel.rc) from your project by default. You have to enable it manually or specify babel options directly invite.config.tsvite.config.tsimport { defineConfig } from "vite";
 import react from "@vitejs/plugin-react";
 import { lingui } from "@lingui/vite-plugin";
 export default defineConfig({
 plugins: [
 react({
 babel: {
 plugins: ["@lingui/babel-plugin-lingui-macro"],
 },
 }),
 lingui(),
 ],
 });
Setup with @vitejs/plugin-react-swc
@vitejs/plugin-react-swc uses SWC to transform your code, which is 20x faster than Babel. LinguiJS relies on @lingui/swc-plugin to compile JSX to ICU Message Format and for automatic ID generation.
- 
Install @lingui/cli,@lingui/swc-pluginas development dependencies and@lingui/reactas a runtime dependency:- npm
- Yarn
- pnpm
 npm install --save-dev @lingui/cli @lingui/vite-plugin @lingui/swc-plugin
 npm install --save @lingui/reactyarn add --dev @lingui/cli @lingui/vite-plugin @lingui/swc-plugin
 yarn add @lingui/reactpnpm add --save-dev @lingui/cli @lingui/vite-plugin @lingui/swc-plugin
 pnpm add @lingui/reactnoteSWC Plugin support is still experimental. Semver backwards compatibility between different @swc/coreversions is not guaranteed.You need to select an appropriate version of the @lingui/swc-pluginto match compatible@swc/coreversion.The version of @swc/coreis specified within the@vitejs/plugin-react-swcpackage.To ensure that the resolved version of @swc/coreis one of the supported versions, you may utilize theresolutionsfield in thepackage.jsonfile, which is supported by Yarn:"resolutions": {
 "@swc/core": "1.3.56"
 },or overridesfor >npm@8.3"overrides": {
 "@swc/core": "1.3.56"
 },For more information about compatibility, see the plugins.swc.rs page. 
- 
Setup Lingui in vite.config.ts:vite.config.tsimport { defineConfig } from "vite";
 import react from "@vitejs/plugin-react-swc";
 import { lingui } from "@lingui/vite-plugin";
 export default defineConfig({
 plugins: [
 react({
 plugins: [["@lingui/swc-plugin", {}]],
 }),
 lingui(),
 ],
 });
Further Setup
- 
Create a lingui.config.tsfile with LinguiJS configuration in the root of your project (next topackage.json). Replacesrcwith the directory name where you have source files:lingui.config.tsimport type { LinguiConfig } from "@lingui/conf";
 const config: LinguiConfig = {
 locales: ["en", "cs", "fr"],
 catalogs: [
 {
 path: "<rootDir>/src/locales/{locale}",
 include: ["src"],
 },
 ],
 };
 export default config;The PO format is recommended for message catalogs, and could be compiled on the fly thanks to @lingui/vite-plugin. See Catalog Formats for other available formats.
- 
Add the following scripts to your package.json:package.json{
 "scripts": {
 "messages:extract": "lingui extract"
 }
 }
- 
Check the installation by running: - npm
- Yarn
- pnpm
 npm run messages:extractyarn messages:extractpnpm run messages:extractThere should be no error and you should see output similar following: - npm
- Yarn
- pnpm
 > npm run messages:extract
 Catalog statistics:
 ┌──────────┬─────────────┬─────────┐
 │ Language │ Total count │ Missing │
 ├──────────┼─────────────┼─────────┤
 │ cs │ 0 │ 0 │
 │ en │ 0 │ 0 │
 │ fr │ 0 │ 0 │
 └──────────┴─────────────┴─────────┘
 (use "lingui extract" to update catalogs with new messages)
 (use "lingui compile" to compile catalogs for production)> yarn messages:extract
 Catalog statistics:
 ┌──────────┬─────────────┬─────────┐
 │ Language │ Total count │ Missing │
 ├──────────┼─────────────┼─────────┤
 │ cs │ 0 │ 0 │
 │ en │ 0 │ 0 │
 │ fr │ 0 │ 0 │
 └──────────┴─────────────┴─────────┘
 (use "lingui extract" to update catalogs with new messages)
 (use "lingui compile" to compile catalogs for production)> pnpm run messages:extract
 Catalog statistics:
 ┌──────────┬─────────────┬─────────┐
 │ Language │ Total count │ Missing │
 ├──────────┼─────────────┼─────────┤
 │ cs │ 0 │ 0 │
 │ en │ 0 │ 0 │
 │ fr │ 0 │ 0 │
 └──────────┴─────────────┴─────────┘
 (use "lingui extract" to update catalogs with new messages)
 (use "lingui compile" to compile catalogs for production)This command should create .pocatalogs in thesrc/locales/folder:src
 └── locales
 ├── cs.po
 ├── en.po
 └── fr.po
- 
Import .pothose files directly in your Vite processed code:export async function dynamicActivate(locale: string) {
 const { messages } = await import(`./locales/${locale}.po`);
 i18n.load(locale, messages);
 i18n.activate(locale);
 }
See the Dynamic loading of message catalogs for more info.
Don't miss the Lingui ESLint Plugin which can help you find and prevent common l10n mistakes in your code.