在?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": "個人設(shè)置", "modifyPassword": "修改密碼", "currentTime": '當(dāng)前時間是 {{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下初始化語言的方法
export function initLangage() { let lang = localStorage.getItem('language') || navigator.language // 獲取瀏覽器的語言環(huán)境,兼容IE的寫法 if (lang) { lang = lang.substr(0, 2).toLowerCase() // 截取前兩位字符,并轉(zhuǎn)化為小寫 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 // 檢測用戶當(dāng)前使用的語言 // 文檔: 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 i18n
3. 在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
到此這篇關(guān)于在 React 中使用 i18next的文章就介紹到這了,更多相關(guān)React 使用 i18next內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react?card?slider實(shí)現(xiàn)滑動卡片教程示例
這篇文章主要為大家介紹了react?card?slider實(shí)現(xiàn)滑動卡片教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09JavaScript的React Web庫的理念剖析及基礎(chǔ)上手指南
這篇文章主要介紹了JavaScript的React Web庫的理念剖析及基礎(chǔ)上手指南,React Web的目的即是把本地的React Native應(yīng)用程序項(xiàng)目變?yōu)閃eb應(yīng)用程序,需要的朋友可以參考下2016-05-05關(guān)于getDerivedStateFromProps填坑記錄
這篇文章主要介紹了關(guān)于getDerivedStateFromProps填坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解
這篇文章主要為大家介紹了阿里低代碼框架lowcode-engine設(shè)置默認(rèn)容器詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02在React項(xiàng)目中實(shí)現(xiàn)一個簡單的錨點(diǎn)目錄定位
錨點(diǎn)目錄定位功能在長頁面和文檔類網(wǎng)站中非常常見,它可以讓用戶快速定位到頁面中的某個章節(jié),本文講給大家介紹一下React項(xiàng)目中如何實(shí)現(xiàn)一個簡單的錨點(diǎn)目錄定位,文中有詳細(xì)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2023-09-09