Skip to content

Commit

Permalink
getTranslation: new msgid w/o context should return msgid
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
janlazo committed Sep 4, 2024
1 parent 848245f commit df0f465
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
4 changes: 4 additions & 0 deletions test/specs/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
Expand Down Expand Up @@ -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')
})
Expand Down

0 comments on commit df0f465

Please sign in to comment.