React國際化react-i18next詳解
簡(jiǎn)介
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解決不了的國際化問題
- 有許多插件的支持,比如可以用插件檢測(cè)當(dāng)前系統(tǒng)的語言環(huán)境,從服務(wù)器或者文件系統(tǒng)加載翻譯資源
安裝
需要同時(shí)安裝 i18next
和 react-i18next
依賴:
npm install react-i18next i18next --save
或
yarn add react-i18next i18next --save
配置
在src
下新建i18n
文件夾,以存放國際化相關(guān)配置
i18n
中分別新建三個(gè)文件:
config.ts
:對(duì) 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":"注冊(cè)", "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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用 React Router Dom 實(shí)現(xiàn)路由導(dǎo)航的詳細(xì)過程
React Router Dom 是 React 應(yīng)用程序中用于處理路由的常用庫,它提供了一系列組件和 API 來管理應(yīng)用程序的路由,這篇文章主要介紹了使用 React Router Dom 實(shí)現(xiàn)路由導(dǎo)航,需要的朋友可以參考下2024-03-03React框架快速實(shí)現(xiàn)簡(jiǎn)易的Markdown編輯器
這篇文章主要為大家介紹了使用React框架實(shí)現(xiàn)簡(jiǎn)易的Markdown編輯器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04新建的React Native就遇到vscode報(bào)警解除方法
這篇文章主要為大家介紹了新建的React Native就遇到vscode報(bào)警解除方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10React報(bào)錯(cuò)信息之Expected?an?assignment?or?function?call?and?
這篇文章主要介紹了React報(bào)錯(cuò)之Expected?an?assignment?or?function?call?and?instead?saw?an?expression,下面有兩個(gè)示例來展示錯(cuò)誤是如何產(chǎn)生的,需要的朋友可以參考下2022-08-08react組件中的constructor和super知識(shí)點(diǎn)整理
這篇文章主要介紹了react組件中的constructor和super知識(shí)點(diǎn)整理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08