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

2.1 common下的zh-CN.js
{
"common": {
"personSetting": "個人設置",
"modifyPassword": "修改密碼",
"currentTime": '當前時間是 {{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' //公共需要翻譯的內容
// 把所有的翻譯資源集合
const resources = {
en: {
translation: {
...langCommon.en_common
},
},
zh: {
translation: {
...langCommon.zh_common
},
}
}
export { resources }2.5 utils下初始化語言的方法
export function initLangage() {
let lang = localStorage.getItem('language') || navigator.language // 獲取瀏覽器的語言環(huán)境,兼容IE的寫法
if (lang) {
lang = lang.substr(0, 2).toLowerCase() // 截取前兩位字符,并轉化為小寫
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
// 檢測用戶當前使用的語言
// 文檔: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// 注入 react-i18next 實例
.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. 頁面中使用
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 返回的對象包含一個 t 方法,這個方法可以翻譯文本。
i18next 提供了插值的用法: 在 t 函數(shù)中傳遞第二個參數(shù),它是一個對象。


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

