Browse Source

Refactor translate method

pull/2385/head
Artur Arseniev 6 years ago
parent
commit
68d8207d06
  1. 10
      src/i18n/index.js
  2. 12
      test/specs/i18n/index.js

10
src/i18n/index.js

@ -163,8 +163,8 @@ export default () => {
/** /**
* Translate the locale message * Translate the locale message
* @param {String} key Label to translate * @param {String} key Label to translate
* @param {Object} [params] Params for the translation
* @param {Object} [opts] Options for the translation * @param {Object} [opts] Options for the translation
* @param {Object} [opts.params] Params for the translation
* @param {Boolean} [opts.noWarn] Avoid warnings in case of missing resources * @param {Boolean} [opts.noWarn] Avoid warnings in case of missing resources
* @returns {String} * @returns {String}
* @example * @example
@ -174,14 +174,14 @@ export default () => {
* }); * });
* obj.t('msg'); * obj.t('msg');
* // -> outputs `Msg` * // -> outputs `Msg`
* obj.t('msg2', { test: 'hello' }); // use params * obj.t('msg2', { params: { test: 'hello' } }); // use params
* // -> outputs `Msg hello` * // -> outputs `Msg hello`
* obj.t('msg2', { test: 'hello' }, { l: 'it' }); // custom local * obj.t('msg2', { l: 'it', params: { test: 'hello' } }); // custom local
* // -> outputs `Msg hello it` * // -> outputs `Msg hello it`
*/ */
t(key, params, opts = {}) { t(key, opts = {}) {
const { em } = this; const { em } = this;
const param = params || {}; const param = opts.params || {};
const locale = opts.l || this.getLocale(); const locale = opts.l || this.getLocale();
const msgSet = this.getMessages(locale, opts); const msgSet = this.getMessages(locale, opts);

12
test/specs/i18n/index.js

@ -65,7 +65,7 @@ describe('I18n', () => {
en: { msg1 }, en: { msg1 },
it: { msg1: `${msg1} it` } it: { msg1: `${msg1} it` }
}); });
expect(obj.t('msg2', 0, { noWarn: 1 })).toBe(undefined); expect(obj.t('msg2', { noWarn: 1 })).toBe(undefined);
expect(obj.t('msg1')).toBe(msg1); expect(obj.t('msg1')).toBe(msg1);
}); });
@ -86,8 +86,8 @@ describe('I18n', () => {
}); });
expect(obj.t('key1.msg1')).toBe(msg1); expect(obj.t('key1.msg1')).toBe(msg1);
expect(obj.t('key1.key2.msg2')).toBe(msg2); expect(obj.t('key1.key2.msg2')).toBe(msg2);
expect(obj.t('key1.key2.msg3', 0, opts)).toBe(undefined); expect(obj.t('key1.key2.msg3', opts)).toBe(undefined);
expect(obj.t('key1.key3.msg2', 0, opts)).toBe(undefined); expect(obj.t('key1.key3.msg2', opts)).toBe(undefined);
}); });
test('Translate method with custom locale', () => { test('Translate method with custom locale', () => {
@ -98,7 +98,7 @@ describe('I18n', () => {
en: { msg1 }, en: { msg1 },
it: { msg1: msg1Alt } it: { msg1: msg1Alt }
}); });
expect(obj.t('msg1', null, { l: 'it' })).toBe(msg1Alt); expect(obj.t('msg1', { l: 'it' })).toBe(msg1Alt);
}); });
test('Translate method with a param', () => { test('Translate method with a param', () => {
@ -109,8 +109,8 @@ describe('I18n', () => {
en: { msg1 }, en: { msg1 },
it: { msg1: msg1Alt } it: { msg1: msg1Alt }
}); });
expect(obj.t('msg1', { test: 'Hello' })).toBe('Msg 1 Hello'); expect(obj.t('msg1', { params: { test: 'Hello' } })).toBe('Msg 1 Hello');
expect(obj.t('msg1', { test: 'Hello' }, { l: 'it' })).toBe( expect(obj.t('msg1', { l: 'it', params: { test: 'Hello' } })).toBe(
'Msg 1 Hello it' 'Msg 1 Hello it'
); );
}); });

Loading…
Cancel
Save