react i18n国际化配置

 Quick start - react-i18next documentation

1.安装

cnpm i react-i18next i18next -S

2. 基本和官网差不多,只是分离了中英文的json文件

import i18n from "i18next";
import { initReactI18next } from "react-i18next";

import translation_en from "./en.json";
import translation_zh from "./zh.json";

const resources = {
  en: {
    translation: translation_en,
  },
  zh: {
    translation: translation_zh,
  },
};

i18n
  .use(initReactI18next) // passes i18n down to react-i18next
  .init({
    resources,
    lng: "zh",
    // 需要链式调用messages.welcome
    // keySeparator: false, // we do not use keys in form messages.welcome
    interpolation: {
      escapeValue: false, // react already safes from xss
    },
  });

export default i18n;

3.引入 index.jsx

import "./i18n/configs"

4.使用

zh.json

{
    "header": {
        "slogan": "让旅行更幸福",

HOC使用 

import { withTranslation, WithTranslation } from "react-i18next";

class HeaderComponnet extends React.Component<
  RouteComponentProps & WithTranslation,
  State
> { {t("header.add_new_language")}}
export const Header = withTranslation()(withRouter(HeaderComponnet));

Hook使用

import { useTranslation } from "react-i18next";
const { t } = useTranslation();
  {t("footer.detail")}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值