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

Feature: array option #22

Merged
merged 4 commits into from
Jan 30, 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
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).
38 changes: 24 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,8 +39,8 @@
return obj && typeof obj === 'object'
}

function assemble (parts, replacements, count, debug) {
var result = [].concat(parts)
function assemble (parts, replacements, count, debug, asArray) {
var result = asArray ? parts.slice() : parts[0]
var len = parts.length
for (var i = 1; i < len; i += 2) {
var part = parts[i]
Expand All @@ -52,13 +53,18 @@
val = '{' + part + '}'
}
}
result[i] = val
if ( asArray ) {
result[i] = val
} else {
result += val + parts[i+1]
}
}
return result
}

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 +105,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, debug, tFunc.opts.array) : 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 +137,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'])
})
})