简体   繁体   English

React intl return [object Object]

[英]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.

相关问题 将React-intl添加到占位符中 - add React-intl to placeholder IN OBJECT 反应国际常量<FormattedMessage />结果是 [object object] - React-intl const <FormattedMessage /> is giving [object object] as result 在模块内使用 FormattedMessage 时出错:错误:[React Intl] 找不到所需的 `intl` object - Getting error when using FormattedMessage inside a module: Error: [React Intl] Could not find required `intl` object React Intl 找不到所需的“intl”对象。<IntlProvider> 需要存在于组件祖先中 - React Intl Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry 将 react-intl 对象注入已安装的酶组件中进行测试 - Injecting react-intl object into mounted Enzyme components for testing React-Intl 将翻译作为字符串变量而不是 object - React-Intl pass translation as string variable and not object 使用react-intl,但是返回值被转义 - using react-intl, but return value is escaped 传入对象引用时,为什么react-intl中的defineMessages会引发错误? - Why does defineMessages from react-intl throw an error when passing in a object reference? 反应自定义 Hook 以返回 object - React custom Hook to return an object 对象进入数组然后返回一个组件-React JS Application - Object into an array then return a component - React JS Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM