Skip to content

Commit fd67919

Browse files
committed
Feature: Add option overwrite-default, to be able to opt out of overwriting the default locale
1 parent fb088f2 commit fd67919

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@
132132
"contributions": [
133133
"example"
134134
]
135+
},
136+
{
137+
"login": "testower",
138+
"name": "Tom Erik Støwer",
139+
"avatar_url": "https://avatars2.githubusercontent.com/u/231492?v=4",
140+
"profile": "https://github.com/testower",
141+
"contributions": [
142+
"code"
143+
]
135144
}
136145
],
137146
"repoType": "github",

cli.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ const cli = meow(
1111
$ extract-messages <input>
1212
1313
Options
14-
-o, --output Output directory [require: true]
15-
-l, --locales locales [require: true]
16-
-f, --format json | yaml [default: json]
17-
-d, --default-locale default locale
18-
--flat json [default: true] | yaml [default: false]
14+
-o, --output Output directory [require: true]
15+
-l, --locales locales [require: true]
16+
-f, --format json | yaml [default: json]
17+
-d, --default-locale default locale
18+
-w, --overwrite-default default: false
19+
--flat json [default: true] | yaml [default: false]
1920
2021
Example
2122
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
@@ -47,6 +48,10 @@ const cli = meow(
4748
type: 'boolean',
4849
default: false
4950
},
51+
'overwrite-default': {
52+
type: 'boolean',
53+
default: true
54+
},
5055
// babel-plugin-react-intl boolean options
5156
enforceDescriptions: {
5257
type: 'boolean',

readme.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
55
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
66
[![MIT License](https://img.shields.io/npm/l/nps.svg?style=flat-square)](./license)
7-
[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
7+
[![All Contributors](https://img.shields.io/badge/all_contributors-14-orange.svg?style=flat-square)](#contributors-)
88

99
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).
1010

@@ -96,12 +96,12 @@ $ extract-messages --help
9696
$ extract-messages <input>
9797

9898
Options
99-
-o, --output Output directory [require: true]
100-
-l, --locales locales [require: true]
101-
-f, --format json|yaml [default: json]
102-
--flat json [default: true] | yaml [default: false]
103-
--default-locale default locale [default: en]
104-
--moduleSourceName module source name from where components are imported [default: react-intl]
99+
-o, --output Output directory [require: true]
100+
-l, --locales locales [require: true]
101+
-f, --format json | yaml [default: json]
102+
-d, --default-locale default locale
103+
-w, --overwrite-default default: false
104+
--flat json [default: true] | yaml [default: false]
105105

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

164164
Set extension to output.
165165

166+
##### overwriteDefault
167+
168+
Type: `boolean`<br>
169+
Default: true
170+
171+
If overwriteDefault is `false`, it will not overwrite messages in the default locale.
172+
166173
##### flat
167174

168175
Type: `boolean`<br>
@@ -201,6 +208,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
201208
<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>
202209
<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>
203210
<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>
211+
<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>
204212
</tr>
205213
</table>
206214

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const extractMessage = async (
7575
format = 'json',
7676
flat = isJson(format),
7777
defaultLocale = 'en',
78+
overwriteDefault = true,
7879
...opts
7980
}: Opts = {
8081
defaultLocale: 'en'
@@ -114,7 +115,7 @@ const extractMessage = async (
114115
locales.map(locale => {
115116
// If the default locale, overwrite the origin file
116117
let localeMap =
117-
locale === defaultLocale
118+
locale === defaultLocale && overwriteDefault
118119
? // Create a clone so we can use only current valid messages below
119120
{ ...oldLocaleMaps[locale], ...newLocaleMaps[locale] }
120121
: { ...newLocaleMaps[locale], ...oldLocaleMaps[locale] }

0 commit comments

Comments
 (0)