From ed5627615a76d8ade080c789ec197a983f5cda35 Mon Sep 17 00:00:00 2001 From: Darren Ethier Date: Thu, 12 Apr 2018 16:22:01 -0400 Subject: [PATCH] i18n: Support accumulatively registering additional locale data for domain (#105) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * allow for setting additional locale data for a domain accumulatively * woops wrong object reference * add test for accumulation of localeData * repackage additional tests * use more performant “in” check instead of lodash `_.has` * use hasOwnProperty instead of in * fix formatting for code style * fix formatting * use Object.assign instead of lodash.merge * add new plural strings to tests * simplify merging * indent, indent * i18n: Fix indentation --- packages/i18n/src/index.js | 6 +++- packages/i18n/src/test/index.js | 52 ++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/packages/i18n/src/index.js b/packages/i18n/src/index.js index 0fe5a7e..00a3db4 100644 --- a/packages/i18n/src/index.js +++ b/packages/i18n/src/index.js @@ -34,7 +34,11 @@ export function setLocaleData( localeData = { '': {} }, domain = 'default' ) { } ); } - i18n.options.locale_data[ domain ] = localeData; + i18n.options.locale_data[ domain ] = Object.assign( + {}, + i18n.options.locale_data[ domain ], + localeData + ); } /** diff --git a/packages/i18n/src/test/index.js b/packages/i18n/src/test/index.js index e31f7cd..c9ae9bb 100644 --- a/packages/i18n/src/test/index.js +++ b/packages/i18n/src/test/index.js @@ -10,22 +10,27 @@ 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' ], + '%d cat': [ 'un chat', '%d chats' ] +}; + setLocaleData( localeData, 'test_domain' ); describe( 'i18n', () => { @@ -84,4 +89,35 @@ describe( 'i18n', () => { expect( result ).toBe( 'bonjour Riad' ); } ); } ); + + 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( '_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' ); + } ); + } ); + } ); } );