lbry-desktop/ui/component/i18nMessage/view.jsx
2019-11-11 13:27:29 -05:00

35 lines
887 B
JavaScript

// @flow
import React from 'react';
type Props = {
tokens: Object,
children: string, // e.g. "Read %faq_link% for more detail"
};
export default function I18nMessage(props: Props) {
const message = __(props.children),
regexp = /%\w+%/g,
matchingGroups = message.match(regexp);
if (!matchingGroups) {
return message;
}
const messageSubstrings = props.children.split(regexp),
tokens = props.tokens;
return (
<React.Fragment>
{messageSubstrings.map((substring, index) => {
const token =
index < matchingGroups.length ? matchingGroups[index].substring(1, matchingGroups[index].length - 1) : null; // get token without % on each side
return (
<React.Fragment key={index}>
{substring}
{token && tokens[token]}
</React.Fragment>
);
})}
</React.Fragment>
);
}