diff --git a/src/i18n/index.js b/src/i18n/index.js index e86490041..69d86cb28 100644 --- a/src/i18n/index.js +++ b/src/i18n/index.js @@ -20,6 +20,11 @@ * const i18n = editor.I18n; * ``` * + * ### Events + * * `i18n:add` - New set of messages is added + * * `i18n:update` - The set of messages is updated + * * `i18n:locale` - Locale changed + * * @module I18n */ import { keys } from 'underscore'; @@ -56,7 +61,10 @@ export default () => { * i18n.setLocale('it'); */ setLocale(locale) { - this.config.locale = locale; + const { em, config } = this; + const evObj = { value: locale, valuePrev: config.locale }; + em && em.trigger('i18n:locale', evObj); + config.locale = locale; return this; }, @@ -106,7 +114,9 @@ export default () => { * // -> { en: { msg2: 'Msg 2 up', msg3: 'Msg 3', } } */ setMessages(msg) { - this.config.messages = msg; + const { em, config } = this; + config.messages = msg; + em && em.trigger('i18n:update', msg); return this; }, @@ -123,7 +133,10 @@ export default () => { * // -> { en: { msg1: 'Msg 1', msg2: 'Msg 2 up', msg3: 'Msg 3', } } */ addMessages(msg) { + const { em } = this; const { messages } = this.config; + em && em.trigger('i18n:add', msg); + keys(msg).forEach(lang => { const langSet = msg[lang]; const currentSet = messages[lang]; @@ -136,6 +149,8 @@ export default () => { }); } }); + this.setMessages(messages); // Need this for the event + return this; }, @@ -164,7 +179,10 @@ export default () => { const msgSet = this.getMessages(locale) || {}; const reg = new RegExp(`\{([\\w\\d-]*)\}`, 'g'); let result = msgSet[key]; - !result && em && em.logWarning(`'${key}' i18n key not found`); + !result && + !opts.noWarn && + em && + em.logWarning(`'${key}' i18n key not found`); result = result ? result.replace(reg, (m, val) => param[val] || '').trim() : result; diff --git a/test/specs/i18n/index.js b/test/specs/i18n/index.js index baab1baca..86ffbfbe4 100644 --- a/test/specs/i18n/index.js +++ b/test/specs/i18n/index.js @@ -1,14 +1,15 @@ import I18n from 'i18n'; +import Editor from 'editor/index'; describe('I18n', () => { describe('Main', () => { - let em; let obj; - let editor; + let editor = Editor().init(); + let em = editor.getModel(); beforeEach(() => { obj = I18n(); - obj.init(); + obj.init({ em }); }); test('Object exists', () => { @@ -64,7 +65,7 @@ describe('I18n', () => { en: { msg1 }, it: { msg1: `${msg1} it` } }); - expect(obj.t('msg2')).toBe(undefined); + expect(obj.t('msg2', 0, { noWarn: 1 })).toBe(undefined); expect(obj.t('msg1')).toBe(msg1); }); @@ -92,5 +93,19 @@ describe('I18n', () => { 'Msg 1 Hello it' ); }); + + test('i18n events', () => { + const handlerAdd = jest.fn(); + const handlerUpdate = jest.fn(); + const handlerLocale = jest.fn(); + em.on('i18n:add', handlerAdd); + em.on('i18n:update', handlerUpdate); + em.on('i18n:locale', handlerLocale); + obj.addMessages({ en: { msg1: 'Msg 1', msg2: 'Msg 2' } }); + obj.setLocale('it'); + expect(handlerAdd).toBeCalledTimes(1); + expect(handlerUpdate).toBeCalledTimes(1); + expect(handlerLocale).toBeCalledTimes(1); + }); }); });