diff --git a/README.md b/README.md index 7f34538..6cfd692 100644 --- a/README.md +++ b/README.md @@ -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. } @@ -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). diff --git a/index.js b/index.js index 7971417..3245772 100644 --- a/index.js +++ b/index.js @@ -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. * } * @@ -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) { @@ -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. @@ -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 @@ -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 } diff --git a/test.js b/test.js index 65d8e4c..e9bea0d 100644 --- a/test.js +++ b/test.js @@ -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']) + }) })