What is @formatjs/cli?
@formatjs/cli is a command-line tool for internationalizing JavaScript applications. It provides functionalities for extracting, compiling, and managing translation messages, making it easier to handle localization in your projects.
What are @formatjs/cli's main functionalities?
Extract Messages
This command extracts all the translation messages from the JavaScript files in the 'src' directory and outputs them to a JSON file named 'en.json'.
npx formatjs extract 'src/**/*.js' --out-file lang/en.json
Compile Messages
This command compiles the extracted messages from 'en.json' into a format that can be used by the application, outputting the result to 'en-compiled.json'.
npx formatjs compile lang/en.json --out-file lang/en-compiled.json
Compile Messages with Pseudo-Localization
This command compiles the messages with pseudo-localization, which is useful for testing how the application handles different languages and character sets. The output is saved to 'en-pseudo.json'.
npx formatjs compile lang/en.json --pseudo-locale --out-file lang/en-pseudo.json
Format Messages
This command formats the extracted messages in 'en.json' to ensure consistency and readability, saving the formatted messages to 'en-formatted.json'.
npx formatjs format lang/en.json --out-file lang/en-formatted.json
Other packages similar to @formatjs/cli
i18next
i18next is a popular internationalization framework for JavaScript. It provides a complete solution for localizing your application, including features for translation management, language detection, and interpolation. Compared to @formatjs/cli, i18next offers a more comprehensive set of tools for managing translations and supports a wider range of use cases.
react-intl
react-intl is a library for internationalizing React applications. It provides React components and an API for formatting dates, numbers, and strings, as well as handling pluralization and gender-specific messages. While @formatjs/cli focuses on command-line tools for managing translations, react-intl integrates directly with React to provide runtime localization support.
formatjs-cli
This CLI allows you to extract intl-messageformat
messages from JavaScript / TypeScript source code,
including React components that uses react-intl
.
Usage
$ npm -g i @formatjs/cli
$ formatjs extract --help
Usage: formatjs extract [options] [files...]
Usage: formatjs extract [options] [files...]
Extract string messages from React components that use react-intl.
The input language is expected to be TypeScript or ES2017 with JSX.
Options:
--messages-dir <dir> The target location where the plugin will output a `.json` file corresponding to each component from which React Intl messages were extracted. If not provided, the extracted message
descriptors will be printed to standard output.
--out-file <path> The target file path where the plugin will output an aggregated `.json` file of allthe translations from the `files` supplied.
This flag will ignore --messages-dir
--id-interpolation-pattern <pattern> If certain message descriptors don't have id, this `pattern` will be used to automaticallygenerate IDs for them. Default to `[contenthash:5]`.
See https://github.com/webpack/loader-utils#interpolatename for sample patterns
--extract-source-location Whether the metadata about the location of the message in the source file should be extracted. If `true`, then `file`, `start`, and `end` fields will exist for each extracted
message descriptors. (default: false)
--module-source-name <name> The ES6 module source name of the React Intl package. Defaults to: `"react-intl"`, but can be changed to another name/path to React Intl.
--remove-default-message Remove `defaultMessage` field in generated js after extraction (default: false)
--additional-component-names <comma-separated-names> Additional component names to extract messages from, e.g: `['FormattedFooBarMessage']`. **NOTE**: By default we check for the fact that `FormattedMessage` is imported from
`moduleSourceName` to make sure variable alias works. This option does not do that so it's less safe.
--extract-from-format-message-call Opt-in to extract from `intl.formatMessage` call with the same restrictions, e.g: has to be called with object literal such as `intl.formatMessage({ id: 'foo', defaultMessage:
'bar', description: 'baz'})` (default: false)
--output-empty-json Output file with empty [] if src has no messages. For build systems like bazel that relies on specific output mapping, not writing out a file can cause issues. (default: false)
--ignore <files> List of glob paths to **not** extract translations from.
--throws Whether to throw an exception when we fail to process any file in the batch.
--pragma <pragma> parse specific additional custom pragma. This allows you to tag certain file with metadata such as `project`. For example with this file:
```
// @intl-meta project:my-custom-project
import {FormattedMessage} from 'react-intl';
<FormattedMessage defaultMessage="foo" id="bar" />;
```
and with option `{pragma: "@intl-meta"}`, we'll parse out `// @intl-meta project:my-custom-project` into `{project: 'my-custom-project'}` in the result file.
-h, --help display help for command