如何与表ANTD一起使用React-I18Next

发布于 2025-02-04 15:55:46 字数 1004 浏览 2 评论 0原文

我有一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2025-02-11 15:55:46

您可以更改生成列的方式,并将翻译用作典型的React Prop:

const dataSource = [
  {
    key: "1",
    name: "Mike",
    age: 32,
    address: "Downing Street"
  },
  {
    key: "2",
    name: "John",
    age: 42,
    address: "Downing Street"
  }
];

const columns = [
  {
    title: "tableHeadings.name",
    dataIndex: "name",
    key: "name"
  },
  {
    title: "tableHeadings.age",
    dataIndex: "age",
    key: "age"
  },
  {
    title: "tableHeadings.address",
    dataIndex: "address",
    key: "address"
  }
];

const CustomTable = () => {
  const { t } = useTranslation();

  return (
    <>
      <Table dataSource={dataSource}>
        {columns.map((column) => (
          <Table.Column
            key={column.key}
            title={t(column.title)}
            dataIndex={column.dataIndex}
          />
        ))}
      </Table>
    </>
  );
};

export default CustomTable;

You can change the way of generating columns and use translation as a typical react prop:

const dataSource = [
  {
    key: "1",
    name: "Mike",
    age: 32,
    address: "Downing Street"
  },
  {
    key: "2",
    name: "John",
    age: 42,
    address: "Downing Street"
  }
];

const columns = [
  {
    title: "tableHeadings.name",
    dataIndex: "name",
    key: "name"
  },
  {
    title: "tableHeadings.age",
    dataIndex: "age",
    key: "age"
  },
  {
    title: "tableHeadings.address",
    dataIndex: "address",
    key: "address"
  }
];

const CustomTable = () => {
  const { t } = useTranslation();

  return (
    <>
      <Table dataSource={dataSource}>
        {columns.map((column) => (
          <Table.Column
            key={column.key}
            title={t(column.title)}
            dataIndex={column.dataIndex}
          />
        ))}
      </Table>
    </>
  );
};

export default CustomTable;
扛刀软妹 2025-02-11 15:55:46

您可以使用的另一种方法是将列作为Props传递给您的可定制组件,然后将翻译添加到标题中,如下所示:

const CustomTable = ({ columns }) => {
const { t } = useTranslation();

const tColumns = columns.map((column) => ({
    ...column,
    title: t(`${column.title}`),
  }));

return (
  <>
    <Table dataSource={dataSource} columns={tColumns} />
  </>
 );
};

export default CustomTable;

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:

const CustomTable = ({ columns }) => {
const { t } = useTranslation();

const tColumns = columns.map((column) => ({
    ...column,
    title: t(`${column.title}`),
  }));

return (
  <>
    <Table dataSource={dataSource} columns={tColumns} />
  </>
 );
};

export default CustomTable;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文