Skip to content

getTranslation: new msgid w/o context should return msgid #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const translate = {
translated = translated[context]
}

// Avoid a crash when a msgid exists with and without a context, see #32.
if (typeof translated === 'object' && !(translated instanceof Array)) {
// As things currently stand, the void key means a void context for easygettext.
translated = translated.hasOwnProperty('') ? translated[''] : void 0
}

if (!translated) {
if (!silent) {
let msg = `Untranslated ${language} key found: ${msgid}`
Expand All @@ -88,12 +94,6 @@ const translate = {
return untranslated
}

// Avoid a crash when a msgid exists with and without a context, see #32.
if (!(translated instanceof Array) && translated.hasOwnProperty('')) {
// As things currently stand, the void key means a void context for easygettext.
translated = translated['']
}

if (typeof translated === 'string') {
translated = [translated]
}
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