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

gender: when gender not passed, avoid error, fallback to neutral #57

Merged
merged 1 commit into from
Sep 22, 2021
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
13 changes: 10 additions & 3 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,21 @@ export default function BananaMessage (message, { wikilinks = false } = {}) {
return result === null ? null : [result[0], result[2]]
}

function templateWithOutFirstParameter () {
const result = sequence([templateName, colon])
return result === null ? null : [result[0], '']
}

const templateContents = choice([
function () {
const res = sequence([
// templates can have placeholders for dynamic
// replacement eg: {{PLURAL:$1|one car|$1 cars}}
// or no placeholders eg:
// {{GRAMMAR:genitive|{{SITENAME}}}
choice([templateWithReplacement, templateWithOutReplacement]),
// or no placeholders eg:{{GRAMMAR:genitive|{{SITENAME}}}
// Templates can also have empty first param eg:{{GENDER:|A|B|C}}
// to indicate current user in the context. We need to parse them without
// error, but can only fallback to gender neutral form.
choice([templateWithReplacement, templateWithOutReplacement, templateWithOutFirstParameter]),
nOrMore(0, templateParam)
])

Expand Down
4 changes: 4 additions & 0 deletions src/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ class BananaEmitter {
/**
* Transform parsed structure into gender Usage
* {{gender:gender|masculine|feminine|neutral}}.
* The first node(gender) must be one of 'male', 'female' or 'unknown'
* Mediawiki allows this string as empty to indicate current logged in user.
* But this library cannot access such user contexts unless explclitly passed.
* So we need to fallback to gender neutral if it is empty.
*
* @param {Array} nodes List [ {String}, {String}, {String} , {String} ]
* @return {string} selected gender form according to current language
Expand Down
7 changes: 7 additions & 0 deletions test/banana.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ describe('Banana', function () {
'This costs $1.',
'No parameter supplied, $1 appears as is'
)

const genderMessageWithoutExplicitGender = '{{gender:|He|She|They}}'
assert.strictEqual(
banana.i18n(genderMessageWithoutExplicitGender),
'They',
'Gender test - no gender passed'
)
})

it('should parse formatnum', () => {
Expand Down