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
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@
"contributions": [
"example"
]
},
{
"login": "testower",
"name": "Tom Erik Støwer",
"avatar_url": "https://avatars2.githubusercontent.com/u/231492?v=4",
"profile": "https://github.com/testower",
"contributions": [
"code"
]
}
],
"repoType": "github",
Expand Down
15 changes: 10 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ const cli = meow(
$ extract-messages <input>

Options
-o, --output Output directory [require: true]
-l, --locales locales [require: true]
-f, --format json | yaml [default: json]
-d, --default-locale default locale
--flat json [default: true] | yaml [default: false]
-o, --output Output directory [require: true]
-l, --locales locales [require: true]
-f, --format json | yaml [default: json]
-d, --default-locale default locale
--overwriteDefault default: false
--flat json [default: true] | yaml [default: false]

Example
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
Expand Down Expand Up @@ -42,6 +43,10 @@ const cli = meow(
type: 'string',
alias: 'd'
},
overwriteDefault: {
type: 'boolean',
default: true
},
withDescriptions: {
type: 'boolean',
default: false
Expand Down
22 changes: 15 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-13-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-14-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).

Expand Down Expand Up @@ -96,12 +96,12 @@ $ extract-messages --help
$ extract-messages <input>

Options
-o, --output Output directory [require: true]
-l, --locales locales [require: true]
-f, --format json|yaml [default: json]
--flat json [default: true] | yaml [default: false]
--default-locale default locale [default: en]
--moduleSourceName module source name from where components are imported [default: react-intl]
-o, --output Output directory [require: true]
-l, --locales locales [require: true]
-f, --format json | yaml [default: json]
-d, --default-locale default locale
--overwriteDefault default: false
--flat json [default: true] | yaml [default: false]

Example
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
Expand Down Expand Up @@ -163,6 +163,13 @@ Default: `json`

Set extension to output.

##### overwriteDefault

Type: `boolean`<br>
Default: true

If overwriteDefault is `false`, it will not overwrite messages in the default locale.

##### flat

Type: `boolean`<br>
Expand Down Expand Up @@ -201,6 +208,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
<td align="center"><a href="http://ddem.us/"><img src="https://avatars1.githubusercontent.com/u/290457?v=4" width="100px;" alt="Gregor MacLennan"/><br /><sub><b>Gregor MacLennan</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=gmaclennan" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/zarv1k"><img src="https://avatars1.githubusercontent.com/u/6296643?v=4" width="100px;" alt="Dmitry Zarva"/><br /><sub><b>Dmitry Zarva</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=zarv1k" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/panpanc"><img src="https://avatars2.githubusercontent.com/u/29132669?v=4" width="100px;" alt="Michael Pan"/><br /><sub><b>Michael Pan</b></sub></a><br /><a href="#example-panpanc" title="Examples">💡</a></td>
<td align="center"><a href="https://webstep.no"><img src="https://avatars2.githubusercontent.com/u/231492?v=4" width="100px;" alt="Tom Erik Støwer"/><br /><sub><b>Tom Erik Støwer</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=testower" title="Code">💻</a></td>
<td align="center"><a href="https://github.com/lensbart"><img src="https://avatars2.githubusercontent.com/u/20876627?s=460&v=4" width="100px;" alt="Bart Lens"/><br /><sub><b>Bart Lens</b></sub></a><br /><a href="https://github.com/akameco/extract-react-intl-messages/commits?author=lensbart" title="Code">💻</a></td>
</tr>
</table>
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Opts = {
defaultLocale: string
format?: string
flat?: boolean
overwriteDefault?: boolean
}

// eslint-disable-next-line max-lines-per-function
Expand All @@ -73,6 +74,7 @@ const extractMessage = async (
format = 'json',
flat = isJson(format),
defaultLocale = 'en',
overwriteDefault = true,
...opts
}: Opts = {
defaultLocale: 'en'
Expand Down Expand Up @@ -112,7 +114,7 @@ const extractMessage = async (
locales.map((locale) => {
// If the default locale, overwrite the origin file
let localeMap =
locale === defaultLocale
locale === defaultLocale && overwriteDefault
? // Create a clone so we can use only current valid messages below
{ ...oldLocaleMaps[locale], ...newLocaleMaps[locale] }
: { ...newLocaleMaps[locale], ...oldLocaleMaps[locale] }
Expand Down
16 changes: 16 additions & 0 deletions src/test/json/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,19 @@ Array [
"z.hello",
]
`;

exports[`with overwriteDefault 1`] = `
Object {
"a.custom.hello": "hello",
"a.custom.world": "world",
"b.custom.message": "Default Message",
}
`;

exports[`with overwriteDefault 2`] = `
Object {
"a.custom.hello": "",
"a.custom.world": "",
"b.custom.message": "",
}
`;
26 changes: 26 additions & 0 deletions src/test/json/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,29 @@ test('export using custom module', async () => {
expect(en).toMatchSnapshot()
expect(ja).toMatchSnapshot()
})

test('with overwriteDefault', async () => {
const tmp = tempy.directory()
fs.writeFileSync(
path.resolve(tmp, 'en.json'),
JSON.stringify(
{
'a.custom.hello': 'hello',
'a.custom.world': 'world',
'b.custom.message': 'Default Message'
},
null,
2
),
'utf8'
)
await m(['en', 'ja'], 'src/test/fixtures/custom/**/*.js', tmp, {
defaultLocale: 'en',
moduleSourceName: '../i18n',
overwriteDefault: false
})
const en = JSON.parse(fs.readFileSync(path.resolve(tmp, 'en.json'), 'utf8'))
const ja = JSON.parse(fs.readFileSync(path.resolve(tmp, 'ja.json'), 'utf8'))
expect(en).toMatchSnapshot()
expect(ja).toMatchSnapshot()
})