Setup with React project¶
Important
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-macros and Babel core packages as a development dependencies and@lingui/reactas a runtime dependency.npm install --save-dev @lingui/cli @babel/core npm install --save-dev @lingui/macro babel-plugin-macros # required for macros npm install --save @lingui/react # or using Yarn yarn add --dev @lingui/cli @babel/core yarn add --dev @lingui/macro babel-plugin-macros # required for macros yarn add @lingui/react
In case you get errors with
import/no-extraneous-dependencieseslint rule feel free to add the dependencies as non-devnpm install --save-dev @lingui/cli @babel/core npm install --save-dev babel-plugin-macros # required for macros npm install --save @lingui/macro @lingui/react # or using Yarn yarn add --dev @lingui/cli @babel/core yarn add --dev babel-plugin-macros # required for macros yarn add @lingui/macro @lingui/react
Add
macrosplugin to Babel config (e.g:.babelrc):{ "plugins": [ "macros" ] }
Important
If you use any preset, check first if it contains
macrosplugin. These presets already includesmacrosplugin:react-scripts
Create
.linguircfile with LinguiJS configuration in root of your project (next topackage.json). Replacesrcwith a directory name where you have source files:{ "locales": ["en", "cs"], "catalogs": [{ "path": "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:{ "scripts": { "extract": "lingui extract", "compile": "lingui compile", } }
Check the installation by running
npm run extract(oryarn extract):npm run extract # or using Yarn yarn extract
There should be no error and you should see output similar following:
> 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)
Congratulations! You’ve sucessfully 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: