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
4 changes: 4 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -46,6 +47,9 @@ const cli = meow(
delimiter: {
type: 'string',
default: '.'
},
'module-name': {
type: 'string'
}
}
}
Expand Down
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,23 @@ 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 }

if (moduleName) {
extractorOptions.moduleSourceName = moduleName
}

const newLocaleMaps = await extractReactIntl(
locales,
pattern,
extractorOptions
)

return Promise.all(
locales.map(locale => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [default: react-intl]

Example
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
Expand Down Expand Up @@ -163,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`<br>
Default: `react-intl`

Set from where _defineMessages_, `<FormatterMessage />` and `<FormattedHTML />` are imported.

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/custom/a/messages.js
Original file line number Diff line number Diff line change
@@ -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'
}
})
8 changes: 8 additions & 0 deletions test/fixtures/custom/b/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineMessages } from '../i18n'

export default defineMessages({
hello: {
id: 'b.custom.message',
defaultMessage: 'Message'
}
})
1 change: 1 addition & 0 deletions test/fixtures/custom/i18n.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { defineMessages } from 'react-intl'
37 changes: 36 additions & 1 deletion test/json/snapshots/test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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': '',
}
Binary file modified test/json/snapshots/test.js.snap
Binary file not shown.
10 changes: 10 additions & 0 deletions test/json/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ test('delimiter - nest', async t => {
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)
})
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down