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);
    记录自己的采坑之路,需要时方便查找
  • 相关阅读:
    Delphi
    delphi trayIcon控件,如何实现窗口最小化的时候到系统托盘
    delphi2010自带 TTrayIcon 托盘图标控件使用方法
    通过例子来简单了解下TProgressBar的使用。 pas文件程序如下
    ORA-12154,ORA-12560解决过程
    博客备份小工具3
    各大招聘网站信息实时查询浏览
    IE7中使用Jquery动态操作name问题
    js问题杂记
    动态sql
  • 原文地址:https://www.cnblogs.com/hahahakc/p/14703980.html
Copyright © 2011-2022 走看看