From feb0397b504231f1fd1cbc4500ba28200fa49b96 Mon Sep 17 00:00:00 2001 From: Andrei Glingeanu Date: Fri, 22 Dec 2017 14:02:28 +0200 Subject: [PATCH] feat($i18n): makes the setLocaleData call append to the existing locales --- i18n/index.js | 19 ++++++++++++++--- i18n/test/jed.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 i18n/test/jed.js diff --git a/i18n/index.js b/i18n/index.js index f61c35fa7c650..594078b1562d3 100644 --- a/i18n/index.js +++ b/i18n/index.js @@ -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 */ diff --git a/i18n/test/jed.js b/i18n/test/jed.js new file mode 100644 index 0000000000000..9489eaa48cf8f --- /dev/null +++ b/i18n/test/jed.js @@ -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' ); + } ); + } ); +} );