Skip to content

Commit

Permalink
feat(__): added tagged template literal support (#44)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: dropping Node 0.10/Node 0.12 support
  • Loading branch information
zkat authored and bcoe committed May 17, 2017
1 parent 77f684e commit 0598daf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
language: node_js
sudo: false
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
- "node"
after_success: npm run coverage
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ output:

`my awesome string foo`

_using tagged template literals_

```js
var __ = require('y18n').__
var str = 'foo'

console.log(__`my awesome string ${str}`)
```

output:

`my awesome string foo`

_pluralization support:_

```js
Expand Down Expand Up @@ -60,6 +73,10 @@ Create an instance of y18n with the config provided, options include:

Print a localized string, `%s` will be replaced with `arg`s.

This function can also be used as a tag for a template literal. You can use it
like this: <code>__&#96;hello ${'world'}&#96;</code>. This will be equivalent to
`__('hello %s', 'world')`.

### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)

Print a localized string with appropriate pluralization. If `%d` is provided
Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ function Y18N (opts) {
}

Y18N.prototype.__ = function () {
if (typeof arguments[0] !== 'string') {
return this._taggedLiteral.apply(this, arguments)
}
var args = Array.prototype.slice.call(arguments)
var str = args.shift()
var cb = function () {} // start with noop.
Expand All @@ -40,6 +43,19 @@ Y18N.prototype.__ = function () {
return util.format.apply(util, [this.cache[this.locale][str] || str].concat(args))
}

Y18N.prototype._taggedLiteral = function (parts) {
var args = arguments
var str = ''
parts.forEach(function (part, i) {
var arg = args[i + 1]
str += part
if (arg) {
str += '%s'
}
})
return this.__.apply(null, [str].concat([].slice.call(arguments, 1)))
}

Y18N.prototype._enqueueWrite = function (work) {
this.writeQueue.push(work)
if (this.writeQueue.length === 1) this._processWriteQueue()
Expand Down
1 change: 1 addition & 0 deletions test/locales/pirate.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"Hello": "Avast ye mateys!",
"Hi, %s %s!": "Yarr! Shiver me timbers, why 'tis %s %s!",
"%d cat": {
"one": "%d land catfish",
"other": "%d land catfishes"
Expand Down
8 changes: 8 additions & 0 deletions test/y18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ describe('y18n', function () {
})

describe('__', function () {
it('can be used as a tag for template literals', function () {
var __ = y18n({
locale: 'pirate',
directory: path.join(__dirname, 'locales')
}).__

__`Hi, ${'Ben'} ${'Coe'}!`.should.equal('Yarr! Shiver me timbers, why \'tis Ben Coe!')
})
it('uses replacements from the default locale if none is configured', function () {
var __ = y18n({
directory: path.join(__dirname, 'locales')
Expand Down

0 comments on commit 0598daf

Please sign in to comment.