Skip to content
This repository has been archived by the owner on Jul 9, 2018. It is now read-only.

Commit

Permalink
i18n: Support accumulatively registering additional locale data for d…
Browse files Browse the repository at this point in the history
…omain (#105)

* 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
nerrad authored and aduth committed Apr 12, 2018
1 parent 9e66c08 commit ed56276
Showing 2 changed files with 49 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/i18n/src/index.js
Original file line number Diff line number Diff line change
@@ -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
);
}

/**
52 changes: 44 additions & 8 deletions packages/i18n/src/test/index.js
Original file line number Diff line number Diff line change
@@ -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' );
} );
} );
} );
} );

0 comments on commit ed56276

Please sign in to comment.