React?中使用?react-i18next?國(guó)際化的過(guò)程(react-i18next?的基本用法)
本文使用 React-i18next
庫(kù)結(jié)合 React, 介紹如何在 React 中配置使用國(guó)際化。
官方地址:i18next | react-i18next
簡(jiǎn)介
react-i18next 是基于 i18next 的一款強(qiáng)大的國(guó)際化框架,可以用于 react 和 react-native 應(yīng)用;
react-i18next 特點(diǎn):
- 提供多種組件可以在hoc, hook 和 class 的情況下進(jìn)行國(guó)際化操作;
- 基于 i18next 不僅限于react,學(xué)一次就可以用在其它地方;
- 適合服務(wù)器的渲染;
- 有許多插件的支持,比如可以用插件檢測(cè)當(dāng)前系統(tǒng)的語(yǔ)言環(huán)境,從服務(wù)器或者文件系統(tǒng)加載翻譯資源;
安裝與使用
安裝
# npm npm install react-i18next i18next --save # 如果需要檢測(cè)當(dāng)前瀏覽器的語(yǔ)言或者從服務(wù)器獲取配置資源可以安裝下面依賴 npm install i18next-http-backend i18next-browser-languagedetector --save
準(zhǔn)備新建一個(gè) React 項(xiàng)目,安裝依賴包;
npm install react-i18next i18next --save
2.新建文件 en.json
和 zh.json
;
src\react-i18next\locales\en.json
{ title: "Hello Word" }
src\react-i18next\locales\zh.json
{ title: "你好 世界" }
3.新建 resources.js
和 i18n.js
;
src\react-i18next\locales\resources.js
import ja from "./ja.json"; import en from "./en.json"; import zh from "./zh.json"; export const resources = { "ja": { translation: ja }, "en": { translation: en }, "zh": { translation: zh } }
src\react-i18next\i18n.js
import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import { resources } from './locales/resources'; i18n // 將 i18n 實(shí)例傳遞給 react-i18next .use(initReactI18next) // 初始化 i18next // 所有配置選項(xiàng): https://www.i18next.com/overview/configuration-options .init({ resources, fallbackLng: "zh", lng: "zh", debug: true, interpolation: { escapeValue: false, // not needed for react as it escapes by default } }); export default i18n;
使用
1.在程序入口引入 i118n;
import React from 'react'; import ReactDOM from 'react-dom/client'; import App from './App'; import "./react-i18next/i18n"; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( <React.StrictMode> <App /> </React.StrictMode> );
2.在 Hooks 中使用國(guó)際化;
import { useTranslation } from "react-i18next"; function App() { const { t } = useTranslation(); return ( <div className="App"> {t("title")} </div> ); } export default App;
3.在 class 組件中使用國(guó)際化;
import React from "react"; import { withTranslation } from "react-i18next"; class App extends React.Component { render() { const { t } = this.props; return (<div className="App"> {t("title")} </div>); } } export default withTranslation()(App);
檢測(cè)當(dāng)前瀏覽器語(yǔ)言國(guó)際化組件
1.安裝依賴
npm install i18next-browser-languagedetector --save
2.配置使用插件
// src\react-i18next\i18n.js import i18n from 'i18next'; import LanguageDetector from 'i18next-browser-languagedetector'; import { initReactI18next } from 'react-i18next'; import { resources } from './locales/resources'; i18n // 檢測(cè)用戶語(yǔ)言 // 操作文檔: https://github.com/i18next/i18next-browser-languageDetector .use(LanguageDetector) // 將 i18n 實(shí)例傳遞給 react-i18next .use(initReactI18next) // 初始化 i18next // 所有配置選項(xiàng): https://www.i18next.com/overview/configuration-options .init({ resources, fallbackLng: "en", lng: navigator.language, debug: true, interpolation: { escapeValue: false, // not needed for react as it escapes by default } }); export default i18n;
上面代碼,首先導(dǎo)入 LanguageDetector,其次 use(LanguageDetector)
, 使用插件,最終在 init 配置項(xiàng)里配置 lng: navigator.language
, 至此切換瀏覽器語(yǔ)言國(guó)際化組件完成;
手動(dòng)切換國(guó)際化語(yǔ)言
// class 組件 const { t, i18n } = this.props; i18n.changeLanguage("en"); // 手動(dòng)切換到英文 // Hooks 組件 const { t, i18n } = useTranslation(); i18n.changeLanguage("zh"); // 手動(dòng)切換到中文
總結(jié)
i18next 是一款強(qiáng)大的國(guó)際化框架,react-i18next 是基于 i18next 適用于 React 的框架,另外 i18next 還和很多的前端框架可以結(jié)合,所以只需要學(xué)習(xí)一次,學(xué)習(xí)成本低;
本文介紹了 react-i18next 的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案;
到此這篇關(guān)于React 中使用 react-i18next 國(guó)際化的文章就介紹到這了,更多相關(guān)React 使用 react-i18next 國(guó)際化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react中ref獲取dom或者組件的實(shí)現(xiàn)方法
這篇文章主要介紹了react中ref獲取dom或者組件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05通過(guò)示例講解Remix?設(shè)計(jì)哲學(xué)理念
這篇文章主要為大家通過(guò)示例講解了Remix?設(shè)計(jì)哲學(xué)理念,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03