diff --git a/.all-contributorsrc b/.all-contributorsrc
index 88d969f..ebe2145 100644
--- a/.all-contributorsrc
+++ b/.all-contributorsrc
@@ -66,6 +66,15 @@
"contributions": [
"code"
]
+ },
+ {
+ "login": "gmaclennan",
+ "name": "Gregor MacLennan",
+ "avatar_url": "https://avatars1.githubusercontent.com/u/290457?v=4",
+ "profile": "http://ddem.us/",
+ "contributions": [
+ "code"
+ ]
}
],
"repoType": "github"
diff --git a/index.js b/index.js
index 86427e6..1408868 100644
--- a/index.js
+++ b/index.js
@@ -78,10 +78,13 @@ module.exports = async (locales, pattern, opts) => {
plugins: resolvePlugins(plugins, babelrcDir)
})
const localeObj = localeMap(locales)
- for (const { id, defaultMessage } of result['react-intl'].messages) {
+ for (const { id, defaultMessage, description } of result['react-intl']
+ .messages) {
for (const locale of locales) {
- localeObj[locale][id] =
- opts.defaultLocale === locale ? defaultMessage : ''
+ const message = opts.defaultLocale === locale ? defaultMessage : ''
+ localeObj[locale][id] = opts.descriptions
+ ? { message, description }
+ : message
}
}
return localeObj
diff --git a/readme.md b/readme.md
index 4a748af..7b44927 100644
--- a/readme.md
+++ b/readme.md
@@ -3,7 +3,7 @@
[](https://travis-ci.org/akameco/extract-react-intl)
[](https://github.com/facebook/jest)
[](https://github.com/prettier/prettier)
-[](#contributors)
+[](#contributors)
This package allows you to extract all messages from a glob. It will return an object with a key for each locale you pass, which in turn contains an object with the ids of each message defined by the [`defineMessages`](https://github.com/yahoo/react-intl/wiki/API#definemessages) function of [react-intl](https://github.com/yahoo/react-intl). The value of each of these keys will be an empty string, except for your `defaultLocale` which will be populated with the [`defaultMessage`](https://github.com/yahoo/react-intl/wiki/API#message-descriptor).
@@ -70,6 +70,49 @@ Type: `string`
Example: `./path/to/module`
Default: `react-intl`
The ES6 module source name of the React Intl package. Defines from where _defineMessages_, `` and `` are imported.
+#### descriptions
+
+Type: `boolean`
Default: `false`
+
+Includes message descriptions in the returned object. Each message id property
+will be an object with properties `message` and `description` instead of a
+message string e.g.
+
+```js
+const extractReactIntl = require('extract-react-intl')
+
+const pattern = 'app/**/*.js'
+const locales = ['en', 'ja']
+
+extractReactIntl(locales, pattern).then(result => {
+ console.log(result)
+ /*
+{
+ en: {
+ 'components/App/hello': {
+ message: 'hello',
+ description: 'hello message description'
+ },
+ 'components/App/welcome': {
+ message: 'welcome to extract-react-intl',
+ description: 'welcome message description
+ }
+ },
+ ja: {
+ 'components/App/hello': {
+ message: '',
+ description: 'hello message description'
+ },
+ 'components/App/welcome': {
+ message: '',
+ description: 'welcome message description
+ }
+ }
+}
+ */
+})
+```
+
##### cwd
Type: `string`
Default: `.`
@@ -85,8 +128,8 @@ Thanks goes to these wonderful people
-| [
akameco](http://akameco.github.io)
[💻](https://github.com/akameco/extract-react-intl/commits?author=akameco "Code") [📖](https://github.com/akameco/extract-react-intl/commits?author=akameco "Documentation") [⚠️](https://github.com/akameco/extract-react-intl/commits?author=akameco "Tests") [🚇](#infra-akameco "Infrastructure (Hosting, Build-Tools, etc)") | [
nodaguti](http://about.me/nodaguti)
[💻](https://github.com/akameco/extract-react-intl/commits?author=nodaguti "Code") [⚠️](https://github.com/akameco/extract-react-intl/commits?author=nodaguti "Tests") | [
fix-fix](https://github.com/fix-fix)
[💻](https://github.com/akameco/extract-react-intl/commits?author=fix-fix "Code") | [
enrique-ramirez](https://github.com/enrique-ramirez)
[📖](https://github.com/akameco/extract-react-intl/commits?author=enrique-ramirez "Documentation") | [
bradbarrow](http://bradbarrow.com)
[🐛](https://github.com/akameco/extract-react-intl/issues?q=author%3Abradbarrow "Bug reports") [💻](https://github.com/akameco/extract-react-intl/commits?author=bradbarrow "Code") [⚠️](https://github.com/akameco/extract-react-intl/commits?author=bradbarrow "Tests") | [
Filip "Filson" Pasternak](https://github.com/Filson14)
[💻](https://github.com/akameco/extract-react-intl/commits?author=Filson14 "Code") |
-| :---: | :---: | :---: | :---: | :---: | :---: |
+| [
akameco](http://akameco.github.io)
[💻](https://github.com/akameco/extract-react-intl/commits?author=akameco "Code") [📖](https://github.com/akameco/extract-react-intl/commits?author=akameco "Documentation") [⚠️](https://github.com/akameco/extract-react-intl/commits?author=akameco "Tests") [🚇](#infra-akameco "Infrastructure (Hosting, Build-Tools, etc)") | [
nodaguti](http://about.me/nodaguti)
[💻](https://github.com/akameco/extract-react-intl/commits?author=nodaguti "Code") [⚠️](https://github.com/akameco/extract-react-intl/commits?author=nodaguti "Tests") | [
fix-fix](https://github.com/fix-fix)
[💻](https://github.com/akameco/extract-react-intl/commits?author=fix-fix "Code") | [
enrique-ramirez](https://github.com/enrique-ramirez)
[📖](https://github.com/akameco/extract-react-intl/commits?author=enrique-ramirez "Documentation") | [
bradbarrow](http://bradbarrow.com)
[🐛](https://github.com/akameco/extract-react-intl/issues?q=author%3Abradbarrow "Bug reports") [💻](https://github.com/akameco/extract-react-intl/commits?author=bradbarrow "Code") [⚠️](https://github.com/akameco/extract-react-intl/commits?author=bradbarrow "Tests") | [
Filip "Filson" Pasternak](https://github.com/Filson14)
[💻](https://github.com/akameco/extract-react-intl/commits?author=Filson14 "Code") | [
Gregor MacLennan](http://ddem.us/)
[💻](https://github.com/akameco/extract-react-intl/commits?author=gmaclennan "Code") |
+| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
diff --git a/test/__snapshots__/test.js.snap b/test/__snapshots__/test.js.snap
index 1329798..a6fdced 100644
--- a/test/__snapshots__/test.js.snap
+++ b/test/__snapshots__/test.js.snap
@@ -30,3 +30,36 @@ Object {
},
}
`;
+
+exports[`extract from file with descriptions 1`] = `
+Object {
+ "en": Object {
+ "components.App.hello": Object {
+ "description": "hello message description",
+ "message": "hello",
+ },
+ "components.App.world": Object {
+ "description": "world message description",
+ "message": "world",
+ },
+ "components/Greeting/welcome": Object {
+ "description": "Welcome message description",
+ "message": "Welcome {name}, you have received {unreadCount, plural, =0 {no new messages} one {{formattedUnreadCount} new message} other {{formattedUnreadCount} new messages}} since {formattedLastLoginTime}.",
+ },
+ },
+ "ja": Object {
+ "components.App.hello": Object {
+ "description": "hello message description",
+ "message": "",
+ },
+ "components.App.world": Object {
+ "description": "world message description",
+ "message": "",
+ },
+ "components/Greeting/welcome": Object {
+ "description": "Welcome message description",
+ "message": "",
+ },
+ },
+}
+`;
diff --git a/test/fixtures/components/App/messages.js b/test/fixtures/components/App/messages.js
index 9765bc5..a5d972d 100644
--- a/test/fixtures/components/App/messages.js
+++ b/test/fixtures/components/App/messages.js
@@ -2,6 +2,8 @@
import { defineMessages } from 'react-intl'
export default defineMessages({
+ // hello message description
hello: 'hello',
+ // world message description
world: 'world'
})
diff --git a/test/fixtures/components/Greeting/messages.js b/test/fixtures/components/Greeting/messages.js
index 9e6d0d9..9e2c5ae 100644
--- a/test/fixtures/components/Greeting/messages.js
+++ b/test/fixtures/components/Greeting/messages.js
@@ -2,6 +2,7 @@
import { defineMessages } from 'react-intl'
export default defineMessages({
+ // Welcome message description
welcome: {
id: 'components/Greeting/welcome',
defaultMessage: `
diff --git a/test/test.js b/test/test.js
index d7929c6..e5f80ba 100644
--- a/test/test.js
+++ b/test/test.js
@@ -30,3 +30,12 @@ test('error', async () => {
expect(error.message).toMatch('File not found')
})
})
+
+test('extract from file with descriptions', async () => {
+ process.env.BABEL_ENV = 'react-intl'
+ const x = await m(locales, pattern, {
+ cwd: './test/fixtures',
+ descriptions: true
+ })
+ expect(x).toMatchSnapshot()
+})