From 574069dfd29d64f1bcb83f6aec928b8fcc3d0df4 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Sat, 2 Nov 2019 19:16:18 +0100 Subject: [PATCH] Add _warn in i18n module --- src/i18n/index.js | 19 +++++++++---------- test/specs/i18n/index.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/i18n/index.js b/src/i18n/index.js index fdaaa76c1..1f2721f92 100644 --- a/src/i18n/index.js +++ b/src/i18n/index.js @@ -100,13 +100,10 @@ export default () => { * // -> { hello: '...' } */ getMessages(lang, opts = {}) { - const { em, config } = this; - const { messages } = config; - !opts.noWarn && - lang && + const { messages } = this.config; + lang && !messages[lang] && - em && - em.logWarning(`'${lang}' i18n lang set not found`); + this._warn(`'${lang}' i18n lang set not found`, opts); return lang ? messages[lang] : messages; }, @@ -189,15 +186,17 @@ export default () => { const msgSet = this.getMessages(locale, opts) || {}; const reg = new RegExp(`\{([\\w\\d-]*)\}`, 'g'); let result = msgSet[key]; - !result && - !opts.noWarn && - em && - em.logWarning(`'${key}' i18n key not found`); + !result && this._warn(`'${key}' i18n key not found`, opts); result = result ? result.replace(reg, (m, val) => param[val] || '').trim() : result; return result; + }, + + _warn(str, opts = {}) { + const { em } = this; + !opts.noWarn && em && em.logWarning(str); } }; }; diff --git a/test/specs/i18n/index.js b/test/specs/i18n/index.js index 86ffbfbe4..bf1079fa8 100644 --- a/test/specs/i18n/index.js +++ b/test/specs/i18n/index.js @@ -69,6 +69,24 @@ describe('I18n', () => { expect(obj.t('msg1')).toBe(msg1); }); + test('Translate method with object structure', () => { + const msg1 = 'Msg level 1'; + const msg2 = 'Msg level 2'; + obj.setLocale('en'); + obj.setMessages({ + en: { + key1: { + msg1, + key2: { + msg2 + } + } + } + }); + expect(obj.t('key1.msg1')).toBe(msg1); + expect(obj.t('key1.key2.msg2')).toBe(msg2); + }); + test('Translate method with custom locale', () => { const msg1 = 'Msg 1'; const msg1Alt = `${msg1} it`;