• React 中 react-i18next 切换语言( 项目国际化 )


    背景

    平时中会遇到需求,就是切换语言,语种等。其实总的来说都是用i18n来实现的

    思路

    首先在项目中安装i18n插件,然后将插件引入到项目,然后配置语言包(语言包需要你自己来进行配置,自己编写语言包,或者你能找到跟你项目适配的也可以),然后就用特定的方法来展示可以切换语言的字段

    实现步骤

    一、安装环境

    需要同时安装 i18next 和 react-i18next 依赖:

    npm install react-i18next i18next --save
    

    二、配置文件

    src下新建i18n文件夹,以存放国际化相关配置
    i18n中分别新建三个文件

    • config.ts:对 i18n 进行初始化操作及插件配置

    • en.json:英文语言配置文件

    • zh.json:中文语言配置文件

    2.1、config.ts

    1. import i18n from 'i18next';
    2. import { initReactI18next } from 'react-i18next';
    3. import translation_en from './en.json';
    4. import translation_zh from './zh.json';
    5. const resources = {
    6. en: {
    7. translation: translation_en,
    8. },
    9. zh: {
    10. translation: translation_zh,
    11. },
    12. };
    13. i18n.use(initReactI18next).init({
    14. resources,
    15. // 默认语言 zh/en 中文/英文
    16. lng: 'zh',
    17. interpolation: {
    18. escapeValue: false,
    19. },
    20. });
    21. export default i18n;

    2.2、zh.json

    1. {
    2. "language":"语言",
    3. "switch":"选择",
    4. }

    2.3、en.json

    1. {
    2. "language":"Language",
    3. "switch":"Switch",
    4. }

    三、使用

    3.1、引用配置文件

    在 page 的index.tsx中引用i18n的配置文件 :import '../i18n/config';

    1. import Page1 from './page1';
    2. import Page2 from './page2';
    3. // 引用配置文件
    4. import '../i18n/config';
    5. export default function IndexPage() {
    6. return (
    7. <div>
    8. <Page1 />
    9. <Page2 />
    10. </div>
    11. );
    12. }

    3.2、在组件中使用

    在 函数式组件 中使用useTranslation 的 hook 来处理国际化

    1. // Page1 函数式组件
    2. import React from 'react';
    3. // 引入 useTranslation
    4. import { useTranslation } from 'react-i18next';
    5. const Page1: React.FC = () => {
    6. const { t } = useTranslation();
    7. return (
    8. <div>
    9. <p>这是Page1</p>
    10. <p>{t('language')}</p>
    11. </div>
    12. );
    13. };
    14. export default Page1;

    在 类组件 中使用withTranslation 高阶函数(HOC) 来完成语言配置的数据注入

    1. // Page2 类组件
    2. import React from 'react';
    3. // 引入HOC高阶函数 withTranslation 和 i18n 的ts类型定义 WithTranslation
    4. import { withTranslation, WithTranslation } from 'react-i18next';
    5. class ClassComponent extends React.Component<WithTranslation> {
    6. render() {
    7. const { t } = this.props;
    8. return (
    9. <div>
    10. <p>{t('language')}</p>
    11. </div>
    12. );
    13. }
    14. }
    15. // withTranslation 完成语言配置数据注入
    16. export const Page2 = withTranslation()(ClassComponent);

    3.3、切换语言

    使用changeLanguage() config 中配置 切换语言

    1. // 函数式组件
    2. import React from 'react';
    3. import { useTranslation, Trans } from 'react-i18next';
    4. const Page1: React.FC = () => {
    5. const { t, i18n } = useTranslation();
    6. return (
    7. <div>
    8. <button onClick={() => i18n.changeLanguage(i18n.language == 'en' ? 'zh' : 'en')}>
    9. {i18n.language == 'en' ? 'zh' : 'en'}
    10. </button>
    11. <p>{t('language')}</p>
    12. </div>
    13. );
    14. };
    15. export default Page1;
    1. // 类式组件
    2. import i18n from 'i18next';
    3. const changeLanguage= (val) => {
    4. i18n.changeLanguage(val); // 传入 'en' / 'zh'
    5. };

  • 相关阅读:
    万向区块链小课堂:DAO如何革新组织方式?
    java实现贪心算法
    命名块 verilog
    279.完全平方数
    【Mqtt】学习MQTT利器 之 Mosquitto服务器的开源实现
    传输层详解
    Mybatis通过pagehelper插件实现分页
    vue xterm4.x自定义请求报文
    国学---佛系算吉凶~
    进程的认识
  • 原文地址:https://blog.csdn.net/qq_59747594/article/details/134518851