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

i18n: Support accumulatively registering additional locale data for domain #105

Merged
merged 13 commits into from
Apr 12, 2018
6 changes: 5 additions & 1 deletion packages/i18n/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

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