小编cro*_*mir的帖子

如何在 intl.formatMessage 中使用占位符

我正在尝试react-intl向我的应用程序添加本地化。像这样

<FormattedHTMLMessage id="marker.title" values={{
        name: (b.name !== null ? b.name : "Bench"),
        seats: Tools.showValue(b.seats),
        material: Tools.showValue(b.material),
        color: Tools.getColorNameFromInt(b.color),
        lat: b.lat,
        lng: b.lng,
    }}/>
Run Code Online (Sandbox Code Playgroud)

但我需要一个字符串所以我试过这个

const title = this.props.intl.formatMessage({
    id: "marker.title",
    values: {
        name: (b.name !== null ? b.name : "Bench"),
        seats: Tools.showValue(b.seats),
        material: Tools.showValue(b.material),
        color: Tools.getColorNameFromInt(b.color),
        lat: b.lat,
        lng: b.lng,
    }
});
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息:

Error: The intl string context variable 'name' was not provided to the string '{name}<br/>Seats: {seats}<br/>Material: {material}<br/>Color: {color}<br/>Location: {lat} / {lng}'
Run Code Online (Sandbox Code Playgroud)

en.json

{
  "marker.title": "{name}<br/>Seats: {seats}<br/>Material: …
Run Code Online (Sandbox Code Playgroud)

reactjs react-intl

4
推荐指数
1
解决办法
7472
查看次数

标签 统计

react-intl ×1

reactjs ×1