如何与表ANTD一起使用React-I18Next
我有一个使用antd
创建的表组件:
import { Table } from "antd";
const dataSource = [
{
key: "1",
name: "Mike",
age: 32,
address: "Downing Street"
},
{
key: "2",
name: "John",
age: 42,
address: "Downing Street"
}
];
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age"
},
{
title: "Address",
dataIndex: "address",
key: "address"
}
];
const CustomTable = () => {
return (
<>
<Table dataSource={dataSource} columns={columns} />
</>
);
};
export default CustomTable;
现在我想将翻译添加到每个列
name。我希望在react-i18next
使用此库的一个简单示例是以下代码:
const {t} = useTranslation();
t('textToTranslate');
此外,我还有一些翻译。价值类似:
{
"name": "name",
"age": "age"
}
如何将翻译添加到每个列
- &gt; 名称
?
I have following Table component created with antd
:
import { Table } from "antd";
const dataSource = [
{
key: "1",
name: "Mike",
age: 32,
address: "Downing Street"
},
{
key: "2",
name: "John",
age: 42,
address: "Downing Street"
}
];
const columns = [
{
title: "Name",
dataIndex: "name",
key: "name"
},
{
title: "Age",
dataIndex: "age",
key: "age"
},
{
title: "Address",
dataIndex: "address",
key: "address"
}
];
const CustomTable = () => {
return (
<>
<Table dataSource={dataSource} columns={columns} />
</>
);
};
export default CustomTable;
Now I would like to add translations to each columns
name. I would like this solution to be obtained with the help of React-i18next
A simple example of using this library is the following code:
const {t} = useTranslation();
t('textToTranslate');
Additionally, I have some translate.json where are my translations key & value like:
{
"name": "name",
"age": "age"
}
How I can add translation to each columns
-> name
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以更改生成列的方式,并将翻译用作典型的React Prop:
You can change the way of generating columns and use translation as a typical react prop:
您可以使用的另一种方法是将列作为Props传递给您的可定制组件,然后将翻译添加到标题中,如下所示:
Another approach you can use is passing the columns as props to your CustomTable component and then adding the translation to the title as follows: