Skip to content

Commit

Permalink
#376 - add notes & tests mustache with markup
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed May 3, 2020
1 parent 4fb18b6 commit a1fa83b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ which puts

You may also use [mustache](http://mustache.github.io/) syntax for your message strings. To pass named parameters to your message, just provide an object as the last parameter. You can still pass unnamed parameters by adding additional arguments.


```js
var greeting = __('Hello {{name}}, how are you today?', { name: 'Marcus' });
```
Expand All @@ -679,6 +680,24 @@ var greeting = __( __('Hello {{name}}, how was your %s?', { name: 'Marcus' }), _

which both put *Hello Marcus, how was your weekend.*

#### how about markup?

Including markup in translation and/or variables is considered to be bad practice, as it leads to side effects (translators need to understand it, might break it, inject malformed markup or worse). But well, mustache supports unescaped markup out-of-the-box (*Quote from https://mustache.github.io/mustache.5.html*):

> All variables are HTML escaped by default. If you want to return unescaped HTML, use the triple mustache: {{{name}}}.

So this will work

```js
var greeting = __('Hello {{{name}}}, how are you today?', { name: '<u>Marcus</u>' });
```

as expected:

```html
Hello <u>Marcus</u>, how are you today
```

### basic plural support

two different plural forms are supported as response to `count`:
Expand Down
3 changes: 2 additions & 1 deletion locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
},
"ordered arguments": "%1$s then %2$s",
"ordered arguments with numbers": "%1$s then %2$d then %3$.2f",
"Hallo Marcus, wie war dein %s?": "Hallo Marcus, wie war dein %s?"
"Hallo Marcus, wie war dein %s?": "Hallo Marcus, wie war dein %s?",
"Hello {{{name}}}": "Hallo {{{name}}}"
}
3 changes: 2 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@
"repeated argument": "%1$s, %1$s, %1$s",
". is first character": "Dot is first character",
"last character is .": "last character is Dot",
"few sentences. with .": "few sentences with Dot"
"few sentences. with .": "few sentences with Dot",
"Hello {{{name}}}": "Hello {{{name}}}"
}
4 changes: 4 additions & 0 deletions test/i18n.api.global.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ describe('Module API', function() {
it('should return en translations as expected, using mustached messages', function() {
i18n.setLocale('en');
should.equal(__('Hello {{name}}', { name: 'Marcus' }), 'Hello Marcus');
should.equal(__('Hello {{{name}}}', { name: '<u>Marcus</u>' }), 'Hello <u>Marcus</u>');
should.equal(__('Hello {{name}}', { name: '<u>Marcus</u>' }), 'Hello &lt;u&gt;Marcus&lt;&#x2F;u&gt;');
should.equal(__('Hello {{name}}, how was your %s?', __('weekend'), { name: 'Marcus' }), 'Hello Marcus, how was your weekend?');
});

Expand All @@ -91,6 +93,8 @@ describe('Module API', function() {

// named only
should.equal(__('Hello {{name}}', { name: 'Marcus' }), 'Hallo Marcus');
should.equal(__('Hello {{{name}}}', { name: '<u>Marcus</u>' }), 'Hallo <u>Marcus</u>');
should.equal(__('Hello {{name}}', { name: '<u>Marcus</u>' }), 'Hallo &lt;u&gt;Marcus&lt;&#x2F;u&gt;');

// named + sprintf
should.equal(__('Hello {{name}}, how was your %s?', __('weekend'), { name: 'Marcus' }), 'Hallo Marcus, wie war dein Wochenende?');
Expand Down

0 comments on commit a1fa83b

Please sign in to comment.