React國際化react-i18next詳解

簡介
react-i18next 是基于 i18next 的一款強(qiáng)大的國際化框架,可以用于 react 和 react-native 應(yīng)用,是目前非常主流的國際化解決方案。
i18next 有著以下優(yōu)點(diǎn):
- 基于i18next不僅限于react,學(xué)一次就可以用在其它地方
- 提供多種組件在hoc、hook和class的情況下進(jìn)行國際化操作
- 適合服務(wù)端的渲染
- 歷史悠久,始于2011年比大多數(shù)的前端框架都要年長
- 因?yàn)闅v史悠久所以更成熟,目前還沒有i18next解決不了的國際化問題
- 有許多插件的支持,比如可以用插件檢測當(dāng)前系統(tǒng)的語言環(huán)境,從服務(wù)器或者文件系統(tǒng)加載翻譯資源
安裝
需要同時安裝 i18next 和 react-i18next 依賴:
npm install react-i18next i18next --save
或
yarn add react-i18next i18next --save
配置
在src下新建i18n文件夾,以存放國際化相關(guān)配置
i18n中分別新建三個文件:
config.ts:對 i18n 進(jìn)行初始化操作及插件配置en.json:英文語言配置文件zh.json:中文語言配置文件
en.json
{
"header": {
"register":"Register",
"signin":"Sign In",
"home": "Home"
},
"footer": {
"detail" : "All rights reserved @ React"
},
"home": {
"hot_recommended": "Hot Recommended",
"new_arrival": "New arrival",
"joint_venture": "Joint Venture"
}
}
zh.json
{
"header": {
"register":"注冊",
"signin":"登陸",
"home": "首頁"
},
"footer": {
"detail" : "版權(quán)所有 @ React"
},
"home": {
"hot_recommended": "爆款推薦",
"new_arrival": "新品上市",
"joint_venture": "合作企業(yè)"
}
}
config.ts
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).init({
resources,
lng: 'zh',
interpolation: {
escapeValue: false,
},
});
export default i18n;
使用
引用配置文件
在index.tsx中引用i18n的配置文件 :import './i18n/config';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import './i18n/config'; // 引用配置文件
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
在組件中使用
方法一
在 類組件 中使用withTranslation 高階函數(shù)(HOC) 來完成語言配置的數(shù)據(jù)注入
import React from 'react';
import styles from './Home.module.css';
// 引入HOC高階函數(shù)withTranslation 和 i18n的ts類型定義WithTranslation
import { withTranslation, WithTranslation } from "react-i18next"
class HomeComponent extends React.Component<WithTranslation> {
render() {
const { t } = this.props;
return <>
<h1>{t('header.home')}</h1>
<ul>
<li>{t('home.hot_recommended')}</li>
<li>{t('home.new_arrival')}</li>
<li>{t('home.joint_venture')}</li>
</ul>
</>
}
}
export const Home = withTranslation()(HomeComponent); // 使用withTranslation高階函數(shù)來完成語言配置的數(shù)據(jù)注入
方法二
在 函數(shù)式組件 中使用useTranslation 的 hook 來處理國際化
import React from 'react';
import { useTranslation, Trans } from 'react-i18next'
export const Home: React.FC = () => {
const { t } = useTranslation()
return (
<div>
<h1>{t('header.home')}</h1>
<ul>
<li>{t('home.hot_recommended')}</li>
{/* 還有一種方式 */}
<li><Trans>home.new_arrival</Trans></li>
</ul>
</div>
);
};
切換語言
import i18n from 'i18next';
const changeLanguage= (val) => {
i18n.changeLanguage(val); // val入?yún)⒅禐?en'或'zh'
};
或
import React from 'react';
import { useTranslation } from 'react-i18next'
export const Home: React.FC = () => {
const { t, i18n } = useTranslation()
return (
<button onClick={()=>i18n.changeLanguage(i18n.language=='en'?'zh':'en')}>{i18n.language=='en'?'zh':'en'}</button>
);
};
到此這篇關(guān)于React國際化react-i18next的文章就介紹到這了,更多相關(guān)React國際化react-i18next內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React實(shí)現(xiàn)基于Antd密碼強(qiáng)度校驗(yàn)組件示例詳解
這篇文章主要為大家介紹了React實(shí)現(xiàn)基于Antd密碼強(qiáng)度校驗(yàn)組件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法)
i18next?是一款強(qiáng)大的國際化框架,react-i18next?是基于?i18next?適用于?React?的框架,本文介紹了?react-i18next?的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案2023-01-01
React通過useContext特性實(shí)現(xiàn)組件數(shù)據(jù)傳遞
本文主要介紹了React如何通過useContext特性實(shí)現(xiàn)組件數(shù)據(jù)傳遞,文中有相關(guān)的代碼示例供大家參考,對我們學(xué)習(xí)React有一定的幫助,需要的朋友可以參考下2023-06-06
react echarts tooltip 區(qū)域新加輸入框編輯保存數(shù)據(jù)功能
這篇文章主要介紹了react echarts tooltip 區(qū)域新加輸入框編輯保存數(shù)據(jù)功能,大概思路是用一個div包裹echarts, 然后在echarts的同級新建一個div用來用來模擬真實(shí)tooltip,通過鼠標(biāo)移入移出事件控制真實(shí)tooltip的顯示與隱藏,需要的朋友可以參考下2023-05-05

