Skip to content

Commit

Permalink
feat(__): added tagged template literal support
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed May 14, 2017
1 parent 77f684e commit 4e107bc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
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
17 changes: 17 additions & 0 deletions test/y18n-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ describe('y18n', function () {
})

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

try {
// We need to do this wrapper in case y18n gets used in runtimes
// without template literal support.
eval('__`Hello ${"Ben"} ${"Coe"}`').should.equal('Hello Ben Coe') // eslint-disable-line
} catch (e) {
if (e.message.match(/SyntaxError/)) {
this.skip()
} else {
throw e
}
}
})
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 4e107bc

Please sign in to comment.