React intl return [对象对象]

时间:2018-09-30 18:37:33

标签: reactjs internationalization react-intl

我正在尝试使用react intl来翻译我网站的页面。 Github

json lang:

fr.json : 
{
    "errorPage.title": Erreur 404"
}

en.json : 
{
    "errorPage.title": Error 404"
}

我在我的js文件中有

const title = <FormattedMessage id="errorPage.title" defaultMessage="Erreur 404" />

document.title = title;

在我的标题页上返回了这个[object Object]

我该怎么办? 谢谢!!

2 个答案:

答案 0 :(得分:0)

由于密钥包含。您需要访问类似

这样的键的符号
# Reading in 3 lines of data and testing your answer
ans_3lines = read_dow('data/dow_jones_index.data', num_lines=3)
sol_3lines = [
['quarter', 'stock', 'date', 'open', 'high', 'low', 'close', 'volume',
 'percent_change_price', 'percent_change_volume_over_last_wk', 
'previous_weeks_volume',
 'next_weeks_open', 'next_weeks_close', 
'percent_change_next_weeks_price', 'days_to_next_dividend',
 'percent_return_next_dividend'],
['1', 'AA', '1/7/2011', '15.82', '16.72', '15.78', '16.42', 
'239655616', '3.79267', '', '', '16.71',
 '15.97', '-4.42849', '26', '0.182704'],
['1', 'AA', '1/14/2011', '16.71', '16.71', '15.64', '15.97', 
'242963398', '-4.42849', '1.380223028',
 '239655616', '16.19', '15.79', '-2.47066', '19', '0.187852']
]
assert_equal(ans_3lines, sol_3lines, msg='Your answer does not match 
the solutions')

# Reading in 5 lines of data and testing your answer
ans_5lines = read_dow('data/dow_jones_index.data')
sol_5lines = [
['quarter', 'stock', 'date', 'open', 'high', 'low', 'close', 'volume',
 'percent_change_price', 'percent_change_volume_over_last_wk', 
'previous_weeks_volume',
 'next_weeks_open', 'next_weeks_close', 
'percent_change_next_weeks_price', 'days_to_next_dividend',
 'percent_return_next_dividend'],
['1', 'AA', '1/7/2011', '15.82', '16.72', '15.78', '16.42', 
'239655616', '3.79267', '', '', '16.71',
 '15.97', '-4.42849', '26', '0.182704'],
['1', 'AA', '1/14/2011', '16.71', '16.71', '15.64', '15.97', 
'242963398', '-4.42849', '1.380223028',
 '239655616', '16.19', '15.79', '-2.47066', '19', '0.187852'],
['1', 'AA', '1/21/2011', '16.19', '16.38', '15.60', '15.79', 
'138428495', '-2.47066', '-43.02495926',
 '242963398', '15.87', '16.13', '1.63831', '12', '0.189994'],
['1', 'AA', '1/28/2011', '15.87', '16.63', '15.82', '16.13', 
'151379173', '1.63831', '9.355500109',
 '138428495', '16.18', '17.14', '5.93325', '5', '0.185989']
]
assert_equal(ans_5lines, sol_5lines, msg='Your answer does not match 
the solutions')

此外,您发布的json键值在开头也有双引号

您需要执行以下操作才能使其正常工作

object["errorPage.title"]

答案 1 :(得分:0)

我知道这是一个老问题,但也许答案可以帮助某人。

FormattedMessage 是一个 React 组件,它提供了一种使用本地化消息的声明性方式。因此,变量 title 的值(来自问题)不是字符串而是对象。

要实现这一点,您应该强制使用 react-intl (official doc)。

const title = intl.formatMessage({ id: "errorPage.title", defaultMessage: "Erreur 404" });

document.title = title;

您可以找到更多使用 herereact-intl 命令式和声明式方式的示例。

相关问题