亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React?中使用?react-i18next?國際化的過程(react-i18next?的基本用法)

 更新時間:2023年01月06日 15:06:17   作者:太輕描淡寫了  
i18next?是一款強(qiáng)大的國際化框架,react-i18next?是基于?i18next?適用于?React?的框架,本文介紹了?react-i18next?的基本用法,如果更特殊的需求,文章開頭的官方地址可以找到答案

本文使用 React-i18next 庫結(jié)合 React, 介紹如何在 React 中配置使用國際化。
官方地址:i18next | react-i18next

簡介

react-i18next 是基于 i18next 的一款強(qiáng)大的國際化框架,可以用于 react 和 react-native 應(yīng)用;
react-i18next 特點(diǎn):

  • 提供多種組件可以在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 項(xiàng)目,安裝依賴包;

npm install react-i18next i18next --save

2.新建文件 en.jsonzh.json;

src\react-i18next\locales\en.json

{
	title: "Hello Word"
}

src\react-i18next\locales\zh.json

{
	title: "你好 世界"
}

3.新建 resources.jsi18n.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 中使用國際化;

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 實(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, 至此切換瀏覽器語言國際化組件完成;

手動切換國際化語言

// class 組件
const { t, i18n } = this.props;
i18n.changeLanguage("en"); 		// 手動切換到英文

// Hooks 組件
const { t, i18n } = useTranslation();
i18n.changeLanguage("zh"); 		// 手動切換到中文

總結(jié)

i18next 是一款強(qiáng)大的國際化框架,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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • React中的refs的使用教程

    React中的refs的使用教程

    本篇文章主要介紹了React中的refs的使用教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • React中state屬性案例詳解

    React中state屬性案例詳解

    在React中,state 是一個用于存儲組件內(nèi)部數(shù)據(jù)的特殊對象,每個React組件都可以包含自己的state,我們往往是通過修改state的值來驅(qū)動React重新渲染組件,這篇文章主要介紹了React中state屬性,需要的朋友可以參考下
    2023-11-11
  • 一文詳解React類組件中的refs屬性

    一文詳解React類組件中的refs屬性

    react中的ref類似于vue中的ref,都是可以操作dom的,這篇文章我們通過一個demo來學(xué)習(xí)這個屬性,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下
    2023-08-08
  • react hooks實(shí)現(xiàn)原理解析

    react hooks實(shí)現(xiàn)原理解析

    這篇文章主要介紹了react hooks實(shí)現(xiàn)原理,文中給大家介紹了useState dispatch 函數(shù)如何與其使用的 Function Component 進(jìn)行綁定,節(jié)后實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-10-10
  • react中ref獲取dom或者組件的實(shí)現(xiàn)方法

    react中ref獲取dom或者組件的實(shí)現(xiàn)方法

    這篇文章主要介紹了react中ref獲取dom或者組件的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 通過示例講解Remix?設(shè)計哲學(xué)理念

    通過示例講解Remix?設(shè)計哲學(xué)理念

    這篇文章主要為大家通過示例講解了Remix?設(shè)計哲學(xué)理念,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • React組件的生命周期詳細(xì)描述

    React組件的生命周期詳細(xì)描述

    本篇文章主要介紹了React組件生命周期,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • React中Redux核心原理深入分析

    React中Redux核心原理深入分析

    這篇文章主要介紹了如何在React中Redux原理,目前redux在react中使用是最多的,所以我們需要將之前編寫的redux代碼,融入到react當(dāng)中去,本文給大家詳細(xì)講解,需要的朋友可以參考下
    2022-11-11
  • React實(shí)踐之Tree組件的使用方法

    React實(shí)踐之Tree組件的使用方法

    本篇文章主要介紹了React實(shí)踐之Tree組件的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • React組件二次包裝的具體實(shí)現(xiàn)

    React組件二次包裝的具體實(shí)現(xiàn)

    本文主要介紹了React組件二次包裝的具體實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02

最新評論