Как использовать заполнитель с intl.formatMessage - PullRequest
0 голосов
/ 03 мая 2018

Я пытаюсь использовать 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,
    }}/>

Но мне нужна строка, поэтому я попробовал это

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,
    }
});

и я получаю следующее сообщение об ошибке:

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}'

en.json

{
  "marker.title": "{name}<br/>Seats: {seats}<br/>Material: {material}<br/>Color: {color}<br/>Location: {lat} / {lng}"
}

1 Ответ

0 голосов
/ 11 мая 2018

Разобрался, если ты так сделаешь, это будет работать

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

Будем надеяться, что я снова найду этот ответ, когда снова застряну в этом.

...