From 94bedde3e5eb2139845854a7ba1e9497e1482e50 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Mon, 22 Jul 2019 11:14:11 +0100 Subject: [PATCH] feat: Add option to include message descriptions in export Adds an option `descriptions` to include the description from `defineMessages`. Changes the returned object from `{ [locale]: message }` to `{ [locale]: { message: message, description: description } }` --- .all-contributorsrc | 9 ++++ index.js | 9 ++-- readme.md | 49 +++++++++++++++++-- test/__snapshots__/test.js.snap | 33 +++++++++++++ test/fixtures/components/App/messages.js | 2 + test/fixtures/components/Greeting/messages.js | 1 + test/test.js | 9 ++++ 7 files changed, 106 insertions(+), 6 deletions(-) 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 @@ [![Build Status](https://travis-ci.org/akameco/extract-react-intl.svg?branch=master)](https://travis-ci.org/akameco/extract-react-intl) [![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) -[![All Contributors](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-7-orange.svg?style=flat-square)](#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
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
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
fix-fix](https://github.com/fix-fix)
[💻](https://github.com/akameco/extract-react-intl/commits?author=fix-fix "Code") | [enrique-ramirez
enrique-ramirez](https://github.com/enrique-ramirez)
[📖](https://github.com/akameco/extract-react-intl/commits?author=enrique-ramirez "Documentation") | [bradbarrow
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
Filip "Filson" Pasternak](https://github.com/Filson14)
[💻](https://github.com/akameco/extract-react-intl/commits?author=Filson14 "Code") | -| :---: | :---: | :---: | :---: | :---: | :---: | +| [akameco
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
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
fix-fix](https://github.com/fix-fix)
[💻](https://github.com/akameco/extract-react-intl/commits?author=fix-fix "Code") | [enrique-ramirez
enrique-ramirez](https://github.com/enrique-ramirez)
[📖](https://github.com/akameco/extract-react-intl/commits?author=enrique-ramirez "Documentation") | [bradbarrow
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
Filip "Filson" Pasternak](https://github.com/Filson14)
[💻](https://github.com/akameco/extract-react-intl/commits?author=Filson14 "Code") | [Gregor MacLennan
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() +})