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

Allow react component interpolation #41

Merged
merged 2 commits into from
Jul 25, 2017
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
2 changes: 1 addition & 1 deletion dist/component/component.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions src/component/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import React from 'react'
import ReactDOMServer from 'react-dom/server'
import {PropTypes} from 'prop-types'
import deepForceUpdate from 'react-deep-force-update'
import {setForceRefresh, setLanguage} from '../actions'
Expand All @@ -18,23 +17,29 @@ class I18n extends React.Component {

// It check if text have params
params(text, params) {
if (params !== undefined) {
for (let k in params) {
let reg = new RegExp('\{' + k + '\}', 'g')
let param = params[k];

// Escape possible '$' in params to prevent unexpected behavior with .replace()
// especially important for IE11, which misinterprets '$0' as a regex command
if (typeof param === 'string') {
param = param.replace(/\$/g, '$$$$');
} else if (typeof param === 'object' && param !== null) {
param = ReactDOMServer.renderToStaticMarkup(param)
// if params don't exist, just return the string
if (!params) {
return text;
}

const children = text.split(/({[^}]+})/g)
.map((child) => {
const match = /{(.+)}/g.exec(child);
if (match) {
const param = params[match[1]];
return param ? param : String(param)
}

text = text.replace(reg, param)
}
}
return text
return child;
});

// if any children are objects (i.e. react components), wrap in a span, otherwise return as string
// ignore anything that is falsy, bypassing null, etc
return children.some(child => child && typeof child === 'object')
// When React 16 is released, change the span to an identity function for array children,
// removing the extra dom node
? React.createElement('span', null, ...children)
: children.join('');
}

// Main method for translating texts
Expand Down
2 changes: 1 addition & 1 deletion test/component.immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('immutable component test', function() {
})

it('object as param', function() {
expect(this.objAsParam.textContent).toEqual('Hello <span>Cesc</span>')
expect(this.objAsParam.textContent).toEqual('Hello Cesc')
})

})
2 changes: 1 addition & 1 deletion test/component.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('component test', function() {
})

it('object as param', function() {
expect(this.objAsParam.textContent).toEqual('Hello <span>Cesc</span>')
expect(this.objAsParam.textContent).toEqual('Hello Cesc')
})

})