Skip to content

Commit

Permalink
Don't throw on {{X}} syntax where X is not a valid i18n parser oper…
Browse files Browse the repository at this point in the history
…ator

Render them literally instead. Fixes issue wikimedia-gadgets/twinkle-core#6.
  • Loading branch information
siddharthvp committed May 15, 2021
1 parent 1b5914c commit 2a13edf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ Some of the changes done in this fork have been merged upstream. The only signif
- ~~This needs the knowledge of how different languages separate words – this can be done using the MediaWiki i18n messages `word-separator`, `comma-separator` and `and` which we fetch using the MediaWiki API at the build step.~~
- Reverted. Now that custom parser hooks are supported, this can instead be implemented as a parser hook.
4. (366319eb) Added support for custom parser hooks. Mostly intended for cases where the hook relies on data that is not available with orange-i18n but is present in the client script.
- MERGED UPSTREAM in https://github.com/wikimedia/banana-i18n/pull/46
5. Added TypeScript type definitions.
- MERGED UPSTREAM in https://github.com/wikimedia/banana-i18n/pull/46
5. Added TypeScript type definitions.
- ALSO implemented in upstream
6. Make it easier to mention wiki-templates in messages by not throwing an error for `{{X}}` syntax where X is not a valid i18n parser operator. Such usages are rendered literally instead.

#### Removed/missing features:
Certain features are removed/missing in order to keep the library light-weight.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orange-i18n",
"version": "3.0.0",
"version": "3.1.0",
"description": "Banana Internationalization library",
"main": "dist/banana-i18n.js",
"typings": "types/index.d.ts",
Expand Down
7 changes: 6 additions & 1 deletion src/emitter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import languages from './languages'
import BananaParser from './parser'

/**
* Matches the first strong directionality codepoint:
Expand Down Expand Up @@ -54,7 +55,11 @@ class BananaEmitter {
if (typeof this[operation] === 'function') {
ret = this[operation](subnodes, replacements)
} else {
throw new Error('unknown operation "' + operation + '"')
// they probably are just talking about wiki templates
// In the name of the wikitemplate, any operators like plural, grammar, etc CANNOT be used.
// Placeholders ($1, $2, ...) can be used
// All operators can be used in wikitemplate parameters.
ret = '{{' + new BananaParser(this.locale).simpleParse(node[0], replacements) + subnodes.map(n => '|' + n) + '}}'
}

break
Expand Down
21 changes: 21 additions & 0 deletions test/banana.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,27 @@ describe('Banana', function () {
assert.strictEqual(banana.i18n('message_2'), 'Message two')
})

it('should not throw errors when referring to wiki-templates', () => {
let banana = new Banana('en')

// simple
assert.strictEqual(banana.i18n('{{Cleanup}}'), '{{Cleanup}}', 'simple')

// should support processing of parameters
assert.strictEqual(banana.i18n('{{Cleanup|count={{PLURAL:$1|$1|$1}}}}', '2'), '{{Cleanup|count=2}}', 'supports processing of parameters')

// should support template names containing placeholders
assert.strictEqual(banana.i18n('{{$1}}', 'Cleanup'), '{{Cleanup}}', 'supports template name being a $N')

assert.strictEqual(banana.i18n('{{$1|minor=$2}}', 'Cleanup', 'yes'), '{{Cleanup|minor=yes}}', 'complex')

assert.strictEqual(banana.i18n('{{cleanup| count = 23}}'), '{{cleanup| count = 23}}', 'preserves spacing in wikitemplates')
assert.strictEqual(banana.i18n('{{cleanup | count = 23 }}'), '{{cleanup | count = 23 }}', 'preserves spacing in wikitemplates')

// won't work
// assert.strictEqual(banana.i18n('{{{{PLURAL:$1|$1|$1}}}}', '4'), '{{4}}', 'support operations in template name')
})

it('should throw errors on invalid locales', () => {
assert.throws(() => {
// eslint-disable-next-line no-new
Expand Down

0 comments on commit 2a13edf

Please sign in to comment.