react國際化化插件react-i18n-auto使用詳解
react-i18n-auto專門為中文國際化提供的自動化方案,快速迭代國際化開發(fā),方法如下
安裝
npm install react-i18n-auto --save-dev
第一步:添加babel插件配置(.babelrc添加方式)
{
"plugins": [
"@babel/plugin-transform-runtime",
"react-i18n-auto",
"..."
]
}
第二步:添加自動化配置 i18n.config.js
const generator = require('react-i18n-auto/generator')
const path = require('path')
generator.gen({
excluded: /node_modules|output/, //排除文件選項(默認為:/node_modules/)
src: path.resolve(__dirname, './code'), //源文件目錄(必選)
outputPath: path.resolve(__dirname, './output'), //國際化配置輸出目錄(必選)
})
然后運行 node i18n.config.js 自動生成配置文件,將localePolyfill.js,localeUtils.js,語言包文件自動生成到outputPath目錄下
localePolyfill.js暴露全局方法 $AI, $$AI 和全局變量 LOCALE (語言包),LOCALE_VERSION (語言包版本)
更多配置請移步至react-i18n-auto github主頁
第三步:修改webpack配置,為每一個entry入口添加localePolyfill.js
// webpack.config.js
const path = require('path')
module.exports = {
entry: {
main: [
path.resolve(__dirname, './output/localePolyfill.js'),
path.resolve(__dirname, './src/index.js')
],
...
},
第四步:修改當(dāng)前語言(中文無需加載語言包)
import React from 'react'
import en_US from '../output/en_US/locale'
import localeUtils from '../output/localeUtils'
localeUtils.locale(en_US)
// lolale.js
module.exports = {
'I_2gaaanh': 'Student',
'I_2aq02r1': 'Teacher'
}
第五步:唯一的額外的工作,動態(tài)加載語言包時(如果語言包已提前加載則無需此操作)
修改前
// const.js
export default Const = {
SelectOptions:[
{
name:'學(xué)生',
value:'student',
},
{
name:'教師',
value:'teacher',
},
]
}
// app.js
import React from 'react'
import Const from './const'
export default class App extends React.Component {
render () {
return <select>
{
Const.selectOptions.map(item => {
return <option key={item.value} value={item.value}>
{item.name}
</option>
})
}
</select>
}
}
由于const為常量,當(dāng)語言包LOCALE更新時,const并不會得到更新,需要手動調(diào)用$AI,類似的情況都需要手動更新
修改后
import React from 'react'
import Const from './const'
export default class App extends React.Component {
render () {
return <select>
{
Const.selectOptions.map(item => {
return <option key={item.value} value={item.value}>
{$AI(item.$_name, item.name)} {/*唯一需要修改的地方*/}
</option>
})
}
</select>
}
}
// 編譯后的const.js
// 所有的中文對應(yīng)的字段,自動添加$_前綴,值為對應(yīng)中文的uuidKey
export default Const = {
selectOptions: [{
name: '學(xué)生',
$_name: "I_2gaaanh",
value: 'student'
}, {
name: '教師',
$_name: "I_2aq02r1",
value: 'teacher'
}]
};
ps :通過代理getter,或提前加載語言包則可跳過步驟5,具體方法可自行嘗試
結(jié)束
編譯前后代碼對照,不污染源碼,無痕開發(fā)
import React from 'react'
export default class App extends React.Component {
render () {
return <div>
<header>這是標(biāo)題</header>
<div title='這是提示文字'>
<p>這是內(nèi)容</p>
</div>
<footer>{this.state.title}</footer>
</div>
}
}
import React from 'react'
export default class App extends React.Component {
render () {
return <div>
<header>{$AI('I_5wtgbv1', '這是標(biāo)題')}</header>
<div title={$AI('I_7reuhi4', '這是提示文字')}>
<p>{$AI('I_4ximl4b', '這是內(nèi)容')}</p>
</div>
<footer>{this.state.title}</footer>
</div>
}
}
到此這篇關(guān)于react國際化化插件react-i18n-auto使用詳解的文章就介紹到這了,更多相關(guān)react i18n auto 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React-Native中禁用Navigator手勢返回的示例代碼
本篇文章主要介紹了React-Native中禁用Navigator手勢返回的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
React中使用ResizeObserver來觀察元素size變化的方法
在 React 中使用 ResizeObserver 來觀察元素的大小變化,可以通過創(chuàng)建一個自定義 Hook 來封裝 ResizeObserver 的邏輯,并在組件中使用這個 Hook,以下是一個完整的示例,展示了如何在 React 中使用 ResizeObserver 來觀察元素的大小變化,需要的朋友可以參考下2024-12-12
使用react-activation實現(xiàn)keepAlive支持返回傳參
本文主要介紹了使用react-activation實現(xiàn)keepAlive支持返回傳參,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
React中g(shù)etDefaultProps的使用小結(jié)
React中的getDefaultProps功能允許開發(fā)者為類組件定義默認屬性,提高組件的靈活性和容錯性,本文介紹了getDefaultProps的作用、語法以及最佳實踐,并探討了其他替代方案,如函數(shù)組件中的默認參數(shù)、高階組件和ContextAPI等,理解這些概念有助于提升代碼的可維護性和用戶體驗2024-09-09
D3.js(v3)+react 實現(xiàn)帶坐標(biāo)與比例尺的柱形圖 (V3版本)
這篇文章主要介紹了D3.js(v3)+react 制作 一個帶坐標(biāo)與比例尺的柱形圖 (V3版本) ,本文通過實例代碼文字相結(jié)合的形式給大家介紹的非常詳細,需要的朋友可以參考下2019-05-05
用React實現(xiàn)一個類 chatGPT 的交互式問答組件的方法詳解
這篇文章主要給大家詳細介紹如何用React實現(xiàn)一個類 chatGPT 的交互式問答組件的方法,文中有詳細的代碼示例,對我們學(xué)習(xí)有一定的幫助,需要的朋友可以參考下2023-06-06
VSCode 配置React Native開發(fā)環(huán)境的方法
本篇文章主要介紹了VSCode 配置React Native開發(fā)環(huán)境的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

