diff --git a/i18n.js b/i18n.js index 9507405..2192eea 100644 --- a/i18n.js +++ b/i18n.js @@ -273,10 +273,9 @@ const i18n = function I18n(_OPTS = false) { // called like __({phrase: "Hello", locale: "en"}) if (typeof phrase === 'object') { if ( - typeof phrase.locale === 'string' && typeof phrase.phrase === 'string' ) { - msg = translate(phrase.locale, phrase.phrase) + msg = translate(getLocaleFromObject(phrase), phrase.phrase) } } // called like __("Hello") @@ -309,11 +308,10 @@ const i18n = function I18n(_OPTS = false) { // called like __({phrase: "Hello", locale: "en"}) if (typeof phrase === 'object') { if ( - typeof phrase.locale === 'string' && typeof phrase.phrase === 'string' ) { msg = phrase.phrase - targetLocale = phrase.locale + targetLocale = getLocaleFromObject(phrase) } } // called like __("Hello") @@ -392,12 +390,11 @@ const i18n = function I18n(_OPTS = false) { // called like __n({singular: "%s cat", plural: "%s cats", locale: "en"}, 3) if (typeof singular === 'object') { if ( - typeof singular.locale === 'string' && typeof singular.singular === 'string' && typeof singular.plural === 'string' ) { - targetLocale = singular.locale - msg = translate(singular.locale, singular.singular, singular.plural) + targetLocale = getLocaleFromObject(singular) || defaultLocale; + msg = translate(targetLocale, singular.singular, singular.plural) } args.unshift(count) diff --git a/test/i18n.mf.js b/test/i18n.mf.js index e6d969d..171c747 100644 --- a/test/i18n.mf.js +++ b/test/i18n.mf.js @@ -4,6 +4,15 @@ const should = require('should') describe('parsing Messageformat phrases', () => { const mfTest = {} + const i18nWithDefault = new i18n.I18n({ + locales: ['en', 'de'], + defaultLocale: 'en', + directory: './locales', + updateFiles: false, + objectNotation: true, + retryInDefaultLocale: true + }) + beforeEach(() => { i18n.configure({ locales: ['en', 'de', 'fr', 'ru'], @@ -184,4 +193,44 @@ describe('parsing Messageformat phrases', () => { mfTest.__mf('mftest', { NUM: 21, lang: 'russian' }) ) }) + + describe('phrase input as object', () => { + it('should work with simple strings', () => { + should.equal(i18nWithDefault.__mf({ phrase: 'Hello', locale: 'de' }), 'Hallo') + }) + + it('should work with basic replacements', () => { + should.equal( + i18nWithDefault.__mf( + { phrase: 'Hello {name}', locale: 'de' }, + { name: 'Marcus' } + ), + 'Hallo Marcus' + ) + }) + + it('should work with plurals', () => { + should.equal( + i18nWithDefault.__mf({phrase: 'mftest', locale: 'de'}, { NUM: 0, lang: 'german' }), + 'In german there others for 0' + ) + should.equal( + i18nWithDefault.__mf({phrase: 'mftest', locale: 'de'}, { NUM: 1, lang: 'german' }), + 'In german there is one for 1' + ) + should.equal( + i18nWithDefault.__mf({phrase: 'mftest', locale: 'de'}, { NUM: 2, lang: 'german' }), + 'In german there others for 2' + ) + should.equal( + i18nWithDefault.__mf({phrase: 'mftest', locale: 'de'}, { NUM: 3, lang: 'german' }), + 'In german there others for 3' + ) + }) + + it('should use translations from defaultLocale if provided locale is "undefined" or "null"', () => { + should.equal(i18nWithDefault.__mf({ phrase: 'greeting.formal', locale: undefined }), 'Hello') + should.equal(i18nWithDefault.__mf({ phrase: 'greeting.formal', locale: null }), 'Hello') + }) + }) }) diff --git a/test/i18n.retryInDefaultLocale.js b/test/i18n.retryInDefaultLocale.js index 7a9c7c7..97d8edd 100644 --- a/test/i18n.retryInDefaultLocale.js +++ b/test/i18n.retryInDefaultLocale.js @@ -31,6 +31,11 @@ describe('retryInDefaultLocale', () => { should.equal(i18nWithDefault.__('greeting.formal'), 'Hello') }) + it('should use translations from defaultLocale if provided locale is "undefined" or "null"', () => { + should.equal(i18nWithDefault.__({ phrase: 'greeting.formal', locale: undefined }), 'Hello') + should.equal(i18nWithDefault.__({ phrase: 'greeting.formal', locale: null }), 'Hello') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nWithDefault.setLocale('sv') should.equal(i18nWithDefault.getLocale(), 'en') @@ -66,6 +71,11 @@ describe('retryInDefaultLocale', () => { should.equal(i18nNoDefault.__('greeting.formal'), 'Hello') }) + it('should use translation from defaultValue if provided locale is "undefined" or "null"', () => { + should.equal(i18nWithDefault.__({ phrase: 'greeting.formal', locale: undefined }), 'Hello') + should.equal(i18nWithDefault.__({ phrase: 'greeting.formal', locale: null }), 'Hello') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nNoDefault.setLocale('sv') should.equal(i18nNoDefault.getLocale(), 'en') @@ -106,6 +116,11 @@ describe('retryInDefaultLocale', () => { should.equal(i18nWithDefault.__n('%s star', 3), '3 stars') }) + it('should use translation from defaultValue if provided locale is "undefined" or "null"', () => { + should.equal(i18nWithDefault.__n({ singular: '%s star', plural: '%s stars', locale: undefined }, 3), '3 stars') + should.equal(i18nWithDefault.__n({ singular: '%s star', plural: '%s stars', locale: null }, 3), '3 stars') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nWithDefault.setLocale('sv') should.equal(i18nWithDefault.getLocale(), 'en') @@ -147,6 +162,11 @@ describe('retryInDefaultLocale', () => { should.equal(i18nNoDefault.__n('%s star', 3), '3 stars') }) + it('should use translation from defaultValue if provided locale is "undefined" or "null"', () => { + should.equal(i18nWithDefault.__n({ singular: '%s star', plural: '%s stars', locale: undefined }, 3), '3 stars') + should.equal(i18nWithDefault.__n({ singular: '%s star', plural: '%s stars', locale: null }, 3), '3 stars') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nNoDefault.setLocale('sv') should.equal(i18nNoDefault.getLocale(), 'en')