Setup with Create React App
Create React App is a framework for writing React apps with no build configuration. This guide suppose you use Create React App 2.0 (the default version).
Install​
Follow Create React App documentation for more info. Bootstrap your project with following commands:
npx create-react-app my-app
cd my-appInstall
@lingui/cli
,@lingui/macro
and Babel core packages as a development dependencies and@lingui/react
as a runtime dependency.- npm
- Yarn
npm install --save-dev @lingui/cli @lingui/macro
npm install --save @lingui/reactyarn add --dev @lingui/cli @lingui/macro
yarn add @lingui/reactIn case you get errors with
import/no-extraneous-dependencies
eslint rule feel free to add the dependencies as non-dev- npm
- Yarn
npm install --save-dev @lingui/cli
npm install --save @lingui/macro @lingui/reactyarn add --dev @lingui/cli
yarn add @lingui/macro @lingui/reactCreate
lingui.config.js
file with LinguiJS configuration in root of your project (next topackage.json
):lingui.config.js/** @type {import('@lingui/conf').LinguiConfig} */
module.exports = {
locales: ["en", "cs", "fr"],
sourceLocale: "en",
catalogs: [
{
path: "<rootDir>/src/locales/{locale}/messages",
include: ["src"],
},
],
format: "po",
};This configuration will extract messages from source files inside
src
directory and write them into message catalogs insrc/locales
(English catalog would be in e.g:src/locales/en/messages.po
). Finally, PO format is recommended. Seeformat
documentation for other available formats.Optionally, add following scripts to your
package.json
for convenience:package.json{
"scripts": {
"extract": "lingui extract",
"compile": "lingui compile"
}
}Check the installation by running extract command:
- npm
- Yarn
npm run extract
yarn extract
There should be no error and you can find extracted messages in
src/locales
.
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: