Skip to content

Commit

Permalink
gender: when gender not passed, avoid error, fallback to neutral (#57)
Browse files Browse the repository at this point in the history
Messages like {{GENDER:|A|B|C}} were causing parser failures.
This patch fixes the parser and introduce a fallback to neutral form.

As a mediawiki independent library, while Gender is supported, we cannot
support gender with current logged in user. For that explicit gender
param placeholder needed(example: {{GENDER:$1|A|B|C}}
  • Loading branch information
santhoshtr authored Sep 22, 2021
1 parent feaf0b9 commit 039d73c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,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

0 comments on commit 039d73c

Please sign in to comment.