【问题标题】:How to use react-i18next when mapping data that comes not from the translation.json映射不是来自translation.json的数据时如何使用react-i18next
【发布时间】:2021-11-04 02:57:44
【问题描述】:

这是我要映射的对象数组。如您所见,它还包含material-ui 图标组件。这就是为什么我无法通过 locales 中的 translation.json 进行映射

  const sidebarData = [

 {
      text: "Picken",
      icon: <GetAppIcon style={{ fontSize: 35 }} />,
      path: "/picken",
      title: "Artikel picken",
 },
 {
      text: "Hole Artikel",
      icon: <AddShoppingCartIcon style={{ fontSize: 35 }} />,
      path: "/holeartikel",
      title: "Hole Artikel zu dieser Workstation",
 },
 {
      text: "Hole Regal",
      icon: <ImportExportIcon style={{ fontSize: 35 }} />,
      path: "/holeregal",
      title: "Hole Regal zu dieser Workstation",
 }];

英文翻译的语言环境 (locales/en/translation.json) 如下所示:

{
 "sidebarmenu": {
      "jobliste": "Joblist",
      "picken": "Picken",
      "artikel": "Get Item",
      "regal": "Get Pod",
      "leer": "Get Storage",
      "karte": "Show Map"
 },
 "header": {
      "jobliste": "Joblist",
      "picken": "Pick Item",
      "artikel": "Get Item to this workstation",
      "regal": "Get Pod to this workstation",
      "leer": "Get empty storage to this workstation",
      "karte": "Show Warehouse Map"
 }

组件看起来像这样:

 import React, { useState, useEffect} from "react";
 import { useHistory } from "react-router-dom";
 import layouttheme from "./layouttheme";
 import ListAltIcon from "@material-ui/icons/ListAlt";
 import {Drawer,Typography,Divider,ListItemText,ListItem,List,Box,} from "@material-ui/core";
 import useStyles from "./LayoutStyles";
 import {useTranslation } from "react-i18next";


 function DrawerComp({ open, setOpen, sidebarData }) {

 const classes = useStyles();
 const history = useHistory();
 const [getPath, setGetPath] = useState("Jobliste");
 const { t } = useTranslation();

 function handleClick(item) {
      history.push(item.path);
      setGetPath(item.title);
 }

 
 return (
      <Drawer variant="persistent" anchor="left" open={open}>
           <List className={classes.sbList} sidebarData={sidebarData}>
                {sidebarData.map(item => (
                     <ListItem
                          key={item.text}
                          onClick={() => handleClick(item)}
                     >
                          <ListItemIcon>{item.icon}</ListItemIcon>
                          <ListItemText>{item.text}</ListItemText>
                     </ListItem>
                ))}
           </List>
      </Drawer>
 );
 }

 export default DrawerComp;

所以我的问题是如何将 sidebarData 与 translation.json 连接以获得翻译?

【问题讨论】:

    标签: javascript reactjs internationalization i18next array.prototype.map


    【解决方案1】:

    如果此对象由父组件组成,您可以翻译 sidebarData

      const sidebarData = [{
          text: t("picken"),
          icon: <GetAppIcon style={{ fontSize: 35 }} />,
          path: "/picken",
          title: "Artikel picken",
     }, {
          text: t("hole_artikel"),
          icon: <AddShoppingCartIcon style={{ fontSize: 35 }} />,
          path: "/holeartikel",
          title: "Hole Artikel zu dieser Workstation",
     }, {
          text: t("hole_regal"),
          icon: <ImportExportIcon style={{ fontSize: 35 }} />,
          path: "/holeregal",
          title: "Hole Regal zu dieser Workstation",
     }];
    

    您还可以使用对象中的翻译键而不是全文。然后在您的渲染函数中,使用t() 包装属性。

    我个人不喜欢这样做,因为您无法使用 i18next-scanner 之类的工具提取翻译。

      const sidebarData = [{
          text: "picken",
          icon: <GetAppIcon style={{ fontSize: 35 }} />,
          path: "/picken",
          title: "Artikel picken",
     }, {
          text: "hole_artikel",
          icon: <AddShoppingCartIcon style={{ fontSize: 35 }} />,
          path: "/holeartikel",
          title: "Hole Artikel zu dieser Workstation",
     }, {
          text: "hole_regal",
          icon: <ImportExportIcon style={{ fontSize: 35 }} />,
          path: "/holeregal",
          title: "Hole Regal zu dieser Workstation",
     }];
    
     return (
          <Drawer variant="persistent" anchor="left" open={open}>
               <List className={classes.sbList} sidebarData={sidebarData}>
                    {sidebarData.map(item => (
                         <ListItem
                              key={item.text}
                              onClick={() => handleClick(item)}
                         >
                              <ListItemIcon>{item.icon}</ListItemIcon>
                              <ListItemText>{t(item.text)}</ListItemText>
                         </ListItem>
                    ))}
               </List>
          </Drawer>
     );
    

    【讨论】:

    • 不幸的是我不能在对象中使用翻译键,因为整个对象在不同的​​ js 文件中,这不是一个函数组件。所以在这个文件(sidebarData.js)中我不能使用 {useTranslation}-Hook 然后我得到了“t”未定义的错误
    • 那么第二个选项对你有用,对吧?
    • 不,它不起作用..但我不确定我是否弄错了:第二个选项是否意味着我的对象中的数据没有改变,但我只是写了这个即时消息我的函数组件 {t(item.text)}?
    • 不,sidebarData 在我给出的示例中使用了翻译键。您不必这样做,但它与您正在使用的当前翻译约定相匹配。
    • 得到它并且它有效。真的真的很酷!非常感谢!
    猜你喜欢
    • 2017-07-07
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多