From 4b69d4ea98553f03f67de2d00917706cc103c094 Mon Sep 17 00:00:00 2001 From: Long Doan Date: Sun, 2 Oct 2022 19:59:50 +0200 Subject: [PATCH 1/4] Use translation from default locale if provided locale is "undefined" or "null". Issue #501 --- i18n.js | 3 +-- test/i18n.retryInDefaultLocale.js | 8 ++++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/i18n.js b/i18n.js index 9507405..95f88b0 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") diff --git a/test/i18n.retryInDefaultLocale.js b/test/i18n.retryInDefaultLocale.js index 7a9c7c7..a882d24 100644 --- a/test/i18n.retryInDefaultLocale.js +++ b/test/i18n.retryInDefaultLocale.js @@ -31,6 +31,10 @@ describe('retryInDefaultLocale', () => { should.equal(i18nWithDefault.__('greeting.formal'), 'Hello') }) + it('should use translations from defaultLocale if provided locale is "undefined"', () => { + should.equal(i18nWithDefault.__({ phrase: 'greeting.formal', locale: undefined }), 'Hello') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nWithDefault.setLocale('sv') should.equal(i18nWithDefault.getLocale(), 'en') @@ -66,6 +70,10 @@ describe('retryInDefaultLocale', () => { should.equal(i18nNoDefault.__('greeting.formal'), 'Hello') }) + it('should use translation from defaultValue if provided locale is "undefined"', () => { + should.equal(i18nWithDefault.__({ phrase: 'greeting.formal', locale: undefined }), 'Hello') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nNoDefault.setLocale('sv') should.equal(i18nNoDefault.getLocale(), 'en') From 22e4daa708f5c98db4d9dc42fe3f30c501fe8293 Mon Sep 17 00:00:00 2001 From: Long Doan Date: Wed, 5 Oct 2022 14:00:37 +0200 Subject: [PATCH 2/4] Use translation from default locale if provided locale is "undefined" or "null" when using method i18n.__n. Issue #501 --- i18n.js | 5 ++--- test/i18n.retryInDefaultLocale.js | 8 ++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/i18n.js b/i18n.js index 95f88b0..8aaa96e 100644 --- a/i18n.js +++ b/i18n.js @@ -391,12 +391,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.retryInDefaultLocale.js b/test/i18n.retryInDefaultLocale.js index a882d24..47336b8 100644 --- a/test/i18n.retryInDefaultLocale.js +++ b/test/i18n.retryInDefaultLocale.js @@ -114,6 +114,10 @@ describe('retryInDefaultLocale', () => { should.equal(i18nWithDefault.__n('%s star', 3), '3 stars') }) + it('should use translation from defaultValue if provided locale is "undefined"', () => { + should.equal(i18nWithDefault.__n({ singular: '%s star', plural: '%s stars', locale: undefined }, 3), '3 stars') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nWithDefault.setLocale('sv') should.equal(i18nWithDefault.getLocale(), 'en') @@ -155,6 +159,10 @@ describe('retryInDefaultLocale', () => { should.equal(i18nNoDefault.__n('%s star', 3), '3 stars') }) + it('should use translation from defaultValue if provided locale is "undefined"', () => { + should.equal(i18nWithDefault.__n({ singular: '%s star', plural: '%s stars', locale: undefined }, 3), '3 stars') + }) + it('should default "en" when locale is set to unconfigured value', () => { i18nNoDefault.setLocale('sv') should.equal(i18nNoDefault.getLocale(), 'en') From 9d6165cbd14805a6753daeb9f0dea4d1053bb038 Mon Sep 17 00:00:00 2001 From: Long Doan Date: Sun, 25 Jun 2023 18:19:59 +0200 Subject: [PATCH 3/4] Use translation from default locale if provided locale is "undefined" or "null" for method i18n.__mf. Issue #501 --- i18n.js | 3 +-- test/i18n.mf.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/i18n.js b/i18n.js index 8aaa96e..2192eea 100644 --- a/i18n.js +++ b/i18n.js @@ -308,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") 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') + }) + }) }) From c1b5d7a1498d36cf57bf04faaa28df60faab0fc6 Mon Sep 17 00:00:00 2001 From: Long Doan Date: Sun, 25 Jun 2023 18:22:34 +0200 Subject: [PATCH 4/4] Add tests cases that provided locale is null. Issue #501 --- test/i18n.retryInDefaultLocale.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/i18n.retryInDefaultLocale.js b/test/i18n.retryInDefaultLocale.js index 47336b8..97d8edd 100644 --- a/test/i18n.retryInDefaultLocale.js +++ b/test/i18n.retryInDefaultLocale.js @@ -31,8 +31,9 @@ describe('retryInDefaultLocale', () => { should.equal(i18nWithDefault.__('greeting.formal'), 'Hello') }) - it('should use translations from defaultLocale if provided locale is "undefined"', () => { + 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', () => { @@ -70,8 +71,9 @@ describe('retryInDefaultLocale', () => { should.equal(i18nNoDefault.__('greeting.formal'), 'Hello') }) - it('should use translation from defaultValue if provided locale is "undefined"', () => { + 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', () => { @@ -114,8 +116,9 @@ describe('retryInDefaultLocale', () => { should.equal(i18nWithDefault.__n('%s star', 3), '3 stars') }) - it('should use translation from defaultValue if provided locale is "undefined"', () => { + 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', () => { @@ -159,8 +162,9 @@ describe('retryInDefaultLocale', () => { should.equal(i18nNoDefault.__n('%s star', 3), '3 stars') }) - it('should use translation from defaultValue if provided locale is "undefined"', () => { + 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', () => {