Skip to content

Commit

Permalink
feat($i18n): makes the setLocaleData call append to the existing locales
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiglingeanu committed Dec 22, 2017
1 parent 5c92e3d commit feb0397
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
19 changes: 16 additions & 3 deletions i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ let i18n;
* @param {Object} data Locale data configuration
*/
export function setLocaleData( data ) {
i18n = new Jed( data );
i18n = new Jed( {
...(i18n ? i18n.options : {}),
...data
} );
}

/**
* Returns the current Jed instance, initializing with a default configuration
* if not already assigned.
* Resets the Jed instance.
*
* @see http://messageformat.github.io/Jed/
*
* @param {Object} data Locale data configuration
*/
export function resetLocaleData () {
i18n = new Jed( { '': {} });
}

/**
* Returns the current Jed instance.
*
* @return {Jed} Jed instance
*/
Expand Down
55 changes: 55 additions & 0 deletions i18n/test/jed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { resetLocaleData, setLocaleData, __, getI18n } from '../index';

describe( 'wp.i18n', () => {
describe( '.setLocaleData', () => {
it('should set locale data', () => {
resetLocaleData();

setLocaleData({
domain: 'gutenberg',
locale_data: {
gutenberg: {
"": {
domain: 'gutenberg',
lang: 'ro_RO'
},
Demo: ["Demonstrație"],
['Gutenberg Team']: ["Echipa Gutenberg"]
}
}
});

expect( __('Demo') ).toBe( 'Demonstrație' );
expect( __('Gutenberg Team') ).toBe( 'Echipa Gutenberg' );
});

it('should preserve local data between multiple .setLocaleData calls', () => {
resetLocaleData();

setLocaleData({
domain: 'gutenberg',
locale_data: {
gutenberg: {
"": {
domain: 'gutenberg',
lang: 'ro_RO'
},
Demo: ["Demonstrație"]
}
}
});

setLocaleData({
...getI18n().options,
locale_data: {
gutenberg: {
...getI18n().options.locale_data.gutenberg,
['Gutenberg Team']: ["Echipa Gutenberg"]
}
}
});

expect( __('Gutenberg Team') ).toBe( 'Echipa Gutenberg' );
} );
} );
} );

0 comments on commit feb0397

Please sign in to comment.