Setup with React project
If you use Create React App, even ejected, follow LinguiJS and Create React App setup guide.
This setup guide is for any project which uses React.
Install
Install
@lingui/cli,@lingui/macro,babel-plugin-macrosand Babel 1 core packages as a development dependencies and@lingui/reactas a runtime dependency.- npm
- Yarn
- pnpm
npm install --save-dev @lingui/cli @babel/core
npm install --save-dev @lingui/macro babel-plugin-macros
npm install --save @lingui/reactyarn add --dev @lingui/cli @babel/core
yarn add --dev @lingui/macro babel-plugin-macros
yarn add @lingui/reactpnpm add --save-dev @lingui/cli @babel/core
pnpm add --save-dev @lingui/macro babel-plugin-macros
pnpm add @lingui/reacttipFor those who prefer not to use Babel, Lingui offers the SWC Plugin as an alternative.
noteIt's recommended to install
@lingui/macropackage as a production dependency rather than development one to avoidimport/no-extraneous-dependencieserrors in ESLint.Add
macrosplugin to Babel config (e.g:.babelrc):{
"plugins": ["macros"]
}infoIf you use any preset, check first if it contains
macrosplugin. These presets already includesmacrosplugin:react-scriptsCreate
lingui.config.jsfile with LinguiJS configuration in root of your project (next topackage.json). Replacesrcwith the directory name where you have source files:lingui.config.js/** @type {import('@lingui/conf').LinguiConfig} */
module.exports = {
locales: ["en", "cs", "fr"],
catalogs: [
{
path: "<rootDir>/src/locales/{locale}/messages",
include: ["src"],
},
],
format: "po",
};PO format is recommended for message catalogs. See
formatdocumentation for other available formats.Add following scripts to your
package.json:package.json{
"scripts": {
"extract": "lingui extract",
"compile": "lingui compile"
}
}
If you use TypeScript, you can add --typescript flag to compile script to produce compiled message catalogs with TypeScript types.
Check the installation by running:
- npm
- Yarn
- pnpm
npm run extractyarn extractpnpm run extractThere should be no error and you should see output similar following:
- npm
- Yarn
- pnpm
> npm run 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 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 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)
Don't miss the Lingui ESLint Plugin which can help you find and prevent common l10n mistakes in your code.
Congratulations! You've successfully set up project with LinguiJS. Now it's good time to follow React tutorial or read about ICU Message Format which is used in messages.
Further reading
Checkout these reference guides for full documentation:
- Internationalization of React apps
- Common i18n patterns in React
@lingui/reactreference documentation- ICU Message Format
- CLI reference
- Configuration reference
- For those who prefer not to use Babel, Lingui offers the SWC Plugin as an alternative.↩