From 6e43b9d1b04623503f6b061a0e989badd3d6b5e6 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Mon, 22 Jul 2019 11:14:11 +0100 Subject: [PATCH 1/2] 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 } }` --- cli.js | 4 +++ extract-react-intl/index.js | 9 +++-- .../test/__snapshots__/test.js.snap | 33 +++++++++++++++++++ .../test/fixtures/components/App/messages.js | 2 ++ .../fixtures/components/Greeting/messages.js | 1 + extract-react-intl/test/test.js | 9 +++++ 6 files changed, 56 insertions(+), 2 deletions(-) diff --git a/cli.js b/cli.js index c890574..df84382 100755 --- a/cli.js +++ b/cli.js @@ -46,6 +46,10 @@ const cli = meow( }, 'module-name': { type: 'string' + }, + withDescriptions: { + type: 'boolean', + default: false } } } diff --git a/extract-react-intl/index.js b/extract-react-intl/index.js index ef1a7ab..92d4f5d 100644 --- a/extract-react-intl/index.js +++ b/extract-react-intl/index.js @@ -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) @@ -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 diff --git a/extract-react-intl/test/__snapshots__/test.js.snap b/extract-react-intl/test/__snapshots__/test.js.snap index db290b2..ca4e056 100644 --- a/extract-react-intl/test/__snapshots__/test.js.snap +++ b/extract-react-intl/test/__snapshots__/test.js.snap @@ -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": "", + }, + }, +} +`; diff --git a/extract-react-intl/test/fixtures/components/App/messages.js b/extract-react-intl/test/fixtures/components/App/messages.js index 9765bc5..a5d972d 100644 --- a/extract-react-intl/test/fixtures/components/App/messages.js +++ b/extract-react-intl/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/extract-react-intl/test/fixtures/components/Greeting/messages.js b/extract-react-intl/test/fixtures/components/Greeting/messages.js index 9e6d0d9..9e2c5ae 100644 --- a/extract-react-intl/test/fixtures/components/Greeting/messages.js +++ b/extract-react-intl/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/extract-react-intl/test/test.js b/extract-react-intl/test/test.js index 727e299..21574be 100644 --- a/extract-react-intl/test/test.js +++ b/extract-react-intl/test/test.js @@ -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() +}) From 02611145fcad6500bbef8956af6c3356e84325d7 Mon Sep 17 00:00:00 2001 From: akameco Date: Wed, 21 Aug 2019 03:55:45 +0900 Subject: [PATCH 2/2] Add @gmaclennan as a contributor --- .all-contributorsrc | 12 +++++++++++- readme.md | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index e14caec..2c63a9c 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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" } diff --git a/readme.md b/readme.md index aaf4448..2aa00c0 100644 --- a/readme.md +++ b/readme.md @@ -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). @@ -203,6 +203,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds nodaguti
nodaguti

💻 ⚠️ fix-fix
fix-fix

💻 bradbarrow
bradbarrow

🐛 💻 ⚠️ + Gregor MacLennan
Gregor MacLennan

💻