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-appInstall
@lingui/cli,@lingui/macroand Babel core packages as a development dependencies and@lingui/reactas 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/reactImportant
Don’t forget Babel packages (
@babel/core,babel-core@bridge) which are required for backward compatibility with projects using Babel 6.Create
.linguircfile 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
srcdirectory 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. Seeformatdocumentation 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 extractThere 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@bridgeand@babel/core(step 2)Check that you use local version of
@lingui/cli. Installing@lingui/cliglobally may clash with other packages which use Babel 6. You should run eithernpm run extractoryarn extract(step 5).