Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vdom awareness - fixes #19 #21

Merged
merged 1 commit into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'])
})
})