From 9e13b73c83709a129beb0360dbcf9cb88d85460a Mon Sep 17 00:00:00 2001 From: Bill Neff Date: Fri, 18 May 2018 16:04:22 -0400 Subject: [PATCH] markuptext-fallback render all fallback children for MarkupText nodes instead of just the first one --- src/lib/translate-mapping.js | 4 +++- src/lib/translate.js | 2 +- test/index.js | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib/translate-mapping.js b/src/lib/translate-mapping.js index 3eb5691..015b090 100644 --- a/src/lib/translate-mapping.js +++ b/src/lib/translate-mapping.js @@ -27,8 +27,10 @@ export default function translateMapping(props, intl, onlyTextNodes) { } else if (def.nodeName===Text) { // it's a , just grab its props: + let c = def.children; def = assign({ - fallback: def.children && def.children[0] + //no fallback if there are no children. Return first child if there is only 1, or array of children if there are more than one + fallback: c.length && (c.length === 1 ? c[0] : c) }, def.attributes); out[name] = translate(def.id, intl.scope, intl.dictionary, def.fields, def.plural, def.fallback); } diff --git a/src/lib/translate.js b/src/lib/translate.js index 1968ad3..5d5696d 100644 --- a/src/lib/translate.js +++ b/src/lib/translate.js @@ -12,7 +12,7 @@ import template from './template'; * @param {Object} dictionary A nested object containing translations * @param {Object} [fields={}] Template fields for use by translated strings * @param {Number} [plural] Indicates a quantity, used to trigger pluralization - * @param {String} [fallback=''] Text to return if no translation is found + * @param {String|Array} [fallback] Text to return if no translation is found * @returns {String} translated */ export default function translate(id, scope, dictionary, fields, plural, fallback) { diff --git a/test/index.js b/test/index.js index cb63934..557b2b4 100644 --- a/test/index.js +++ b/test/index.js @@ -251,6 +251,16 @@ describe('intl', () => { expect(root.innerHTML).to.equal('FOO'); }); + it('should render multi-child fallback', () => { + rndr( +
+ This is the fallback with multiple children +
+ ); + + expect(root.innerHTML).to.equal('This is the fallback with multiple children'); + }); + it('should render text without scope', () => { rndr(