Browse Source

Add _warn in i18n module

pull/2385/head
Artur Arseniev 6 years ago
parent
commit
574069dfd2
  1. 19
      src/i18n/index.js
  2. 18
      test/specs/i18n/index.js

19
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);
}
};
};

18
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`;

Loading…
Cancel
Save