Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 6aca983

Browse files
committed
Improve docs and error messages
Fixes #6 Fixes #8
1 parent 38d85ba commit 6aca983

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# babel-plugin-react-intl
22

3-
**THIS IS A PREVIEW RELEASE THAT WORKS WITH A FUTURE, UNRELEASED VERSION OF REACT INTL.**
3+
Extracts string messages for translation from modules that use [React Intl][].
44

5-
Extracts string messages from React components that use [React Intl][].
5+
_**Note:** This Babel plugin works with React Intl v2 which is [in development][v2-discussion]._
66

77
## Installation
88

@@ -12,6 +12,10 @@ $ npm install babel-plugin-react-intl
1212

1313
## Usage
1414

15+
**This Babel plugin only visits ES6 modules which `import` React Intl.**
16+
17+
The default message descriptors for the app's default language will be extracted from: `defineMessages()`, `<FormattedMessage>`, and `<FormattedHTMLMeessage>`; all of which are named exports of the React Intl package.
18+
1519
### Via `.babelrc` (Recommended)
1620

1721
**.babelrc**
@@ -30,9 +34,11 @@ $ npm install babel-plugin-react-intl
3034

3135
#### Options
3236

33-
- **`messagesDir`**: The target location where the plugin will output a `.json` file corresponding to each component from which React Intl messages were extracted.
37+
- **`messagesDir`**: 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 only be accessible via Babel's API.
3438

35-
- **`enforceDescriptions`**: Whether or not message declarations _must_ contain a `description` to provide context to translators.
39+
- **`enforceDescriptions`**: Whether or not message declarations _must_ contain a `description` to provide context to translators. Defaults to: `false`.
40+
41+
- **`moduleSourceName`**: 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.
3642

3743
### Via CLI
3844

@@ -42,6 +48,8 @@ $ babel --plugins react-intl script.js
4248

4349
### Via Node API
4450

51+
The extract message descriptors are available via the `metadata` property on the object returned from Babel's `transform()` API:
52+
4553
```javascript
4654
require("babel-core").transform("code", {
4755
plugins: ["react-intl"]
@@ -50,3 +58,4 @@ require("babel-core").transform("code", {
5058

5159

5260
[React Intl]: http://formatjs.io/react/
61+
[v2-discussion]: https://github.com/yahoo/react-intl/issues/162

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "babel-plugin-react-intl",
33
"version": "1.0.0-beta-4",
4-
"description": "Extracts string messages from modules that use react-intl.",
4+
"description": "Extracts string messages for translation from modules that use React Intl.",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/yahoo/babel-plugin-react-intl.git"

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ export default function ({Plugin, types: t}) {
263263
if (!(messageObj && messageObj.isObjectExpression())) {
264264
throw file.errorWithNode(node,
265265
`[React Intl] \`${callee.node.name}()\` must be ` +
266-
`called with message descriptor defined via an ` +
267-
`object expression.`
266+
`called with message descriptors defined as ` +
267+
`object expressions.`
268268
);
269269
}
270270

@@ -288,9 +288,9 @@ export default function ({Plugin, types: t}) {
288288
let callee = this.get('callee');
289289

290290
if (referencesImport(callee, moduleSourceName, FUNCTION_NAMES)) {
291-
let firstArg = this.get('arguments')[0];
291+
let messagesObj = this.get('arguments')[0];
292292

293-
firstArg.get('properties')
293+
messagesObj.get('properties')
294294
.map((prop) => prop.get('value'))
295295
.forEach(processMessageObject);
296296
}

0 commit comments

Comments
 (0)