Skip to content

Commit 0598daf

Browse files
zkatbcoe
authored andcommitted
feat(__): added tagged template literal support (#44)
BREAKING CHANGE: dropping Node 0.10/Node 0.12 support
1 parent 77f684e commit 0598daf

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

Diff for: .travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
language: node_js
22
sudo: false
33
node_js:
4-
- "0.10"
5-
- "0.12"
64
- "4"
7-
- "5"
5+
- "6"
86
- "node"
97
after_success: npm run coverage

Diff for: README.md

+17
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ output:
2323

2424
`my awesome string foo`
2525

26+
_using tagged template literals_
27+
28+
```js
29+
var __ = require('y18n').__
30+
var str = 'foo'
31+
32+
console.log(__`my awesome string ${str}`)
33+
```
34+
35+
output:
36+
37+
`my awesome string foo`
38+
2639
_pluralization support:_
2740

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

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

76+
This function can also be used as a tag for a template literal. You can use it
77+
like this: <code>__&#96;hello ${'world'}&#96;</code>. This will be equivalent to
78+
`__('hello %s', 'world')`.
79+
6380
### y18n.\_\_n(singularString, pluralString, count, arg, arg, arg)
6481

6582
Print a localized string with appropriate pluralization. If `%d` is provided

Diff for: index.js

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function Y18N (opts) {
1616
}
1717

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

46+
Y18N.prototype._taggedLiteral = function (parts) {
47+
var args = arguments
48+
var str = ''
49+
parts.forEach(function (part, i) {
50+
var arg = args[i + 1]
51+
str += part
52+
if (arg) {
53+
str += '%s'
54+
}
55+
})
56+
return this.__.apply(null, [str].concat([].slice.call(arguments, 1)))
57+
}
58+
4359
Y18N.prototype._enqueueWrite = function (work) {
4460
this.writeQueue.push(work)
4561
if (this.writeQueue.length === 1) this._processWriteQueue()

Diff for: test/locales/pirate.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"Hello": "Avast ye mateys!",
3+
"Hi, %s %s!": "Yarr! Shiver me timbers, why 'tis %s %s!",
34
"%d cat": {
45
"one": "%d land catfish",
56
"other": "%d land catfishes"

Diff for: test/y18n-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ describe('y18n', function () {
3030
})
3131

3232
describe('__', function () {
33+
it('can be used as a tag for template literals', function () {
34+
var __ = y18n({
35+
locale: 'pirate',
36+
directory: path.join(__dirname, 'locales')
37+
}).__
38+
39+
__`Hi, ${'Ben'} ${'Coe'}!`.should.equal('Yarr! Shiver me timbers, why \'tis Ben Coe!')
40+
})
3341
it('uses replacements from the default locale if none is configured', function () {
3442
var __ = y18n({
3543
directory: path.join(__dirname, 'locales')

0 commit comments

Comments
 (0)