npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-intl-extractor-cli

v1.0.5

Published

CLI tool to extract messages JSON for react-intl library with smart merging

Downloads

4

Readme

React-intl-exporter CLI

This library adapts @formatjs/cli tool to export translations in a smart way.

React-intl-exporter CLI extracts all messages from the application and saves them for all specified languages. If a file already exists, the tool merges its content with currently extracted messages. Every message is an object with at least two fields:

  • defaultMessage - same like generated by the original tool; contains value of defaultMessage found in the application;
  • translatedMessage - it's a message defaultMessage translated to the current language; when generated for first time, it has the same value as defaultMessage; it should be translated manually; compilation process should take this values into account, not from defaultMessage fields.

If there is a new message not available in the JSON file, it will be added. If a message from JSON is not extracted, it will be removed from the file. If in JSON file is already a message with the same key but different defaultMessage than in generated, generated message will replace the existing one. If both key and defaultMessage are the same, message is not changed.

NOTE All commands here assumes that you use yarn tool. Using npm or any other tool doesn't change much. Please adapt an exact command to the tool you use.

Installation

You can add this tool to your application in these steps:

  1. Install the package:
yarn add react-intl-extractor-cli -D
  1. Add a new script to scripts section in your packages.json file, e.g.
"intl:extract": "intl_extractor extract "src/**/!(*.test).js" --languages en,de,pl",
  1. Create a file with formatter defined by the tool (to support translatedMessage instead of defaultMessage). Minimal file formatter.js is:
const defaultFormatter = require('react-intl-extractor-cli').defaultFormatter;
module.exports.default = defaultFormatter;
  1. When you use compiler from @formatjs/cli, specify --format <formatter_file> parameter where <formatter_file is a location of formatter.js file created in a previous step. Name of the file can be any, however in both steps must be the same.

Simple usage

You can add extraction and compilation commands to scripts section of package.json file:

"intl:extract": "intl_extractor extract \"src/**/!(*.test).js\" src/messages --languages en,pl",
"intl:compile": "formatjs compile-folder src/messages src/translations --format src/utils/compileFormatter.js",

Then you can use just: yarn intl:extract - to extract or update already extracted messages yarn intl:compile - to compile extracted (and manually translated) message files to files consumable by react-intl.

Commands

Extract

yarn intl_extractor extract [options] <inputFile> <outputFolder>

Most of the parameters used in this command are the same as for original formatjs extract command. You can check them here. Below are described differences.

<inputFile> is a pattern to determine in which files messages are located. Eg. "src/**/!(*.test).js".

<outputFolder> determines where output files with extracted messages should be located. It's also a location where currently existing message files are tried to be found.

Parameters

--languages <codes> It's the most important parameter as it specifies language codes for which translation messages should be generated. <codes> contain code strings separated by comma "," signs.

--verbose Using this parameter you inform the tool that you expect more details about processed languages and messages.

Help

intl_extractor help It just displays help message