消除 Antd Pro 的 i18n 缺失翻译警告提示 Missing message

文章目录

    由于我用 Antd Pro 开发的管理后台只需要中文,并不需要其他语言的翻译,所以代码里就没有使用翻译组件。

    但是在运行时,浏览器一堆警告信息:

    devScripts.js:6523 [React Intl] Missing message: “menu.知识库” for locale: “zh-CN”, using default message as fallback.

    消除 i18n 翻译警告的方法

    编辑 config/config.ts:

    将 layout 配置中的 locale 注释掉:

      layout: {
        // https://umijs.org/zh-CN/plugins/plugin-layout
        //locale: true,
        siderWidth: 208,
        ...defaultSettings,
      },
    

    这样绝大多数的警告就消失了,但是还是残留了一个:

    devScripts.js:6523 [React Intl] Missing message: “app.copyright.produced” for locale: “zh-CN”, using default message as fallback.

    编辑:

    src/components/Footer/index.tsx

    删除翻译组件,并将 footer 组件返回置空。

    import { useIntl } from 'umi';
    import { GithubOutlined } from '@ant-design/icons';
    import { DefaultFooter } from '@ant-design/pro-layout';
    
    export default () => {
      const intl = useIntl();
      const defaultMessage = "";
    
      return (
        /*
        <DefaultFooter
          copyright={`2021 ${defaultMessage}`}
        />
        */
        <div></div>
      );
    };
    

    这样就可以了。

    内置的方法

    不推荐使用!!!

    package.json 中可以看到这个命令

    "i18n-remove": "pro i18n-remove --locale=zh-CN --write",
    

    所以执行:

    tyarn i18n-remove
    

    可以看到:

    yarn run v1.22.10
    $ pro i18n-remove --locale=zh-CN --write
    ✔ 🕵  find js or ts files
    ✔ 📦  load all locale file and build ts
    ✔ ✂  remove locale for src/components/Footer/index.tsx.
    ✔ ✂  remove locale for src/global.tsx.
    ✔ ✂  remove locale for src/pages/Admin.tsx.
    ✔ ✂  remove locale for src/pages/AdminUsers/components/UpdateForm.tsx.
    ✔ ✂  remove locale for src/pages/AdminUsers/index.tsx.
    ✔ ✂  remove locale for src/pages/Articles/components/UpdateForm.tsx.
    ✔ ✂  remove locale for src/pages/Articles/index.tsx.
    ✔ ✂  remove locale for src/pages/Devices/components/UpdateForm.tsx.
    ✔ ✂  remove locale for src/pages/Devices/index.tsx.
    ✔ ✂  remove locale for src/pages/TableList/components/UpdateForm.tsx.
    ✔ ✂  remove locale for src/pages/TableList/index.tsx.
    ✔ ✂  remove locale for src/pages/user/Login/index.tsx.
    ✔ ✂  remove locale for src/pages/Welcome.tsx.
    Done in 40.16s.
    

    从 git diff 看,还修改了很多不需要修改的地方,而且就算修改了,还是要去修改 config.ts 文件,
    所以用这个的意义不大,就是自动删除了翻译组件。

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式