Skip to content
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

Fix getTranslation method for plurals with context #94

Merged
merged 1 commit into from
Apr 3, 2019
Merged
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
10 changes: 5 additions & 5 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ export default {
return untranslated
}

if (typeof translated === 'string') {
translated = [translated]
}

// 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['']]
translated = translated['']
}

if (typeof translated === 'string') {
translated = [translated]
}

let translationIndex = plurals.getTranslationIndex(language, n)
Expand Down
20 changes: 20 additions & 0 deletions test/specs/json/translate.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
"%{ carCount } cars (verb)"
]
},
"%{ carCount } car (multiple contexts)": {
"": [
"1 car",
"%{ carCount } cars"
],
"Context": [
"1 car with context",
"%{ carCount } cars with context"
]
},
"Object": {
"": "Object",
"Context": "Object with context"
Expand Down Expand Up @@ -52,6 +62,16 @@
"%{ carCount } véhicules (verbe)"
]
},
"%{ carCount } car (multiple contexts)": {
"": [
"1 véhicule",
"%{ carCount } véhicules"
],
"Context": [
"1 véhicule avec contexte",
"%{ carCount } véhicules avec contexte"
]
},
"Object": {
"": "Objet",
"Context": "Objet avec contexte"
Expand Down
10 changes: 10 additions & 0 deletions test/specs/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ describe('Translate tests', () => {
translated = translate.getTranslation('Untranslated %{ n } item', 2, null, 'Untranslated %{ n } items', 'en_US')
expect(translated).to.equal('Untranslated %{ n } items')

// Test plural message with multiple contexts (default context and 'Context'')
translated = translate.getTranslation('%{ carCount } car (multiple contexts)', 1, null, null, 'en_US')
expect(translated).to.equal('1 car')
translated = translate.getTranslation('%{ carCount } car (multiple contexts)', 2, null, null, 'en_US')
expect(translated).to.equal('%{ carCount } cars')
translated = translate.getTranslation('%{ carCount } car (multiple contexts)', 1, 'Context', null, 'en_US')
expect(translated).to.equal('1 car with context')
translated = translate.getTranslation('%{ carCount } car (multiple contexts)', 2, 'Context', null, 'en_US')
expect(translated).to.equal('%{ carCount } cars with context')

})

it('tests the gettext() method', () => {
Expand Down