Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down
49 changes: 49 additions & 0 deletions test/i18n.mf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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')
})
})
})
20 changes: 20 additions & 0 deletions test/i18n.retryInDefaultLocale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down