From 1d9d7f87a8d4e736c9ca279010450c88127b5807 Mon Sep 17 00:00:00 2001 From: Filip Pasternak Date: Tue, 22 Jan 2019 17:07:36 +0100 Subject: [PATCH 1/5] Enable to specify custom react-intl module source name/path (allowed by modified extract-react-intl) --- cli.js | 4 ++++ index.js | 14 ++++++++++---- readme.md | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cli.js b/cli.js index a1198cf..8281d9d 100755 --- a/cli.js +++ b/cli.js @@ -16,6 +16,7 @@ const cli = meow( -d, --default-locale default locale --flat json [default: true] | yaml [default: false] --delimiter json | yaml [default: .] + --module-name module source name from where components are imported Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' @@ -46,6 +47,9 @@ const cli = meow( delimiter: { type: 'string', default: '.' + }, + 'module-name': { + type: 'string' } } } diff --git a/index.js b/index.js index 19b5f58..14fe8a1 100644 --- a/index.js +++ b/index.js @@ -80,15 +80,21 @@ module.exports = async (locales, pattern, buildDir, opts) => { const ext = isJson(opts.format) ? 'json' : 'yml' - const { defaultLocale } = opts + const { defaultLocale, moduleName } = opts const delimiter = opts.delimiter ? opts.delimiter : '.' const oldLocaleMaps = loadLocaleFiles(locales, buildDir, ext, delimiter) - const newLocaleMaps = await extractReactIntl(locales, pattern, { - defaultLocale - }) + const extractorOptions = { defaultLocale } + + moduleName && (extractorOptions.moduleSourceName = moduleName) + + const newLocaleMaps = await extractReactIntl( + locales, + pattern, + extractorOptions + ) return Promise.all( locales.map(locale => { diff --git a/readme.md b/readme.md index 7edf550..5febe54 100644 --- a/readme.md +++ b/readme.md @@ -93,6 +93,7 @@ $ extract-messages --help --flat json [default: true] | yaml [default: false] --default-locale default locale [default: en] --delimiter json | yaml [default: .] + --module-name module source name from where components are imported Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' From 2b4ceb1c689a59cdf70bb0a708b294810b13428a Mon Sep 17 00:00:00 2001 From: Filip Pasternak Date: Tue, 29 Jan 2019 08:36:43 +0100 Subject: [PATCH 2/5] extract-react-intl dependency upgrade --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 77c49e2..5360105 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "messages" ], "dependencies": { - "extract-react-intl": "^0.8.1", + "extract-react-intl": "^0.9.0", "flat": "^4.1.0", "js-yaml": "^3.12.1", "load-json-file": "^5.1.0", From fdf12f970cfe05bcaa3c1e82a0c2f824a24675fa Mon Sep 17 00:00:00 2001 From: Filip Pasternak Date: Tue, 29 Jan 2019 09:04:52 +0100 Subject: [PATCH 3/5] Syntax improvement --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 14fe8a1..6bb787f 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,9 @@ module.exports = async (locales, pattern, buildDir, opts) => { const extractorOptions = { defaultLocale } - moduleName && (extractorOptions.moduleSourceName = moduleName) + if (moduleName) { + extractorOptions.moduleSourceName = moduleName + } const newLocaleMaps = await extractReactIntl( locales, From 787295bfc5b15c09bc5e483dbfde203d3b2c8cb2 Mon Sep 17 00:00:00 2001 From: Filip Pasternak Date: Tue, 29 Jan 2019 09:13:24 +0100 Subject: [PATCH 4/5] README update --- readme.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 5febe54..67937af 100644 --- a/readme.md +++ b/readme.md @@ -93,7 +93,7 @@ $ extract-messages --help --flat json [default: true] | yaml [default: false] --default-locale default locale [default: en] --delimiter json | yaml [default: .] - --module-name module source name from where components are imported + --module-name module source name from where components are imported [default: react-intl] Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' @@ -164,6 +164,13 @@ If format is `yaml`, set to `false`. Be careful if `false`. See [this issue](https://github.com/akameco/extract-react-intl-messages/issues/3). +##### moduleName + +Type: `string`
+Default: `react-intl` + +Set from where _defineMessages_, `` and `` are imported. + ## Contributors Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)): From b17c10b15504cf456faba91f2f9eca09c5f41b1c Mon Sep 17 00:00:00 2001 From: Filip Pasternak Date: Tue, 29 Jan 2019 09:48:42 +0100 Subject: [PATCH 5/5] Test case for use with custom module. --- test/fixtures/custom/a/messages.js | 12 ++++++++++ test/fixtures/custom/b/messages.js | 8 +++++++ test/fixtures/custom/i18n.js | 1 + test/json/snapshots/test.js.md | 37 ++++++++++++++++++++++++++++- test/json/snapshots/test.js.snap | Bin 546 -> 629 bytes test/json/test.js | 10 ++++++++ yarn.lock | 8 +++---- 7 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/custom/a/messages.js create mode 100644 test/fixtures/custom/b/messages.js create mode 100644 test/fixtures/custom/i18n.js diff --git a/test/fixtures/custom/a/messages.js b/test/fixtures/custom/a/messages.js new file mode 100644 index 0000000..fd212ce --- /dev/null +++ b/test/fixtures/custom/a/messages.js @@ -0,0 +1,12 @@ +import { defineMessages } from '../i18n' + +export default defineMessages({ + hello: { + id: 'a.custom.hello', + defaultMessage: 'hello' + }, + world: { + id: 'a.custom.world', + defaultMessage: 'world' + } +}) diff --git a/test/fixtures/custom/b/messages.js b/test/fixtures/custom/b/messages.js new file mode 100644 index 0000000..a802b6d --- /dev/null +++ b/test/fixtures/custom/b/messages.js @@ -0,0 +1,8 @@ +import { defineMessages } from '../i18n' + +export default defineMessages({ + hello: { + id: 'b.custom.message', + defaultMessage: 'Message' + } +}) diff --git a/test/fixtures/custom/i18n.js b/test/fixtures/custom/i18n.js new file mode 100644 index 0000000..121ca9c --- /dev/null +++ b/test/fixtures/custom/i18n.js @@ -0,0 +1 @@ +export { defineMessages } from 'react-intl' diff --git a/test/json/snapshots/test.js.md b/test/json/snapshots/test.js.md index a12a44b..06238c0 100644 --- a/test/json/snapshots/test.js.md +++ b/test/json/snapshots/test.js.md @@ -132,4 +132,39 @@ Generated by [AVA](https://ava.li). 'c.hello', 'y.hello', 'z.hello', - ] + +## export using custom module + +> Snapshot 1 + + { + 'a.custom.hello': 'hello', + 'a.custom.world': 'world', + 'b.custom.message': 'Message', + } + +> Snapshot 2 + + { + 'a.custom.hello': '', + 'a.custom.world': '', + 'b.custom.message': '', + } + +## export using custom module + +> Snapshot 1 + + { + 'a.custom.hello': 'hello', + 'a.custom.world': 'world', + 'b.custom.message': 'Message', + } + +> Snapshot 2 + + { + 'a.custom.hello': '', + 'a.custom.world': '', + 'b.custom.message': '', + } diff --git a/test/json/snapshots/test.js.snap b/test/json/snapshots/test.js.snap index bd65ebc39050c36c11d1e649196a53726e6c6286..9d364acd009292fa828d7e3901cb2f2378a811d0 100644 GIT binary patch delta 563 zcmV-30?hrQ1oZ?mK~_N^Q*L2!b7*gLAa*he0su?|v7OPca|N9W__Q07!RH@~2mk;8 z00003d&I!Nz{Zg>PJf^!47CdmOl^MPxjZiTW!R*x=aiYiqF11J0y_hPIZM;rJ3V2g z64e{u6ez^Fv4BNWI3QSo6M|1eaS9jE+`p=~7&ghJ-~6`F|ApSm!%SdN4Q>d&0mRG< z>|oz8axgL*0ci^$_6A~MMm9l4Hvgoo)Z`LIMmSSIK!Aafmw$ngiGh)ikueUaP=t}0 zfss{^kuec0)QK#V1U7aHP-qtr-$gf;3DsB*kT#&Ffmo9fO(`=r7w`ksFu^Qi%}CA3 z$w%d(D=E(}%1J@xQPV6shUIm_Vfh89gx__`2jr;vAZ5!6)tx(004XJT8g?1003tM B@tgnv delta 480 zcmV<60U!SL1fm2oK~_N^Q*L2!b7*gLAa*he0su_h#b$N##95lSq*mmq3s)bD2mk;8 z00003JHWuez{-&_PJaeA28P-N2c|YZ@LV1j{4#9P)^o~CV9_g3Jb|5o!JMUO?wy{n zQir zz{thG$j-pX1Anpx9%Af?deDdii!(AJNkF3>A^~?9dlIS+W+Vv|9V~E(WK$q7_!x~3qT++a3?)e00C6G^w-I(XGg)y#m0*TD WH5!PkD0V7^VF& { t.snapshot(en) t.snapshot(ja) }) + +test('export using custom module', async t => { + const tmp = tempy.directory() + const opts = { moduleName: '../i18n' } + await m(['en', 'ja'], 'test/fixtures/custom/**/*.js', tmp, opts) + const en = JSON.parse(fs.readFileSync(path.resolve(tmp, 'en.json'), 'utf8')) + const ja = JSON.parse(fs.readFileSync(path.resolve(tmp, 'ja.json'), 'utf8')) + t.snapshot(en) + t.snapshot(ja) +}) diff --git a/yarn.lock b/yarn.lock index f2d899d..2b4b3d7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2021,10 +2021,10 @@ extglob@^2.0.4: snapdragon "^0.8.1" to-regex "^3.0.1" -extract-react-intl@^0.8.1: - version "0.8.1" - resolved "https://registry.yarnpkg.com/extract-react-intl/-/extract-react-intl-0.8.1.tgz#09a585e4c3bd816be993e24f8d77945356b8a8e6" - integrity sha512-NsU2pqsxUtYKG4EPaYHIwW1EJTSSd9gmqEjBwbJeq5PNkPJmOui5wp7KzH56MldvopEJp7bKx0UX98L1hkXJIg== +extract-react-intl@^0.9.0: + version "0.9.0" + resolved "https://registry.yarnpkg.com/extract-react-intl/-/extract-react-intl-0.9.0.tgz#7eb9de48d222d483eefd4975afc965a1a43e667a" + integrity sha512-O/AqANf7QIXlZPvXeuBr8aXbK/sFMZ0gn26YcRXUSsBaSSFHEvXVrLlQO6nGTAOOVghLoVRS52im5ylPbpdD6g== dependencies: "@babel/core" "^7.0.0" babel-plugin-react-intl "^3.0.1"