在?React?中使用?i18next的示例
1. 安裝依賴(lài)
npm i i18next react-i18next i18next-browser-languagedetector
- i18next 提供了翻譯的基本能力。
- react-i18next 是 i18next 的一個(gè)插件,用來(lái)降低 react 的使用成本。
- i18next-browser-languagedetector 是用來(lái)檢測(cè)瀏覽器語(yǔ)言的插件。
2. 在src下創(chuàng)建i18n文件夾

2.1 common下的zh-CN.js
{
"common": {
"personSetting": "個(gè)人設(shè)置",
"modifyPassword": "修改密碼",
"currentTime": '當(dāng)前時(shí)間是 {{time}}',
}
}2.2 common下的en-US.js
{
"common": {
"personSetting": "Personal settings",
"modifyPassword": "change Password",
"currentTime": 'Current time is {{time}}',
}
}2.3 在common的index.js文件中引入
import en_common from './en-US/translation.json'
import zh_common from './zh-CN/translation.json'
export const langCommon = { en_common, zh_common }2.4 在resources.js中引入common模塊的翻譯
import { langCommon } from './locales/common' //公共需要翻譯的內(nèi)容
// 把所有的翻譯資源集合
const resources = {
en: {
translation: {
...langCommon.en_common
},
},
zh: {
translation: {
...langCommon.zh_common
},
}
}
export { resources }2.5 utils下初始化語(yǔ)言的方法
export function initLangage() {
let lang = localStorage.getItem('language') || navigator.language // 獲取瀏覽器的語(yǔ)言環(huán)境,兼容IE的寫(xiě)法
if (lang) {
lang = lang.substr(0, 2).toLowerCase() // 截取前兩位字符,并轉(zhuǎn)化為小寫(xiě)
return lang
} else {
return 'en'
}
}2.6 i18n.js代碼如下
import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'
import LanguageDetector from 'i18next-browser-languagedetector';
import { resources } from '@/i18n/resources'
import { initLangage } from '@/utils'
i18n
// 檢測(cè)用戶(hù)當(dāng)前使用的語(yǔ)言
// 文檔: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// 注入 react-i18next 實(shí)例
.use(initReactI18next)
// 初始化 i18next
// 配置參數(shù)的文檔: https://www.i18next.com/overview/configuration-options
.init({
resources, //資源初始化
lng: initLangage(),
interpolation: {
escapeValue: false, // react already safes from xss
},
react: {
useSuspense: false, // this will do the magic
},
debug: false,
})
export default i18n3. 在app.tsx中引入
import './i18n/i18n'
4. 頁(yè)面中使用
import { useTranslation } from 'react-i18next';
const SafetyManage: React.FC = (): React.ReactElement => {
const { t } = useTranslation();
return (
<div >
<Button
type="primary"
>
{t('common.personnalSetting')}
</Button>,
<Button
>
{t('common.modifyPassword')}
</Button>,
<p>
{t('common.currentTime', { time: dayjs().format('MM/DD/YYYY') })}
</p>
</div>
);
}
export default App;useTranslation 返回的對(duì)象包含一個(gè) t 方法,這個(gè)方法可以翻譯文本。
i18next 提供了插值的用法: 在 t 函數(shù)中傳遞第二個(gè)參數(shù),它是一個(gè)對(duì)象。


參考文章:https://www.zadmei.com/qdizjsjz.html
到此這篇關(guān)于在 React 中使用 i18next的文章就介紹到這了,更多相關(guān)React 使用 i18next內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React+Router多級(jí)導(dǎo)航切換路由方式
這篇文章主要介紹了React+Router多級(jí)導(dǎo)航切換路由方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
React Hook useState useEffect componentD
這篇文章主要介紹了React Hook useState useEffect componentDidMount componentDidUpdate componentWillUnmount問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
React+hook實(shí)現(xiàn)聯(lián)動(dòng)模糊搜索
這篇文章主要為大家詳細(xì)介紹了如何利用React+hook+antd實(shí)現(xiàn)聯(lián)動(dòng)模糊搜索功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02
基于React-Dropzone開(kāi)發(fā)上傳組件功能(實(shí)例演示)
這篇文章主要介紹了基于React-Dropzone開(kāi)發(fā)上傳組件,主要講述的是在React-Flask框架上開(kāi)發(fā)上傳組件的技巧,需要的朋友可以參考下2021-08-08
react+react-beautiful-dnd實(shí)現(xiàn)代辦事項(xiàng)思路詳解
這篇文章主要介紹了react+react-beautiful-dnd實(shí)現(xiàn)代辦事項(xiàng),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
React useMemo與useCallabck有什么區(qū)別
useCallback和useMemo是一樣的東西,只是入?yún)⒂兴煌瑄seCallback緩存的是回調(diào)函數(shù),如果依賴(lài)項(xiàng)沒(méi)有更新,就會(huì)使用緩存的回調(diào)函數(shù);useMemo緩存的是回調(diào)函數(shù)的return,如果依賴(lài)項(xiàng)沒(méi)有更新,就會(huì)使用緩存的return2022-12-12

