Skip to content

Commit

Permalink
Merge pull request #22 from synacor/markuptext-fallback
Browse files Browse the repository at this point in the history
Render all fallback children for MarkupText nodes instead of just the first one
  • Loading branch information
billneff79 authored May 18, 2018
2 parents 0287d0f + 9e13b73 commit a0f7944
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lib/translate-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export default function translateMapping(props, intl, onlyTextNodes) {
}
else if (def.nodeName===Text) {
// it's a <Text />, 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ describe('intl', () => {
expect(root.innerHTML).to.equal('<span><b>FOO</b></span>');
});

it('should render multi-child fallback', () => {
rndr(
<div>
<MarkupText>This <b>is the fallback</b> with multiple children</MarkupText>
</div>
);

expect(root.innerHTML).to.equal('<span>This <b>is the fallback</b> with multiple children</span>');
});

it('should render text without scope', () => {
rndr(
<div>
Expand Down

0 comments on commit a0f7944

Please sign in to comment.