Skip to content

Commit c935163

Browse files
authored
Merge pull request #137 from Coding/zhengxinqi/i18n_03
Zhengxinqi/i18n优化
2 parents e67477c + 3276e26 commit c935163

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1005
-747
lines changed

.i18nGenerator.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
require('dotenv').config()
2+
const fs = require('fs')
3+
const fromJS = require('immutable').fromJS
4+
5+
const root_path = process.argv[2] || 'app'
6+
const i18nTargetRoot = process.env.I18nTargetRoot || 'app/i18n'
7+
8+
const i18nRegex = /(?:i18n`(.+?):=)(.+?)(?:`)/gm
9+
const i18nRegexPer = /(?:i18n`(.+?):=)(.+?)(?:`)/
10+
11+
console.log('search i18n shape in folder : ' + root_path)
12+
13+
const i18nFolders = require(`./${i18nTargetRoot}/index.json`)
14+
console.log('found folders ' + i18nFolders)
15+
16+
17+
// 遍历读取所有的js和jsx文件,当看到有文件内容有特征内容,则替换内容,并且过程中 有一个side effect 在指定的目录生成json文件
18+
19+
function generateI18n(root) {
20+
const files = fs.readdirSync(root);
21+
files.forEach(file => {
22+
var pathname = root+'/'+file
23+
, stat = fs.lstatSync(pathname);
24+
if (stat.isDirectory()) {
25+
generateI18n(pathname)
26+
}
27+
if (['js', 'jsx'].includes(file.split('.').pop())) {
28+
const readablePathname = pathname.replace(root,'.')
29+
const data = fs.readFileSync(pathname, "utf-8")
30+
if (i18nRegex.test(data)) {
31+
const newData = data.replace(i18nRegex, (matchable) => {
32+
console.log(`find i18n shape ${matchable}, generating...`)
33+
return generate(matchable)
34+
})
35+
fs.writeFile(pathname, newData, (err) => {
36+
if (err) throw err
37+
})
38+
}
39+
}
40+
})
41+
}
42+
43+
function generate(matchable) {
44+
const perMatchableArray = matchable.match(i18nRegexPer)
45+
const fileKey = perMatchableArray[1].split('$')[0]
46+
const value = perMatchableArray[2]
47+
const folderArray = fileKey.split('.')
48+
const folder = folderArray.shift()
49+
50+
i18nFolders.forEach(i18nFolder => {
51+
const jsonFileName = `${i18nTargetRoot}/${i18nFolder}/${folder}.json`
52+
const i18nFiles = fs.readdirSync(`${i18nTargetRoot}/${i18nFolder}`);
53+
if (!i18nFiles.includes(`${folder}.json`)) {
54+
console.log(`create a new File ${jsonFileName}`)
55+
fs.writeFileSync(jsonFileName, '{}', "utf-8")
56+
}
57+
const json = fs.readFileSync(jsonFileName, "utf-8")
58+
const tmp = fromJS(JSON.parse(json));
59+
if (!tmp.getIn(folderArray, '')) {
60+
const nextData = tmp.setIn(folderArray, value).toJS()
61+
fs.writeFileSync(jsonFileName, JSON.stringify(nextData, undefined, 3), "utf-8")
62+
console.log(`generate in ${jsonFileName} success`)
63+
} else {
64+
console.log(`${i18nFolder} already has value, skip`)
65+
}
66+
})
67+
return matchable.replace(/:=.+/, '`');
68+
}
69+
70+
generateI18n(root_path)

app/CodingSDK.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { request } from './utils'
22
import store from './store'
3-
import { bindActionCreators } from 'redux'
43
import config from './config'
54
import * as Modal from './components/Modal/actions'
65
import { notify, NOTIFY_TYPE } from './components/Notification/actions'
76
import { addComToSideBar } from './components/Panel/actions'
7+
import { CreateI18n } from 'utils/createI18n'
8+
89

