Skip to content

Commit

Permalink
Allow to include objects in translations - this allows to use vdom-no…
Browse files Browse the repository at this point in the history
…des in translations
  • Loading branch information
StephanHoyer committed Jan 29, 2016
1 parent ea29028 commit 2931b16
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,24 @@ var t = translate( messages_IS, { pluralize:pluralize_IS })

Here's a large list of [pluralization algorithms by language](http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html?id=l10n/pluralforms).

## Working with VDOM libs

If you work with VDOM-libs such as [mithril.js](http://mithril.js.org/) you
sometimes want to include VDOM nodes into the translation. This is possible
by using the `arr`-helper. It does not convert the translation result to a
string but rather returns an array with all the replacements left intact.

```js
var t = translate({
test: 'abc {fancyImage} def'
})

t.arr('test', {
fancyImage: m('img', { src: 'image.jpg' })
})
// results in ['abc ', { tag: 'img', ... }, ' def']
})
```

## Namespace-Support

Expand Down
21 changes: 12 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* v0.3.0
*
* Usage:
*
*
* var translate = require('translate.js')
*
*
* var messages = {
* translationKey: 'translationValue'
* }
Expand Down Expand Up @@ -38,11 +38,10 @@
return obj && typeof obj === 'object'
}

function assemble(parts, replacements, count, debug) {
var result = parts[0]
var i = 1
function assemble (parts, replacements, count, debug) {
var result = [].concat(parts)
var len = parts.length
while (i < len) {
for (var i = 1; i < len; i += 2) {
var part = parts[i]
var val = replacements[part]
if (val == null) {
Expand All @@ -53,8 +52,7 @@
val = '{' + part + '}'
}
}
result += val + parts[i+1]
i += 2
result[i] = val
}
return result
}
Expand Down Expand Up @@ -105,7 +103,7 @@
return result
}

var tFunc = function (translationKey, count, replacements) {
function runTranslation (joinResult, translationKey, count, replacements) {
var translation = tFunc.keys[translationKey]
var complex = count != null || replacements != null

Expand Down Expand Up @@ -134,9 +132,14 @@
translation = replacePlaceholders(translation, replacements, count)
}

if (joinResult && translation && translation.join) {
return translation.join('')
}
return translation
}

var tFunc = runTranslation.bind(null, true)
tFunc.arr = runTranslation.bind(null, false)
tFunc.keys = messageObject || {}
tFunc.opts = options || {}

Expand Down
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,12 @@ describe('translate.js', function () {
expect(t('test', { x: 'HelloWorld' })).to.equal('HelloWorld')
})
})

describe('vdom awareness', function () {
it('should not kill any objects and return arrays as translations', function () {
var t = translate({
test: 'abc {xyz} def'
})
expect(t.arr('test', { xyz: { foo: 'bar' } })).to.eql(['abc ', { foo: 'bar' }, ' def'])
})
})

0 comments on commit 2931b16

Please sign in to comment.