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. Boostrap your project with following commands:
npx create-react-app my-app cd my-app
Install
@lingui/cli
,@lingui/macro
and Babel core packages as a development dependencies and@lingui/react
as a runtime dependency.npm install --save-dev @lingui/cli @lingui/macro @babel/core babel-core@bridge npm install --save @lingui/react # or using Yarn yarn add --dev @lingui/cli @lingui/macro @babel/core babel-core@bridge yarn add @lingui/react
Important
Don’t forget Babel packages (
@babel/core
,babel-core@bridge
) which are required for backward compatibility with projects using Babel 6.Create
.linguirc
file with LinguiJS configuration in root of your project (next topackage.json
):{ "localeDir": "src/locales/", "srcPathDirs": ["src/"], "format": "po", "sourceLocale": "en" }
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.Add following scripts to your
package.json
:{ "scripts": { "add-locale": "lingui add-locale", "extract": "lingui extract", "compile": "lingui compile" } }
Run
npm run add-locale
(oryarn add-locale
) with locale codes you would like to use in your app:npm run add-locale en es fr # Add English, Spanish and French locale # or using Yarn yarn add-locale en es fr
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 add-locale <language>" to add more locales) (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:
Troubleshooting¶
Cannot find module ‘babel-core’¶
Check that you installed
babel-core@bridge
and@babel/core
(step 2)Check that you use local version of
@lingui/cli
. Installing@lingui/cli
globally may clash with other packages which use Babel 6. You should run eithernpm run extract
oryarn extract
(step 5).