我有一个问题-如何在React Intl中换行? 我将我的翻译放在文件中,例如:
[LOCALES.ENGLISH]: {
'hello': 'Hello',
}
如何使用 . 来调用它们?怎样制作一个包含两行或以上内容的消息?
[LOCALES.ENGLISH]: {
'hello': 'Hello',
}
如何使用 . 来调用它们?怎样制作一个包含两行或以上内容的消息?
使用参数添加<br/>
标签:
const messages = {
MESSAGE: {
id: "message",
defaultMessage: "<p>Message with {br} linebreaks</p>"
}
};
export default function App() {
return (
<IntlProvider locale="en" messages={messages}>
<div className="App">
<FormattedMessage
{...messages.MESSAGE}
values={{
p: (...chunks) => <p>{chunks}</p>,
br: <br />
}}
/>
</div>
</IntlProvider>
);
}
完整沙箱: https://codesandbox.io/s/formattedmessage-key-error-6kx6c?file=/src/App.js
\n
并应用 white-space: pre-line
样式。{'hello': 'Hello \n World'}
<p style="white-space: pre-line;"><FormattedMessage id="hello"/></p>
您可以使用带有变量的消息,并将<br/>
作为变量的值发送,如下所示:
<FormattedMessage {...strings.headTitle} values={{br: <br/>}} />
字符串:
export default defineMessages({
headTitle: {
id: 'test',
defaultMessage: 'test {br} next line',
} })
从'react-html-parser'导入ReactHtmlparser;
在下面的类构造函数中添加super()之后 这.formatMessage = props.intl.formatMessage
并在你的jsx中使用
{ReactHtmlparser(this.formatMessage({id: "your id"}))}
export default injectIntl(MyClass);