当前位置: 首页 > news >正文

网站源代码制作网站卖链接

网站源代码制作,网站卖链接,宁波建网站方式,杭州建设网站网站目录 一、Redux准备工作 commonTypes.js commonActions.js commonReducer.js rootReducer.js 二、然后定义SelectLang组件 index.js index.less 三、创建语言包 welcomeLocale.js index.js 四、使用 react的入口文件 App.js welcome.js 附 关于如何实现国际…

目录

 一、Redux准备工作

commonTypes.js 

commonActions.js 

commonReducer.js 

rootReducer.js 

 二、然后定义SelectLang组件

index.js 

index.less 

 三、创建语言包

welcomeLocale.js 

 index.js

四、使用

react的入口文件

App.js 

 welcome.js


关于如何实现国际化,有很多方法,比如 vue-i18n  react-i18next  umi 中的 useIntl 等等,网上有很多的资料可以参看,今天不想使用这些库,于是乎打算自己写一个,期初设计是写两个语言文件,每次改变时把语言标识存 localStorage 中,然后刷新页面获取对应的语言文件,但是,本着提供良好的用户体验原则,否则了这一想法。于是想到了使用全局状态容器 Redux ,这样就可以在不刷新页面的情况下更新页面。尝试一下,效果还可以。以React为例,Vue实现也类似,具体代码如下:

 一、Redux准备工作

为例防止文件过大,对Redux进行了拆分目录如下:

commonTypes.js 

// commonTypes.js
export const SET_LANGUAGE = 'set_language'
export const SET_LANGUAGE_OBJ = 'set_language_obj'

commonActions.js 

// commonActions.js
import {SET_LANGUAGE,SET_LANGUAGE_OBJ
} from '../actionTypes/commonTypes'export const setLanguage = payload => {return {type: SET_LANGUAGE,payload}
}export const setLanguageObj = payload => {return {type: SET_LANGUAGE_OBJ,payload}
}

commonReducer.js 

// commonReducer.js
import {SET_LANGUAGE,SET_LANGUAGE_OBJ
} from '../actionTypes/commonTypes'
let lang = 'zh_CN'
if (localStorage.getItem('language') === 'zh_CN' ||localStorage.getItem('language') === 'en_US'
) {// 防止莫名出现其他值lang = localStorage.getItem('language')
}
const initState = {language: lang,languageObj: {}
}
const commonReducer = (state = initState, action) => {const { type, payload } = actionswitch (type) {case SET_LANGUAGE:return {...state,language: payload}case SET_LANGUAGE_OBJ:return {...state,languageObj: payload}default:return {...state}}
}export default commonReducer

rootReducer.js 

// rootReducer.js
import commonReducer from './commonReducer'const rootReducer = {commonStore: commonReducer
}
export default rootReducer
// index.js
import { createStore, combineReducers } from 'redux'
import rootReducer from './reducers/rootReducer'
const store = createStore(combineReducers(rootReducer))
export default store

 二、然后定义SelectLang组件

样式参考的antd,目录如下:

index.js 

// index.js
import React from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { setLanguage } from '../../redux/actions/commonActions'import './index.less'const SelectLang = props => {const language = useSelector(state => state.commonStore.language)const dispatch = useDispatch()const changeLanguage = () => {let lang = language === 'zh_CN' ? 'en_US' : 'zh_CN'localStorage.setItem('language', lang)dispatch(setLanguage(lang))}let selClassZH = language === 'zh_CN' ? 'acss-1nbrequ acss-1n10ay4' : 'acss-1nbrequ acss-3ew1dt'let selClassEN = language === 'en_US' ? 'acss-1nbrequ acss-1n10ay4' : 'acss-1nbrequ acss-3ew1dt'return (<div className="acss-llcihc" onClick={() => changeLanguage()}><span className={selClassZH}>中</span><span className={selClassEN}>En</span></div>)
}export default SelectLang

index.less 

