Skip to content

Commit 360279b

Browse files
authored
Merge pull request #31 from Filson14/master
Allow to use with custom module
2 parents c836bbb + b17c10b commit 360279b

File tree

11 files changed

+96
-10
lines changed

11 files changed

+96
-10
lines changed

cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const cli = meow(
1616
-d, --default-locale default locale
1717
--flat json [default: true] | yaml [default: false]
1818
--delimiter json | yaml [default: .]
19+
--module-name module source name from where components are imported
1920
2021
Example
2122
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
@@ -46,6 +47,9 @@ const cli = meow(
4647
delimiter: {
4748
type: 'string',
4849
default: '.'
50+
},
51+
'module-name': {
52+
type: 'string'
4953
}
5054
}
5155
}

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,23 @@ module.exports = async (locales, pattern, buildDir, opts) => {
8080

8181
const ext = isJson(opts.format) ? 'json' : 'yml'
8282

83-
const { defaultLocale } = opts
83+
const { defaultLocale, moduleName } = opts
8484

8585
const delimiter = opts.delimiter ? opts.delimiter : '.'
8686

8787
const oldLocaleMaps = loadLocaleFiles(locales, buildDir, ext, delimiter)
8888

89-
const newLocaleMaps = await extractReactIntl(locales, pattern, {
90-
defaultLocale
91-
})
89+
const extractorOptions = { defaultLocale }
90+
91+
if (moduleName) {
92+
extractorOptions.moduleSourceName = moduleName
93+
}
94+
95+
const newLocaleMaps = await extractReactIntl(
96+
locales,
97+
pattern,
98+
extractorOptions
99+
)
92100

93101
return Promise.all(
94102
locales.map(locale => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"messages"
3737
],
3838
"dependencies": {
39-
"extract-react-intl": "^0.8.1",
39+
"extract-react-intl": "^0.9.0",
4040
"flat": "^4.1.0",
4141
"js-yaml": "^3.12.1",
4242
"load-json-file": "^5.1.0",

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ $ extract-messages --help
9393
--flat json [default: true] | yaml [default: false]
9494
--default-locale default locale [default: en]
9595
--delimiter json | yaml [default: .]
96+
--module-name module source name from where components are imported [default: react-intl]
9697

9798
Example
9899
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
@@ -163,6 +164,13 @@ If format is `yaml`, set to `false`.
163164
Be careful if `false`.
164165
See [this issue](https://github.com/akameco/extract-react-intl-messages/issues/3).
165166

167+
##### moduleName
168+
169+
Type: `string`<br>
170+
Default: `react-intl`
171+
172+
Set from where _defineMessages_, `<FormatterMessage />` and `<FormattedHTML />` are imported.
173+
166174
## Contributors
167175

168176
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):

test/fixtures/custom/a/messages.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { defineMessages } from '../i18n'
2+
3+
export default defineMessages({
4+
hello: {
5+
id: 'a.custom.hello',
6+
defaultMessage: 'hello'
7+
},
8+
world: {
9+
id: 'a.custom.world',
10+
defaultMessage: 'world'
11+
}
12+
})

test/fixtures/custom/b/messages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineMessages } from '../i18n'
2+
3+
export default defineMessages({
4+
hello: {
5+
id: 'b.custom.message',
6+
defaultMessage: 'Message'
7+
}
8+
})

test/fixtures/custom/i18n.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { defineMessages } from 'react-intl'

test/json/snapshots/test.js.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,39 @@ Generated by [AVA](https://ava.li).
132132
'c.hello',
133133
'y.hello',
134134
'z.hello',
135-
]
135+
136+
## export using custom module
137+
138+
> Snapshot 1
139+
140+
{
141+
'a.custom.hello': 'hello',
142+
'a.custom.world': 'world',
143+
'b.custom.message': 'Message',
144+
}
145+
146+
> Snapshot 2
147+
148+
{
149+
'a.custom.hello': '',
150+
'a.custom.world': '',
151+
'b.custom.message': '',
152+
}
153+
154+
## export using custom module
155+
156+
> Snapshot 1
157+
158+
{
159+
'a.custom.hello': 'hello',
160+
'a.custom.world': 'world',
161+
'b.custom.message': 'Message',
162+
}
163+
164+
> Snapshot 2
165+
166+
{
167+
'a.custom.hello': '',
168+
'a.custom.world': '',
169+
'b.custom.message': '',
170+
}

test/json/snapshots/test.js.snap

83 Bytes
Binary file not shown.

test/json/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,13 @@ test('delimiter - nest', async t => {
6464
t.snapshot(en)
6565
t.snapshot(ja)
6666
})
67+
68+
test('export using custom module', async t => {
69+
const tmp = tempy.directory()
70+
const opts = { moduleName: '../i18n' }
71+
await m(['en', 'ja'], 'test/fixtures/custom/**/*.js', tmp, opts)
72+
const en = JSON.parse(fs.readFileSync(path.resolve(tmp, 'en.json'), 'utf8'))
73+
const ja = JSON.parse(fs.readFileSync(path.resolve(tmp, 'ja.json'), 'utf8'))
74+
t.snapshot(en)
75+
t.snapshot(ja)
76+
})

0 commit comments

Comments
 (0)