From df0f4657a663ebd6fb0736854b9d07e0ab0e5781 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 4 Sep 2024 01:00:32 -0400 Subject: [PATCH] getTranslation: new msgid w/o context should return msgid When msgid has multiple context and does not have no-context fallback with empty string ("") context, gettext(msgid) returns "undefined". It should return msgid instead to be consistent with "untranslated" behavior. --- src/translate.js | 4 ++-- test/specs/translate.spec.js | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/translate.js b/src/translate.js index def910f..1ab487a 100644 --- a/src/translate.js +++ b/src/translate.js @@ -89,9 +89,9 @@ const translate = { } // Avoid a crash when a msgid exists with and without a context, see #32. - if (!(translated instanceof Array) && translated.hasOwnProperty('')) { + if (typeof translated === 'object' && !(translated instanceof Array)) { // As things currently stand, the void key means a void context for easygettext. - translated = translated[''] + translated = translated.hasOwnProperty('') ? translated[''] : untranslated } if (typeof translated === 'string') { diff --git a/test/specs/translate.spec.js b/test/specs/translate.spec.js index 93dec43..157d9f6 100644 --- a/test/specs/translate.spec.js +++ b/test/specs/translate.spec.js @@ -88,9 +88,11 @@ describe('Translate tests', () => { Vue.config.language = 'fr_FR' expect(undetectableGettext('Pending')).to.equal('En cours') + expect(undetectableGettext('Answer')).to.equal('Answer') Vue.config.language = 'en_US' expect(undetectableGettext('Pending')).to.equal('Pending') + expect(undetectableGettext('Answer')).to.equal('Answer') expect(undetectableGettext('Pending', 'fr_FR')).to.equal('En cours') }) @@ -268,9 +270,11 @@ describe('Translate tests without Vue', () => { config.language = 'fr_FR' expect(undetectableGettext('Pending')).to.equal('En cours') + expect(undetectableGettext('Answer')).to.equal('Answer') config.language = 'en_US' expect(undetectableGettext('Pending')).to.equal('Pending') + expect(undetectableGettext('Answer')).to.equal('Answer') expect(undetectableGettext('Pending', 'fr_FR')).to.equal('En cours') })