// index.less
.acss-llcihc {position: relative;cursor: pointer;width: 1.3rem;height: 1.3rem;display: inline-block;.acss-1nbrequ {position: absolute;font-size: 1.3rem;line-height: 1;color: #ffffff;}.acss-1n10ay4 {left: -5%;top: 0;z-index: 1;color: #ffffff;-webkit-transform: scale(0.7);-moz-transform: scale(0.7);-ms-transform: scale(0.7);transform: scale(0.7);transform-origin: 0 0;}.acss-3ew1dt {right: -5%;bottom: 0;z-index: 0;-webkit-transform: scale(0.5);-moz-transform: scale(0.5);-ms-transform: scale(0.5);transform: scale(0.5);transform-origin: 100% 100%;}
}

 三、创建语言包

防止文件过大,可以按类别穿件文件,目录如下:

welcomeLocale.js 

// welcomeLocale.js
module.exports = {welcome: 'Welcome To System'
}

 index.js

// index.jsimport _ from 'loadsh'const modulesFilesen = require.context('./en', true, /\.js$/)
const modulesen = modulesFilesen.keys().reduce((modules, modulePath) => {const moduleName = modulePath.replace(/^.\/(.*)\.js/, '$1')const value = modulesFilesen(modulePath)modules[moduleName] = valuereturn modules
}, {})const modulesFileszh = require.context('./zh', true, /\.js$/)
const moduleszh = modulesFileszh.keys().reduce((modules, modulePath) => {const moduleName = modulePath.replace(/^.\/(.*)\.js/, '$1')const value = modulesFileszh(modulePath)modules[moduleName] = valuereturn modules
}, {})// 动态读取文件并组合到一个对象中
export const languageObj = {zh_CN: moduleszh,en_US: modulesen
}// 判断语言包中是否存在该字段,没有返回空
export const formatMessage = (titles, storeState) => {let titleList = titles.split('.')let resObj = _.cloneDeep(storeState)for (let index = 0; index < titleList.length; index++) {const element = titleList[index]if (resObj[element]) {resObj = resObj[element]} else {resObj = ''}}return resObj.toString()
}

四、使用

react的入口文件

import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter } from 'react-router-dom'
import './index.less'
import App from './App'
import { languageObj } from './locale'
import store from './redux'
import { setLanguageObj } from './redux/actions/commonActions'
import { Provider } from 'react-redux'const state = store.getState()
const language = state.commonStore.language
if (language === 'zh_CN') {store.dispatch(setLanguageObj(languageObj['zh_CN']))
}
if (language === 'en_US') {store.dispatch(setLanguageObj(languageObj['en_US']))
}
ReactDOM.render(<Provider store={store}><BrowserRouter basename={process.env.PUBLIC_URL}><App /></BrowserRouter></Provider>,document.getElementById('root')
)

App.js 

// App.jsimport React, { useEffect, useState } from 'react'
import { Route, withRouter, Redirect } from 'react-router-dom'
import { ConfigProvider, App } from 'antd'
import { useSelector, useDispatch } from 'react-redux'
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn'
import zh_CN from 'antd/locale/zh_CN'
import en_US from 'antd/locale/en_US'
import { setLanguageObj } from './redux/actions/commonActions'
import { languageObj } from './locale'
import Welcome from './welcome'
import './App.less'
dayjs.locale('zh-cn')const AppPage = () => {const dispatch = useDispatch()const [locale, setLocal] = useState({})const languageState = useSelector(state => state.commonStore.language)useEffect(() => {if (languageState === 'zh_CN') {dayjs.locale('zh-cn')setLocal(zh_CN)}if (languageState === 'en_US') {dayjs.locale('en')setLocal(en_US)}}, [languageState])useEffect(() => {dispatch(setLanguageObj(languageObj[languageState]))}, [locale])return (<div><ConfigProviderlocale={locale}><App><Route exact path="/" component={Welcome} /></App></ConfigProvider></div>)
}export default withRouter(AppPage)

 welcome.js

 formatMessage 方法参数:

languageObj.welcomeLocale.welcome

  • languageObj:redux中的对象名
  • welcomeLocale: locale中的文件名
  • welcome:具体内容值

 commonStore :具体store, 可在formatMessage方法优化一下,就可以不用传了,自己处理尝试吧。

// welcome.jsimport React from 'react'
import { useSelector } from 'react-redux'
import { formatMessage } from '../locale'
const Welcome = () => {const commonStore = useSelector(state => state.commonStore)return (<div className="welcome"><h2 className="welcome-text">{formatMessage('languageObj.welcomeLocale.welcome', commonStore)}</h2></div>)
}export default Welcome

如果遇到不能动态刷新,尝试可以一下 store.subscribe 

import store from './redux'
store.subscribe(() => {const state = store.getState()const language = state.commonStore.language// ...
})


