From 601b63867945949366f37771c1e9f0dd6d12f4ab Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 10 Apr 2018 11:11:40 -0400 Subject: [PATCH 01/13] allow for setting additional locale data for a domain accumulatively --- packages/i18n/src/index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 0fe5a7e..3f2e8a4 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -3,6 +3,7 @@ */ import Jed from 'jed'; import memoize from 'memize'; +import { merge, has } from 'lodash'; let i18n; @@ -34,7 +35,9 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = localeData; + i18n.options.locale_data[ domain ] = has( i18n.options, domain ) + ? merge( {}, i18n.options.locale_data[ domain ], localeData ) + : localeData; } /** From b3775ae66f82fcc70e7bf5c01ce6f9545e90885c Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 10 Apr 2018 11:20:17 -0400 Subject: [PATCH 02/13] woops wrong object reference --- packages/i18n/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 3f2e8a4..b533718 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -35,7 +35,7 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = has( i18n.options, domain ) + i18n.options.locale_data[ domain ] = has( i18n.options.locale_data, domain ) ? merge( {}, i18n.options.locale_data[ domain ], localeData ) : localeData; } From 1e430cf672dceaa6d1dcaa7327a0802050dfeb2f Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 10 Apr 2018 12:46:57 -0400 Subject: [PATCH 03/13] add test for accumulation of localeData --- packages/i18n/src/test/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/i18n/src/test/index.js b/packages/i18n/src/test/index.js index e31f7cd..05d3f1e 100644 --- a/packages/i18n/src/test/index.js +++ b/packages/i18n/src/test/index.js @@ -26,6 +26,10 @@ const localeData = { "fruit\u0004%d apple" : [ "une pomme", "%d pommes" ], } +const additionalLocaleData = { + "cheeseburger" : ["hamburger au fromage"] +} + setLocaleData( localeData, 'test_domain' ); describe( 'i18n', () => { @@ -85,3 +89,20 @@ describe( 'i18n', () => { } ); } ); } ); + +//test setting additional localeData +setLocaleData( additionalLocaleData, 'test_domain' ); + +describe( 'i18n' , () => { + describe( '__', () => { + it( 'existing translation still available', () => { + expect( __( 'hello', 'test_domain' ) ).toBe( 'bonjour' ); + } ); + } ); + + describe( '__', () => { + it( 'new translation available.', () => { + expect( __( 'cheeseburger', 'test_domain' ) ).toBe( 'hamburger au fromage' ); + } ); + } ); +} ); From 07577679c37f1a29df9c3a774b1b0cd16504b019 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 10 Apr 2018 13:32:30 -0400 Subject: [PATCH 04/13] repackage additional tests --- packages/i18n/src/test/index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/i18n/src/test/index.js b/packages/i18n/src/test/index.js index 05d3f1e..7a66924 100644 --- a/packages/i18n/src/test/index.js +++ b/packages/i18n/src/test/index.js @@ -88,21 +88,21 @@ describe( 'i18n', () => { expect( result ).toBe( 'bonjour Riad' ); } ); } ); -} ); - -//test setting additional localeData -setLocaleData( additionalLocaleData, 'test_domain' ); -describe( 'i18n' , () => { - describe( '__', () => { - it( 'existing translation still available', () => { - expect( __( 'hello', 'test_domain' ) ).toBe( 'bonjour' ); + describe( 'setAdditionalLocale' , () => { + beforeAll( () => { + setLocaleData( additionalLocaleData, 'test_domain' ); + } ); + describe( '__', () => { + it( 'existing translation still available', () => { + expect( __( 'hello', 'test_domain' ) ).toBe( 'bonjour' ); + } ); } ); - } ); - describe( '__', () => { - it( 'new translation available.', () => { - expect( __( 'cheeseburger', 'test_domain' ) ).toBe( 'hamburger au fromage' ); + describe( '__', () => { + it( 'new translation available.', () => { + expect( __( 'cheeseburger', 'test_domain' ) ).toBe( 'hamburger au fromage' ); + } ); } ); } ); } ); From 3bb5b9cd9bf753cba973724177fae0267519cfeb Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Tue, 10 Apr 2018 19:44:11 -0400 Subject: [PATCH 05/13] =?UTF-8?q?use=20more=20performant=20=E2=80=9Cin?= =?UTF-8?q?=E2=80=9D=20check=20instead=20of=20lodash=20`=5F.has`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/i18n/src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index b533718..ff615af 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -3,7 +3,7 @@ */ import Jed from 'jed'; import memoize from 'memize'; -import { merge, has } from 'lodash'; +import { merge } from 'lodash'; let i18n; @@ -35,7 +35,7 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = has( i18n.options.locale_data, domain ) + i18n.options.locale_data[ domain ] = domain in i18n.options.locale_data ? merge( {}, i18n.options.locale_data[ domain ], localeData ) : localeData; } From 9a6d78ea3377f1e749d3f5d5d5f06a0661899f6e Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Wed, 11 Apr 2018 12:53:19 -0400 Subject: [PATCH 06/13] use hasOwnProperty instead of in --- packages/i18n/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index ff615af..24e2d29 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -35,7 +35,7 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = domain in i18n.options.locale_data + i18n.options.locale_data[ domain ] = i18n.options.locale_data.hasOwnProperty(domain) ? merge( {}, i18n.options.locale_data[ domain ], localeData ) : localeData; } From 3f56469970430a3b82568579cc7e74e8dd054cee Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 15:07:29 -0400 Subject: [PATCH 07/13] fix formatting for code style --- packages/i18n/src/test/index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/i18n/src/test/index.js b/packages/i18n/src/test/index.js index 7a66924..a52a6ff 100644 --- a/packages/i18n/src/test/index.js +++ b/packages/i18n/src/test/index.js @@ -10,25 +10,25 @@ jest.mock( 'memize', () => ( fn ) => fn ); const localeData = { "" : { // Domain name - "domain" : "test_domain", - "lang" : "fr", + domain: 'test_domain', + lang: 'fr', // Plural form function for language - "plural_forms" : "nplurals=2; plural=(n != 1);" + plural_forms: 'nplurals=2; plural=(n != 1);' }, - "hello" : [ "bonjour" ], + hello: [ 'bonjour' ], - "verb\u0004feed": [ "nourrir" ], + 'verb\u0004feed': [ 'nourrir' ], - "hello %s": [ "bonjour %s"], + 'hello %s': [ 'bonjour %s' ], - "%d banana" : [ "une banane", "%d bananes" ], + '%d banana': [ 'une banane', '%d bananes' ], - "fruit\u0004%d apple" : [ "une pomme", "%d pommes" ], + 'fruit\u0004%d apple': [ 'une pomme', '%d pommes' ], } const additionalLocaleData = { - "cheeseburger" : ["hamburger au fromage"] -} + cheeseburger: [ 'hamburger au fromage' ] +}; setLocaleData( localeData, 'test_domain' ); @@ -89,7 +89,7 @@ describe( 'i18n', () => { } ); } ); - describe( 'setAdditionalLocale' , () => { + describe( 'setAdditionalLocale', () => { beforeAll( () => { setLocaleData( additionalLocaleData, 'test_domain' ); } ); From 25c686f6d481c7a9f243d2cddd23fb960cbe9784 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 15:08:46 -0400 Subject: [PATCH 08/13] fix formatting --- packages/i18n/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 24e2d29..5974d2a 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -35,7 +35,7 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = i18n.options.locale_data.hasOwnProperty(domain) + i18n.options.locale_data[ domain ] = i18n.options.locale_data.hasOwnProperty( domain ) ? merge( {}, i18n.options.locale_data[ domain ], localeData ) : localeData; } From 8dfe686cfd772f8446dfd2fa477110c8fd180bf4 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 15:42:45 -0400 Subject: [PATCH 09/13] use Object.assign instead of lodash.merge --- packages/i18n/src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 5974d2a..2a450ec 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -3,7 +3,6 @@ */ import Jed from 'jed'; import memoize from 'memize'; -import { merge } from 'lodash'; let i18n; @@ -36,7 +35,7 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } i18n.options.locale_data[ domain ] = i18n.options.locale_data.hasOwnProperty( domain ) - ? merge( {}, i18n.options.locale_data[ domain ], localeData ) + ? Object.assign( {}, i18n.options.locale_data[ domain ], localeData ) : localeData; } From a39924adacc3f6ad2d17354dda8819450055201c Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 15:43:19 -0400 Subject: [PATCH 10/13] add new plural strings to tests --- packages/i18n/src/test/index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/i18n/src/test/index.js b/packages/i18n/src/test/index.js index a52a6ff..c9ae9bb 100644 --- a/packages/i18n/src/test/index.js +++ b/packages/i18n/src/test/index.js @@ -27,7 +27,8 @@ const localeData = { 'fruit\u0004%d apple': [ 'une pomme', '%d pommes' ], } const additionalLocaleData = { - cheeseburger: [ 'hamburger au fromage' ] + cheeseburger: [ 'hamburger au fromage' ], + '%d cat': [ 'un chat', '%d chats' ] }; setLocaleData( localeData, 'test_domain' ); @@ -104,5 +105,19 @@ describe( 'i18n', () => { expect( __( 'cheeseburger', 'test_domain' ) ).toBe( 'hamburger au fromage' ); } ); } ); + + describe( '_n', () => { + it( 'existing plural form still works', () => { + expect( _n( '%d banana', '%d bananas', 3, 'test_domain' ) ).toBe( '%d bananes' ); + } ); + + it( 'new singular form was added', () => { + expect( _n( '%d cat', '%d cats', 1, 'test_domain' ) ).toBe( 'un chat' ); + } ); + + it( 'new plural form was added', () => { + expect( _n( '%d cat', '%d cats', 3, 'test_domain' ) ).toBe( '%d chats' ); + } ); + } ); } ); } ); From 533c64e59c1008396db15778f95e7feefe9f16fa Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 16:08:13 -0400 Subject: [PATCH 11/13] simplify merging --- packages/i18n/src/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 2a450ec..c67630f 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -34,9 +34,11 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = i18n.options.locale_data.hasOwnProperty( domain ) - ? Object.assign( {}, i18n.options.locale_data[ domain ], localeData ) - : localeData; +i18n.options.locale_data[ domain ] = Object.assign( + {}, + i18n.options.locale_data[ domain ], + localeData +); } /** From 1b31bc822fecea8e9970832b4645d361040fb097 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 16:20:01 -0400 Subject: [PATCH 12/13] indent, indent --- packages/i18n/src/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index c67630f..7711cb1 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -35,9 +35,9 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } i18n.options.locale_data[ domain ] = Object.assign( - {}, - i18n.options.locale_data[ domain ], - localeData + {}, + i18n.options.locale_data[ domain ], + localeData ); } From aed293b8c02cda02c1e06090645af9ea69d3e779 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Thu, 12 Apr 2018 16:21:29 -0400 Subject: [PATCH 13/13] i18n: Fix indentation --- packages/i18n/src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 7711cb1..00a3db4 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -34,11 +34,11 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } -i18n.options.locale_data[ domain ] = Object.assign( + i18n.options.locale_data[ domain ] = Object.assign( {}, i18n.options.locale_data[ domain ], localeData -); + ); } /**