Skip to content

Commit

Permalink
Add array option and move "joinResult" check into assemble
Browse files Browse the repository at this point in the history
This adds flexibility and reduces overhead for the
large majority of translations where no replacements
are performed.
  • Loading branch information
maranomynet committed Jan 30, 2016
1 parent 74d431b commit 796f01d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ var messages = {

var options = {
// These are the defaults:
debug: false, //[Boolean]: Logs missing translations to console and add "@@" around output if `true`.
debug: false, //[Boolean]: Logs missing translations to console and add "@@" around output, if `true`.
array: false, //[Boolean]: Returns translations with placeholder-replacements as Arrays, if `true`.
pluralize: function(n,translKey){ return Math.abs(n); } //[Function(count,translationKey)]: Provides a custom pluralization mapping function.
}

Expand Down Expand Up @@ -171,17 +172,27 @@ by using the `arr`-helper. It does not convert the translation result to a
string but rather returns an array with all the placeholder-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']
var keys = { test: 'abc {fancyImage} def' }
var t = translate(keys)

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

You can also set this as the default behaviour, by supplying `array:true` option when initializing the translation function.

```js
t = translate(keys, { array: true });

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


## Namespace-Support

Namespace support was dropped in version 0.3.0. If you need it, use [version 0.2.2](https://github.com/StephanHoyer/translate.js/tree/v0.2.2).
34 changes: 20 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*
* var options = {
* // These are the defaults:
* debug: false, //[Boolean]: Logs missing translations to console and add "@@" around output if `true`.
* debug: false, //[Boolean]: Logs missing translations to console and add "@@" around output if `true`.
* array: false, //[Boolean]: `true` returns translations with placeholder-replacements as Arrays.
* pluralize: function(n,translKey){ return Math.abs(n); } //[Function(count,translationKey)]: Provides a custom pluralization mapping function.
* }
*
Expand All @@ -38,7 +39,7 @@
return obj && typeof obj === 'object'
}

function assemble (parts, replacements, count, debug) {
function assemble (parts, replacements, count, options) {
var result = [].concat(parts)
var len = parts.length
for (var i = 1; i < len; i += 2) {
Expand All @@ -48,17 +49,18 @@
if (part === 'n' && count != null) {
val = count
} else {
debug && console.warn('No "' + part + '" in placeholder object:', replacements)
options.debug && console.warn('No "' + part + '" in placeholder object:', replacements)
val = '{' + part + '}'
}
}
result[i] = val
}
return result
return options.array ? result : result.join('')
}

var translatejs = function (messageObject, options) {
var debug = options && options.debug
options = options || {}
var debug = options.debug

function getPluralValue (translation, count) {
// Opinionated assumption: Pluralization rules are the same for negative and positive values.
Expand Down Expand Up @@ -99,11 +101,11 @@
result = parts.length > 1 ? parts : parts[0]
replCache[translation] = result
}
result = result.pop ? assemble(result, replacements, count, debug) : result
result = result.pop ? assemble(result, replacements, count, tFunc.opts) : result
return result
}

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

Expand Down Expand Up @@ -131,17 +133,21 @@
} else if (complex || debug) {
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)
// Convenience function.
tFunc.arr = function () {
var opts = tFunc.opts
var normalArrayOption = opts.array
opts.array = true
var result = tFunc.apply(null, arguments)
opts.array = normalArrayOption
return result
};

tFunc.keys = messageObject || {}
tFunc.opts = options || {}
tFunc.opts = options

return tFunc
}
Expand Down
6 changes: 6 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,10 @@ describe('Return array option', function () {
})
expect(t.arr('test', { xyz: { foo: 'bar' } })).to.eql(['abc ', { foo: 'bar' }, ' def'])
})
it('should return replacement-token translations as Arrays, when `array` option is supplied', function () {
var t = translate({
test: 'abc {xyz} def'
}, { array:true })
expect(t('test', { xyz: { foo: 'bar' } })).to.eql(['abc ', { foo: 'bar' }, ' def'])
})
})

0 comments on commit 796f01d

Please sign in to comment.