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' ); + } ); + } ); + } ); } );