zoukankan      html  css  js  c++  java
  • React-i18next切换中英文

    https://zhuanlan.zhihu.com/p/94788672

    https://github.com/i18next/react-i18next/pull/759/files

    1. 安装依赖

    yarn add react-i18next i18next i18next-browser-languagedetector

    2. 配置多语言json

    /locales/en-us.json:

    {
        "user": {
            "user_management": "User Management",
            "user_id": "User ID",
            "status": "Status",
            "role": "Role",
            "search": "Search",
            "reset": "Reset",
            "add_new": "Add New",
            "edit_user": "Edit User",
            "add_user": "Add User",
            "first_name": "First Name",
            "last_name": "Last Name",
            "email_address": "Email Address",
            "operate": "Operate"
        }
    }

    /locales/zh-cn.json:

    {
        "user": {
            "user_management": "用户管理",
            "user_id": "用户 ID",
            "status": "状态",
            "role": "角色",
            "search": "搜索",
            "reset": "重置",
            "add_new": "添加用户",
            "edit_user": "编辑用户",
            "add_user": "添加用户",
            "first_name": "名字",
            "last_name": "姓",
            "email_address": "邮箱地址",
            "operate": "操作"
        }
    }

     3. 定义i18n.tsx:

    import LanguageDetector from 'i18next-browser-languagedetector';
    import i18n from 'i18next';
    import enUsTrans from './locales/en-us.json';
    import zhCnTrans from './locales/zh-cn.json';
    import { initReactI18next } from 'react-i18next';
    
    i18n
      .use(LanguageDetector) //嗅探当前浏览器语言
      .use(initReactI18next) //init i18next
      .init({
        //引入资源文件
        resources: {
          en: {
            translation: enUsTrans,
          },
          zh: {
            translation: zhCnTrans,
          },
        },
        //选择默认语言,选择内容为上述配置中的key,即en/zh
        fallbackLng: 'en',
        debug: false,
        interpolation: {
          escapeValue: false, // not needed for react as it escapes by default
        },
      });
    
    export default i18n;

    4. 在主入口文件_app.tsx中引用i18n.tsx

    import './i18n';

    5. 高阶组件中使用

    import { withTranslation, WithTranslation } from 'react-i18next';
    
    export class UserManagement extends React.Component<IUserProps & WithTranslation> {}
    export default withTranslation()(UserManagement);
    记录自己的采坑之路,需要时方便查找
  • 相关阅读:
    .ascx
    *.ascx *.asax *.aspx.resx *.asax.resx是什么文件
    DNN Learning How to install 1
    LG7881 [Ynoi2006] rmpq【分块,分治】
    LG6783 [Ynoi2008] rrusq【扫描线,KDT】
    UOJ681【UR #22】月球列车【二进制,Trie】
    AGC056E Cheese【概率期望,dp】
    AGC055F Creative Splitting【双射转化,dp】
    CTT2022 游记
    NOIP2021 退役记
  • 原文地址:https://www.cnblogs.com/hahahakc/p/14703980.html
Copyright © 2011-2022 走看看