From fd679194a792c43bcf4a3e6905f7cbc53ae9c5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20Erik=20St=C3=B8wer?= Date: Fri, 4 Oct 2019 06:59:48 +0200 Subject: [PATCH 1/2] Feature: Add option overwrite-default, to be able to opt out of overwriting the default locale --- .all-contributorsrc | 9 +++++++++ cli.js | 15 ++++++++++----- readme.md | 22 +++++++++++++++------- src/index.ts | 3 ++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 3a876df..1d17d3f 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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", diff --git a/cli.js b/cli.js index aa61f5f..9770666 100755 --- a/cli.js +++ b/cli.js @@ -11,11 +11,12 @@ const cli = meow( $ extract-messages 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 + -w, --overwrite-default default: false + --flat json [default: true] | yaml [default: false] Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' @@ -47,6 +48,10 @@ const cli = meow( type: 'boolean', default: false }, + 'overwrite-default': { + type: 'boolean', + default: true + }, // babel-plugin-react-intl boolean options enforceDescriptions: { type: 'boolean', diff --git a/readme.md b/readme.md index 926aedf..e248eec 100644 --- a/readme.md +++ b/readme.md @@ -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). @@ -96,12 +96,12 @@ $ extract-messages --help $ extract-messages 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 + -w, --overwrite-default default: false + --flat json [default: true] | yaml [default: false] Example $ extract-messages --locales=ja,en --output app/translations 'app/**/*.js' @@ -163,6 +163,13 @@ Default: `json` Set extension to output. +##### overwriteDefault + +Type: `boolean`
+Default: true + +If overwriteDefault is `false`, it will not overwrite messages in the default locale. + ##### flat Type: `boolean`
@@ -201,6 +208,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds Gregor MacLennan
Gregor MacLennan

💻 Dmitry Zarva
Dmitry Zarva

💻 Michael Pan
Michael Pan

💡 + Tom Erik Støwer
Tom Erik Støwer

💻 diff --git a/src/index.ts b/src/index.ts index c7d2f79..6a232f8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -75,6 +75,7 @@ const extractMessage = async ( format = 'json', flat = isJson(format), defaultLocale = 'en', + overwriteDefault = true, ...opts }: Opts = { defaultLocale: 'en' @@ -114,7 +115,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] } From 5c62d8f2a5f1065e051f1d910ff3c841c9587d98 Mon Sep 17 00:00:00 2001 From: akameco Date: Tue, 24 Mar 2020 16:22:21 +0900 Subject: [PATCH 2/2] test: add overwriteDefault test --- cli.js | 10 ++++----- readme.md | 2 +- src/index.ts | 1 + src/test/json/__snapshots__/test.ts.snap | 16 +++++++++++++++ src/test/json/test.ts | 26 ++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/cli.js b/cli.js index c70d39c..d2d8a11 100755 --- a/cli.js +++ b/cli.js @@ -14,7 +14,7 @@ const cli = meow( -l, --locales locales [require: true] -f, --format json | yaml [default: json] -d, --default-locale default locale - -w, --overwrite-default default: false + --overwriteDefault default: false --flat json [default: true] | yaml [default: false] Example @@ -43,13 +43,13 @@ const cli = meow( type: 'string', alias: 'd' }, - withDescriptions: { + overwriteDefault: { type: 'boolean', - default: false + default: true }, - 'overwrite-default': { + withDescriptions: { type: 'boolean', - default: true + default: false }, // babel-plugin-react-intl boolean options extractSourceLocation: { diff --git a/readme.md b/readme.md index 60a0283..b2a486f 100644 --- a/readme.md +++ b/readme.md @@ -100,7 +100,7 @@ $ extract-messages --help -l, --locales locales [require: true] -f, --format json | yaml [default: json] -d, --default-locale default locale - -w, --overwrite-default default: false + --overwriteDefault default: false --flat json [default: true] | yaml [default: false] Example diff --git a/src/index.ts b/src/index.ts index b7cf7f4..fedc455 100644 --- a/src/index.ts +++ b/src/index.ts @@ -62,6 +62,7 @@ type Opts = { defaultLocale: string format?: string flat?: boolean + overwriteDefault?: boolean } // eslint-disable-next-line max-lines-per-function diff --git a/src/test/json/__snapshots__/test.ts.snap b/src/test/json/__snapshots__/test.ts.snap index 9403e15..2f168af 100644 --- a/src/test/json/__snapshots__/test.ts.snap +++ b/src/test/json/__snapshots__/test.ts.snap @@ -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": "", +} +`; diff --git a/src/test/json/test.ts b/src/test/json/test.ts index 9f2b75c..81767d5 100644 --- a/src/test/json/test.ts +++ b/src/test/json/test.ts @@ -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() +})