[英]React intl return [object Object]
I am trying to use react intl to translate the page of my site.我正在尝试使用 react intl 来翻译我网站的页面。 Github
GitHub
json lang : json 语言:
fr.json :
{
"errorPage.title": Erreur 404"
}
en.json :
{
"errorPage.title": Error 404"
}
I have that on my js file我的 js 文件中有这个
const title = <FormattedMessage id="errorPage.title" defaultMessage="Erreur 404" />
document.title = title;
That return on my title page this [object Object]在我的标题页上返回这个 [object Object]
How i can do ?我该怎么办? Thank's !
谢谢 !
Since the key contains .由于密钥包含 . Notation you need to access key like
您需要访问密钥的符号,例如
object["errorPage.title"]
Also the json key value you posted has double quotes missing in the start此外,您发布的 json 键值在开头缺少双引号
You need to do something like below to make it work您需要执行以下操作才能使其正常工作
const object = {
"errorPage.title": "Error 404"
}
const title = object["errorPage.title"];
document.title = title;
I know this is an old question, but maybe the answer can help someone.我知道这是一个老问题,但也许答案可以帮助某人。
The FormattedMessage
is a React component that provides a declarative way of using localized messages. FormattedMessage
是一个 React 组件,它提供了一种使用本地化消息的声明方式。 Therefore, the value of the variable title
(from the question) is not a string but an object.因此,变量
title
(来自问题)的值不是字符串而是对象。
To achieve this, you should imperatively use the react-intl
( official doc ).为此,您应该强制使用
react-intl
(官方文档)。
const title = intl.formatMessage({ id: "errorPage.title", defaultMessage: "Erreur 404" });
document.title = title;
You can find more examples of the react-intl
imperative and declarative way of the usage here .您可以在此处找到有关
react-intl
命令式和声明式用法的更多示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.