Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,17 @@
"code",
"test"
]
},
{
"login": "gmaclennan",
"name": "Gregor MacLennan",
"avatar_url": "https://avatars1.githubusercontent.com/u/290457?v=4",
"profile": "http://ddem.us/",
"contributions": [
"code"
]
}
],
"repoType": "github"
"repoType": "github",
"commitConvention": "none"
}
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const cli = meow(
},
'module-name': {
type: 'string'
},
withDescriptions: {
type: 'boolean',
default: false
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions extract-react-intl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = async (locales, pattern, opts = {}) => {

const defaultLocale = opts.defaultLocale || 'en'
const cwd = opts.cwd || process.cwd()
const withDescriptions = opts.withDescriptions || false

const babelrc = getBabelrc(cwd) || {}
const babelrcDir = getBabelrcDir(cwd)
Expand All @@ -79,10 +80,14 @@ module.exports = async (locales, pattern, opts = {}) => {
const { metadata: result } = await pify(transformFile)(file, babelOpts)
const localeObj = localeMap(locales)
// eslint-disable-next-line no-unused-vars
for (const { id, defaultMessage } of result['react-intl'].messages) {
for (const { id, defaultMessage, description } of result['react-intl']
.messages) {
// eslint-disable-next-line no-unused-vars
for (const locale of locales) {
localeObj[locale][id] = defaultLocale === locale ? defaultMessage : ''
const message = defaultLocale === locale ? defaultMessage : ''
localeObj[locale][id] = withDescriptions
? { message, description }
: message
}
}
return localeObj
Expand Down
33 changes: 33 additions & 0 deletions extract-react-intl/test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,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": "",
},
},
}
`;
2 changes: 2 additions & 0 deletions extract-react-intl/test/fixtures/components/App/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { defineMessages } from 'react-intl'

export default defineMessages({
// hello message description
hello: 'hello',
// world message description
world: 'world'
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { defineMessages } from 'react-intl'

export default defineMessages({
// Welcome message description
welcome: {
id: 'components/Greeting/welcome',
defaultMessage: `
Expand Down
9 changes: 9 additions & 0 deletions extract-react-intl/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,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',
withDescriptions: true
})
expect(x).toMatchSnapshot()
})
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![MIT License](https://img.shields.io/npm/l/nps.svg?style=flat-square)](./license)
[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors-)

This package will generate json or yaml files from a glob. It will generate one file per locale, 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).

Expand Down Expand Up @@ -203,6 +203,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
<td align="center"><a href="http://about.me/nodaguti"><img src="https://avatars0.githubusercontent.com/u/27622?v=4" width="100px;" alt="nodaguti"/><br /><sub><b>nodaguti</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=nodaguti" title="Code">💻</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=nodaguti" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/fix-fix"><img src="https://avatars1.githubusercontent.com/u/11943024?v=4" width="100px;" alt="fix-fix"/><br /><sub><b>fix-fix</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=fix-fix" title="Code">💻</a></td>
<td align="center"><a href="http://bradbarrow.com"><img src="https://avatars3.githubusercontent.com/u/1264276?v=4" width="100px;" alt="bradbarrow"/><br /><sub><b>bradbarrow</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/issues?q=author%3Abradbarrow" title="Bug reports">🐛</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=bradbarrow" title="Code">💻</a> <a href="https://github.com/akameco/extract-react-intl-messages/commits?author=bradbarrow" title="Tests">⚠️</a></td>
<td align="center"><a href="http://ddem.us/"><img src="https://avatars1.githubusercontent.com/u/290457?v=4" width="100px;" alt="Gregor MacLennan"/><br /><sub><b>Gregor MacLennan</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=gmaclennan" title="Code">💻</a></td>
</tr>
</table>

Expand Down