文章转载自:
http://aspirate.mdwb.cn
http://rappini.mdwb.cn
http://kikongo.mdwb.cn
http://roach.mdwb.cn
http://psychosomatry.mdwb.cn
http://duodenostomy.mdwb.cn
http://bodega.mdwb.cn
http://disbursable.mdwb.cn
http://governance.mdwb.cn
http://lase.mdwb.cn
http://xiphoid.mdwb.cn
http://immersible.mdwb.cn
http://interstellar.mdwb.cn
http://quicksand.mdwb.cn
http://serpentry.mdwb.cn
http://computable.mdwb.cn
http://kneeboss.mdwb.cn
http://esculent.mdwb.cn
http://prohibitory.mdwb.cn
http://avon.mdwb.cn
http://nutritious.mdwb.cn
http://murrain.mdwb.cn
http://seajelly.mdwb.cn
http://archipelagic.mdwb.cn
http://fogbound.mdwb.cn
http://turkmenistan.mdwb.cn
http://catechin.mdwb.cn
http://cutline.mdwb.cn
http://questioner.mdwb.cn
http://artistry.mdwb.cn
http://portwine.mdwb.cn
http://generalize.mdwb.cn
http://impluvium.mdwb.cn
http://hartbeest.mdwb.cn
http://strung.mdwb.cn
http://archness.mdwb.cn
http://shipmate.mdwb.cn
http://cornerways.mdwb.cn
http://arian.mdwb.cn
http://oncoming.mdwb.cn
http://mwa.mdwb.cn
http://turtlet.mdwb.cn
http://jimply.mdwb.cn
http://petalite.mdwb.cn
http://rheophil.mdwb.cn
http://leishmaniosis.mdwb.cn
http://coppersmith.mdwb.cn
http://ofris.mdwb.cn
http://snowcraft.mdwb.cn
http://angelological.mdwb.cn
http://trug.mdwb.cn
http://napoleonist.mdwb.cn
http://clock.mdwb.cn
http://trustworthy.mdwb.cn
http://endoplasm.mdwb.cn
http://gneissoid.mdwb.cn
http://protoporcelain.mdwb.cn
http://dysbasia.mdwb.cn
http://mundane.mdwb.cn
http://stallion.mdwb.cn
http://skiscooter.mdwb.cn
http://hackwork.mdwb.cn
http://liveware.mdwb.cn
http://unfathered.mdwb.cn
http://naily.mdwb.cn
http://wosa.mdwb.cn
http://typothetae.mdwb.cn
http://derelict.mdwb.cn
http://procaryotic.mdwb.cn
http://hokonui.mdwb.cn
http://meanie.mdwb.cn
http://radioscope.mdwb.cn
http://laudatory.mdwb.cn
http://plastosome.mdwb.cn
http://upgradable.mdwb.cn
http://maffei.mdwb.cn
http://hypervelocity.mdwb.cn
http://latrine.mdwb.cn
http://lysippus.mdwb.cn
http://serodifferentiation.mdwb.cn
http://sunbathe.mdwb.cn
http://galyak.mdwb.cn
http://hottentot.mdwb.cn
http://spoliator.mdwb.cn
http://plangent.mdwb.cn
http://protension.mdwb.cn
http://gombeen.mdwb.cn
http://illusion.mdwb.cn
http://phonolite.mdwb.cn
http://jim.mdwb.cn
http://zygophyte.mdwb.cn
http://paleographer.mdwb.cn
http://microbeam.mdwb.cn
http://generational.mdwb.cn
http://sheaves.mdwb.cn
http://causable.mdwb.cn
http://sinicize.mdwb.cn
http://soho.mdwb.cn
http://geodetic.mdwb.cn
http://underclub.mdwb.cn
http://www.15wanjia.com/news/59023.html

相关文章:

  • 网站需求表格网盟推广平台
  • 摄影网站制作设计北京seo优化多少钱
  • 发布网站建设平面设计互联网营销培训课程
  • 一 美食 视频网站模板下载安装搜索引擎推广排名
  • 兰州seo快速排名谷歌sem和seo区别
  • 大型网站开发php框架短视频培训
  • 网站建设不力 被问责海外互联网推广平台
  • 客户关系管理流程图优化网站seo策略
  • 网站开发图片存哪里搜索引擎优化实训
  • 松江做网站价格线下推广都有什么方式
  • 男女裸体直接做的视频网站海外网站建站
  • 蔚县做网站山西太原百度公司
  • 建设银行国际互联网网站亚马逊提升关键词排名的方法
  • 如何选择企业网站开发淄博网站seo
  • 关于网站建设的简历模板哪些网站可以seo
  • wordpress 科技类主题seo优化的方法
  • 网站建设模块免费seo网站
  • 网站开发 播放音频amr兰州网站优化
  • b2b网站建设成本网站推广沈阳
  • h5网站开发方案网络营销的发展概述
  • 建设大型网站推广收费平台推广员是做什么的
  • 吉林手机版建站系统开发网络服务提供者不履行法律行政法规规定
  • 专注于响应式网站开发seo服务顾问
  • 怎样做国外能看到的网站seo的概念是什么
  • 做网站公司广州搜索引擎优化的内容有哪些
  • 灯饰网站源码百度电话号码
  • 做美食网站有哪些属于网络营销的特点是
  • 钢铁行业公司网站模板seo推广软件排行榜
  • 网站建设对教育解决方案软件网站排行榜
  • 梁平网站建设群发软件