react国际化(react整合多语言)

一起探讨学习

欢迎大家进群,一起讨论学习

每天给大家提供技术干货

在这里插入图片描述

博主技术笔记 https://notes.xiyankt.com


博主开源微服架构前后端分离技术博客项目源码地址,欢迎各位star https://gitee.com/bright-boy/xiyan-blog


1.安装

npm install i18next react-i18next --save

2.在src文件下新建配置文件 i18n.js

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import Cookie from "js-cookie";
// not like to use this?
// have a look at the Quick start guide
// for passing in lng and translations on init

i18n
    // load translation using xhr -> see /public/locales
    // learn more: https://github.com/i18next/i18next-xhr-backend
    // .use(Backend)
    // detect user language
    // learn more: https://github.com/i18next/i18next-browser-languageDetector
    // .use(LanguageDetector)
    // pass the i18n instance to react-i18next.
    .use(initReactI18next)
    // init i18next
    // for all options read: https://www.i18next.com/overview/configuration-options
    .init({
        resources: {
            EN: {
                translations: require("./locale/en.json"),
            },
            CN: {
                translations: require("./locale/zh.json"),
            },
        },
        // fallbackLng: 'zh-CN', // 使用LanguageDetector 取消注释
        fallbackLng: Cookie.get("lang") === "EN" ? "EN" : "CN",
        debug: true,
        // have a common namespace used around the full app
        ns: ["translations"],
        defaultNS: "translations",

        keySeparator: false, // we use content as keys

        interpolation: {
            escapeValue: false, // not needed for react!!
        },

        react: {
            wait: true,
        },
    });

export default i18n;

3.静态资源下新建json语言包,有几种语言建几种包,文件名

例:
中文包 zh.json

{
    "cn": "中文",
    "en": "英语",
    "welcome": "欢迎你:"
}

英文包 en.json

{
  "cn": "CN",
  "en": "EN",
  "welcome": "Welcome:"
}

使用
将配置文件引入到indexjs入口文件

//引入i18n.js
import './i18n'

4.i18next的使用(简单方便,但是在当前页面不能及时刷新)

import i18next from "i18next";

// 这种写法,必须手动刷新,不能及时刷新
 <div>{i18next.t("en")}</div>
 
react-i18next的使用(使用复杂,但在当前页面切换可以不刷新切换),使用时需要包裹当前组件
import { withTranslation } from "react-i18next";

 <div style={{ margin: 10 }}>
    <p>{this.props.t("en")}</p>
  </div>

export default withTranslation("translations")(LongTxt);
 
hook写法使用
import { useTranslation } from "react-i18next";

const Dashboard = (props) => {
    const { t } = useTranslation();
    return <div>{t("welcome")}</div>
}

5.切换的时候设置语言

onClick = (key) => {
        Cookie.set("lang", key.key)
        //设置语言
        i18next.changeLanguage(key.key.toUpperCase())
        this.setState({locale: i18next.t(key.key.toUpperCase())})
    };
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘明同学呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值