910
export default class {
1011
// app data
1112
constructor (config = {}) {
1213
this.subscribeDataArray = config.subscribeDataArray || []
1314
this.pkgId = config.pkgId || ''
15+
this.i18nConfig = config.i18n
1416
}
1517
getData () {
1618
const currentStore = store.getState()
@@ -34,6 +36,9 @@ export default class {
3436
addComToSideBar
3537
})
3638
}
39+
get i18n () {
40+
return new CreateI18n(this.i18nConfig || {})
41+
}
3742
get config () {
3843
return config
3944
}

app/components/Git/GitBranchWidget.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default class GitBranchWidget extends Component {
3737
{this.state.isActive ?
3838
<div className='git-branch-widget'>
3939
<div className='widget-header'>
40-
<h2>Git Branches</h2>
40+
<h2>{i18n`git.branchWidget.branches`}</h2>
4141
</div>
4242
<Menu className={cx('bottom-up to-left', { active: this.state.isActive })}
4343
style={{

app/components/Git/modals/commitDiff.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { bindActionCreators } from 'redux'
44
import { dispatchCommand } from '../../../commands'
55
import cx from 'classnames'
66
import { connect } from 'react-redux'
7+
import i18n from 'utils/createI18n'
8+
79

810
import * as GitActions from '../actions'
911
import GitFileTree from '../GitFileTree'
@@ -31,9 +33,9 @@ class GitCommitDiffView extends Component {
3133
handleClick={(path) => {
3234
this.handleFileClick(path)
3335
}} />
34-
36+
3537
<div className='modal-ops'>
36-
<button className='btn btn-default' onClick={e => dispatchCommand('modal:dismiss')}>Cancel</button>
38+
<button className='btn btn-default' onClick={e => dispatchCommand('modal:dismiss')}>{i18n`git.cancel`}</button>
3739
</div>
3840
</div>
3941
</div>

app/components/Git/modals/diffFile.jsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const jsdiff = require('diff')
99
// CodeMirror
1010
import CodeMirror from 'codemirror'
1111
require(['diff_match_patch'], (lib) => {
12-
Object.assign(window, lib) //@fixme: diff_match_patch is now exposed into the global ns
12+
Object.assign(window, lib) // @fixme: diff_match_patch is now exposed into the global ns
1313
require(['codemirror/addon/merge/merge.js'])
1414
})
1515
import 'codemirror/addon/merge/merge.css'
@@ -29,49 +29,49 @@ class GitDiffView extends Component {
2929
}
3030

3131
componentWillMount () {
32-
const {path, oldRef, newRef} = this.props.content
32+
const { path, oldRef, newRef } = this.props.content
3333
if (oldRef !== '') {
3434
this.props.gitFileDiff({
35-
path: path,
36-
oldRef: oldRef,
37-
newRef: newRef
38-
}).then(res => {
35+
path,
36+
oldRef,
37+
newRef
38+
}).then((res) => {
3939
this.setState({
4040
isLoading: false,
4141
})
4242
const diffPatch = res.diff
4343
if (diffPatch === '' || diffPatch.split('\n')[3] === '--- /dev/null') {
44-
this.props.gitReadFile({ref: newRef, path: path})
45-
.then(res => {
44+
this.props.gitReadFile({ ref: newRef, path })
45+
.then((res) => {
4646
this.initDiff('', res.content)
4747
})
48-
} else if (oldRef && oldRef !== '~~unstaged~~'){
49-
this.props.gitReadFile({ref: oldRef, path: path})
50-
.then(res => {
51-
let content = res.content
52-
let newContent = jsdiff.applyPatch(content, diffPatch)
48+
} else if (oldRef && oldRef !== '~~unstaged~~') {
49+
this.props.gitReadFile({ ref: oldRef, path })
50+
.then((res) => {
51+
const content = res.content
52+
const newContent = jsdiff.applyPatch(content, diffPatch)
5353
this.initDiff(newContent, content)
5454
})
5555
} else {
56-
this.props.readFile({ path: path })
57-
.then(res => {
58-
let content = res.content
59-
let newContent = jsdiff.applyPatch(content, diffPatch)
56+
this.props.readFile({ path })
57+
.then((res) => {
58+
const content = res.content
59+
const newContent = jsdiff.applyPatch(content, diffPatch)
6060
this.initDiff(newContent, content)
6161
})
6262
}
6363
})
6464
} else {
65-
this.props.gitReadFile({ref: newRef, path: path})
66-
.then(res => {
65+
this.props.gitReadFile({ ref: newRef, path })
66+
.then((res) => {
6767
this.initDiff('', res.content)
6868
})
6969
}
7070
}
7171

7272
render () {
73-
const {theme, content} = this.props
74-
const {path, oldRef, newRef} = this.props.content
73+
const { theme, content } = this.props
74+
const { path, oldRef, newRef } = this.props.content
7575
let loadDiv = ''
7676
if (this.state.isLoading) {
7777
loadDiv = (
@@ -84,28 +84,29 @@ class GitDiffView extends Component {
8484
}
8585
let title = ''
8686
if (oldRef !== '') {
87-
title = `Diff File: ${path} - ${newRef} vs ${oldRef}`
87+
title = i18n`git.diffFileModal.titleWithOldRef${{ path, newRef, oldRef }}`
8888
} else {
89-
title = `Diff File: ${path}`
89+
title = i18n`git.diffFileModal.title${{ path }}`
9090
}
9191
return (
9292
<div>
9393
<div className='git-merge'>
9494
<h1>
95-
{title}
95+
{title}
9696
</h1>
9797
<hr />
9898
<div className='diffModal'>
9999
<div
100100
id='flex-container'
101-
className='diffContainer'>
102-
<div id='cm-merge-view-wrapper' ref={r=>this.editorDOM=r} ></div>
101+
className='diffContainer'
102+
>
103+
<div id='cm-merge-view-wrapper' ref={r => this.editorDOM = r} />
103104
</div>
104105
{ loadDiv }
105106
</div>
106107
<hr />
107108
<div className='modal-ops'>
108-
<button className='btn btn-default' onClick={e => dispatchCommand('modal:dismiss')}>Cancel</button>
109+
<button className='btn btn-default' onClick={e => dispatchCommand('modal:dismiss')}>{i18n`git.cancel`}</button>
109110
</div>
110111
</div>
111112
</div>

app/components/Git/modals/merge.jsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ class GitMergeView extends Component {
2121
return (
2222
<div>
2323
<div className='git-reset-container'>
24-
<h1>Merge Branch</h1>
24+
<h1>{i18n`git.mergeModal.title`}</h1>
2525
<hr />
2626
<form className='form-horizontal'>
2727
<div className='form-group'>
28-
<label className='col-sm-3 control-label'>Current Branch</label>
28+
<label className='col-sm-3 control-label'>{i18n`git.mergeModal.currentBranch`}</label>
2929
<label className='col-sm-9 checkbox-inline'>{currentBranch}</label>
3030
</div>
3131
<div className='form-group'>
32-
<label className='col-sm-3 control-label'>Branch to merge</label>
32+
<label className='col-sm-3 control-label'>{i18n`git.mergeModal.destBranch`}</label>
3333
<label className='col-sm-5' style={{ width: 'auto' }}>
3434
<select className='form-control'
3535
onChange={e => this.setState({ branchToMerge: e.target.value, selectChanged: true })}
3636
value={this.state.branchToMerge}
3737
style={this.state.selectChanged ? null : { color: '#aaa' }}
3838
>
3939
<option selected value='' disabled={this.state.selectChanged}>
40-
-- select a branch --
40+
-- {i18n.get('git.mergeModal.selectBranch')} --
4141
</option>
4242
{allBranches.map(branch => <option key={branch} value={branch}>{branch}</option>)}
4343
</select>
@@ -46,7 +46,7 @@ class GitMergeView extends Component {
4646
</form>
4747
<hr />
4848
<div className='modal-ops'>
49-
<button className='btn btn-default' onClick={e => dispatchCommand('modal:dismiss')}>Cancel</button>
49+
<button className='btn btn-default' onClick={e => dispatchCommand('modal:dismiss')}>{i18n`git.cancel`}</button>
5050
<button className='btn btn-primary'
5151
onClick={e => dispatch(GitActions.mergeBranch(this.state.branchToMerge))}
5252
disabled={!this.state.branchToMerge}

app/components/Git/modals/mergeFile.jsx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import { dispatchCommand } from '../../../commands'
44
import cx from 'classnames'
55
import { connect } from 'react-redux'
66
import * as GitActions from '../actions'
7+
import i18n from 'utils/createI18n'
8+
79

810
// CodeMirror
911
import CodeMirror from 'codemirror'
1012
require(['diff_match_patch'], (lib) => {
11-
Object.assign(window, lib) //@fixme: diff_match_patch is now exposed into the global ns
13+
Object.assign(window, lib) // @fixme: diff_match_patch is now exposed into the global ns
1214
require(['codemirror/addon/merge/merge.js'])
1315
})
1416
import 'codemirror/addon/merge/merge.css'
@@ -20,15 +22,15 @@ class GitMergeView extends Component {
2022
width: '100%',
2123
}
2224

23-
constructor(props) {
25+
constructor (props) {
2426
super(props)
2527
this.state = {
2628
isLoading: true
2729
}
2830
}
2931

3032
componentWillMount () {
31-
this.props.getConflicts({path: this.props.content.path}).then(res => {
33+
this.props.getConflicts({ path: this.props.content.path }).then((res) => {
3234
this.setState({
3335
isLoading: false,
3436
})
@@ -37,7 +39,7 @@ class GitMergeView extends Component {
3739
}
3840

3941
render () {
40-
const {theme, content} = this.props;
42+
const { theme, content } = this.props
4143
let loadDiv = ''
4244
if (this.state.isLoading) {
4345
loadDiv = (
@@ -52,34 +54,35 @@ class GitMergeView extends Component {
5254
<div>
5355
<div className='git-merge'>
5456
<h1>
55-
Merge File: {this.props.content.path}
57+
{i18n`git.mergeFile.title`} : {this.props.content.path}
5658
</h1>
5759
<hr />
5860
<div className='diffModal'>
5961
<div className='mergeTitle'>
6062
<div>
61-
LOCAL
63+
{i18n`git.mergeFile.local`}
6264
</div>
63-
<div className='gutterTitle'></div>
65+
<div className='gutterTitle' />
6466
<div>
65-
BASE
67+
{i18n`git.mergeFile.base`}
6668
</div>
67-
<div className='gutterTitle'></div>
69+
<div className='gutterTitle' />
6870
<div>
69-
REMOTE
71+
{i18n`git.mergeFile.remote`}
7072
</div>
7173
</div>
7274
<div
7375
id='flex-container'
74-
className='mergeContainer'>
75-
<div id='cm-merge-view-wrapper' ref={r=>this.editorDOM=r} ></div>
76+
className='mergeContainer'
77+
>
78+
<div id='cm-merge-view-wrapper' ref={r => this.editorDOM = r} />
7679
</div>
7780
{ loadDiv }
7881
</div>
7982
<hr />
8083
<div className='modal-ops'>
81-
<button className='btn btn-default' onClick={this.handleCancel}>Cancel</button>
82-
<button className='btn btn-primary' onClick={this.handleConfirm}>Confirm</button>
84+
<button className='btn btn-default' onClick={this.handleCancel}>{i18n`git.cancel`}</button>
85+
<button className='btn btn-primary' onClick={this.handleConfirm}>{i18n`git.commit`}</button>
8386
</div>
8487
</div>
8588
</div>

0 commit comments

Comments
 (0)