React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法)
本文使用 React-i18next 庫結(jié)合 React, 介紹如何在 React 中配置使用國際化。
官方地址:i18next | react-i18next
簡介
react-i18next 是基于 i18next 的一款強大的國際化框架,可以用于 react 和 react-native 應(yīng)用;
react-i18next 特點:
- 提供多種組件可以在hoc, hook 和 class 的情況下進(jìn)行國際化操作;
- 基于 i18next 不僅限于react,學(xué)一次就可以用在其它地方;
- 適合服務(wù)器的渲染;
- 有許多插件的支持,比如可以用插件檢測當(dāng)前系統(tǒng)的語言環(huán)境,從服務(wù)器或者文件系統(tǒng)加載翻譯資源;
安裝與使用
安裝
# npm npm install react-i18next i18next --save # 如果需要檢測當(dāng)前瀏覽器的語言或者從服務(wù)器獲取配置資源可以安裝下面依賴 npm install i18next-http-backend i18next-browser-languagedetector --save
準(zhǔn)備新建一個 React 項目,安裝依賴包;
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 實例傳遞給 react-i18next
.use(initReactI18next)
// 初始化 i18next
// 所有配置選項: 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 中使用國際化;
import { useTranslation } from "react-i18next";
function App() {
const { t } = useTranslation();
return (
<div className="App">
{t("title")}
</div>
);
}
export default App;3.在 class 組件中使用國際化;
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);檢測當(dāng)前瀏覽器語言國際化組件
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
// 檢測用戶語言
// 操作文檔: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// 將 i18n 實例傳遞給 react-i18next
.use(initReactI18next)
// 初始化 i18next
// 所有配置選項: 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 配置項里配置 lng: navigator.language, 至此切換瀏覽器語言國際化組件完成;
手動切換國際化語言
// class 組件
const { t, i18n } = this.props;
i18n.changeLanguage("en"); // 手動切換到英文
// Hooks 組件
const { t, i18n } = useTranslation();
i18n.changeLanguage("zh"); // 手動切換到中文總結(jié)
i18next 是一款強大的國際化框架,react-i18next 是基于 i18next 適用于 React 的框架,另外 i18next 還和很多的前端框架可以結(jié)合,所以只需要學(xué)習(xí)一次,學(xué)習(xí)成本低;
本文介紹了 react-i18next 的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案;
到此這篇關(guān)于React 中使用 react-i18next 國際化的文章就介紹到這了,更多相關(guān)React 使用 react-i18next 國際化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

