Skip to content

Commit 22c1291

Browse files
TitanSnowdark-flames
authored andcommitted
i18n: add English translation (#17)
* i18n
1 parent bde8c0c commit 22c1291

File tree

8 files changed

+99
-22
lines changed

8 files changed

+99
-22
lines changed

src/components/Dialog.vue

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div class="mp-dialog-container">
66

77
<div class="mp-dialog-header">
8-
<strong>{{ request.title }}</strong>
8+
<strong>{{ t(request.title) }}</strong>
99
<a class="fa fa-close mp-dialog-close" @click="close()"></a>
1010
</div>
1111
<div class="mp-dialog-body">
@@ -17,8 +17,8 @@
1717

1818
<div class="mp-dialog-footer">
1919
<div>
20-
<button class="mp-dialog-button" @click="close()">取消</button>
21-
<button class="mp-dialog-button" @click="finish()" style="margin-right: 7px">确定</button>
20+
<button class="mp-dialog-button" @click="close()">{{ t('取消') }}</button>
21+
<button class="mp-dialog-button" @click="finish()" style="margin-right: 7px">{{ t('确定') }}</button>
2222
</div>
2323
</div>
2424
</div>
@@ -154,6 +154,7 @@ export default {
154154
this.responseData[request.name] = request.value
155155
}
156156
},
157-
components: DialogComponents
157+
components: DialogComponents,
158+
inject: ['t']
158159
}
159-
</script>
160+
</script>

src/components/MarkdownPalettes.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import EditorDialog from './Dialog.vue'
9494
import { defaultConfig, getConfig } from './DefaultConfig'
9595
import { contentParserFactory } from './ContentParserFactory'
9696
import InjectLnParser from './plugins/InjectLnParser.js'
97+
import { getText } from './i18n'
9798
9899
export default {
99100
name: 'markdown-palettes',
@@ -182,6 +183,7 @@ export default {
182183
this.code = newValue
183184
this.updateCode(newValue)
184185
}
185-
}
186+
},
187+
provide: () => ({ t: getText })
186188
}
187189
</script>

src/components/Toolbar.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<ul id="mp-editor-menu" v-if="toolbarConfig.length > 0">
44
<li v-for="item in toolbarConfig" :class="{'mp-divider':item.name === '|'}" >
55
<span v-if="item.name === '|'">|</span>
6-
<a v-else :title="item.title" @click="handleAction(item.action)" unselectable="on">
6+
<a v-else :title="t(item.title)" @click="handleAction(item.action)" unselectable="on">
77
<i :class="['fa', item.icon]" :name="item.name" unselectable="on">{{ item.content }}</i>
88
</a>
99
</li>
@@ -106,6 +106,7 @@ export default {
106106
this.requestData(action.request)
107107
}
108108
}
109-
}
109+
},
110+
inject: ['t']
110111
}
111-
</script>
112+
</script>

src/components/dialog-input-components/AbstractInputComponent.vue renamed to src/components/dialog-input-components/AbstractInputComponent.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
<script>
21
export default {
32
props: {
43
requestField: {
@@ -16,11 +15,14 @@ export default {
1615
value: this.requestField.default ? this.requestField.default : ''
1716
}
1817
},
18+
computed: {
19+
title () { return this.t(this.request.title) }
20+
},
1921
watch: {
2022
value (newValue) {
2123
this.request.value = newValue
2224
this.$emit('change', this.request)
2325
}
24-
}
26+
},
27+
inject: ['t']
2528
}
26-
</script>

src/components/dialog-input-components/CodeMirror.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="mp-dialog-codemirror">
3-
<label v-if="request.title">{{ request.title }}</label>
3+
<label v-if="title">{{ title }}</label>
44
<div class="mp-dialog-codemirror-editor"></div>
55
<codemirror
66
:value="value"
@@ -32,7 +32,7 @@
3232
</style>
3333

3434
<script>
35-
import abstractInputComponent from './AbstractInputComponent.vue'
35+
import abstractInputComponent from './AbstractInputComponent'
3636
import { codemirror } from 'vue-codemirror-lite'
3737
3838
export default {
@@ -67,4 +67,4 @@ export default {
6767
}
6868
}
6969
}
70-
</script>
70+
</script>

src/components/dialog-input-components/Input.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div id="mp-dialog-input">
3-
<label>{{ request.title }}</label>
3+
<label>{{ title }}</label>
44
<input v-model="value">
55
</div>
66
</template>
@@ -30,10 +30,10 @@
3030
</style>
3131

3232
<script>
33-
import abstractInputComponent from './AbstractInputComponent.vue'
33+
import abstractInputComponent from './AbstractInputComponent'
3434
3535
export default {
3636
name: 'dialog-input',
3737
extends: abstractInputComponent
3838
}
39-
</script>
39+
</script>

src/components/dialog-input-components/Select.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<div id="mp-dialog-select">
3-
<label>{{ request.title }}</label>
3+
<label>{{ title }}</label>
44
<select v-model="value">
5-
<option v-for="option in request.param.options" :value="option.value">{{ option.title }}</option>
5+
<option v-for="option in request.param.options" :value="option.value">{{ t(option.title) }}</option>
66
</select>
77
</div>
88
</template>
@@ -33,10 +33,10 @@
3333
</style>
3434

3535
<script>
36-
import abstractInputComponent from './AbstractInputComponent.vue'
36+
import abstractInputComponent from './AbstractInputComponent'
3737
3838
export default {
3939
name: 'dialog-select',
4040
extends: abstractInputComponent
4141
}
42-
</script>
42+
</script>

src/components/i18n.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import _ from 'lodash'
2+
3+
export const dictionary = new Map([
4+
[/^en/, new Map([
5+
['确定', 'OK'],
6+
['取消', 'Cancel'],
7+
['粗体', 'Bold'],
8+
['插入代码', 'Insert code'],
9+
['语言类型', 'Language'],
10+
['未选择', 'Unselected'],
11+
['全屏', 'Fullscreen'],
12+
[/^(?<level>\d)$/, (text, {groups: {level}}) => `Header ${level}`],
13+
['隐藏', 'Hide'],
14+
['分割线', 'Horizontal rule'],
15+
['插入图片', 'Insert image'],
16+
['图片地址', 'Image URL'],
17+
['图片描述', 'Image title'],
18+
['关于', 'About'],
19+
['斜体', 'Italic'],
20+
['插入链接', 'Insert hyperlink'],
21+
['链接地址', 'Link URL'],
22+
['链接标题', 'Link title'],
23+
['有序列表', 'Ordered list'],
24+
['同步滚动', 'Scroll sync'],
25+
['删除线', 'Strikeout'],
26+
['插入表格', 'Insert table'],
27+
['行数', 'Number of rows'],
28+
['列数', 'Number of columns'],
29+
['对齐方式', 'Alignment'],
30+
['左对齐', 'Flush left'],
31+
['居中', 'Centered'],
32+
['右对齐', 'Flush right'],
33+
['无序列表', 'Unordered list']
34+
])],
35+
[/^zh/, new Map([
36+
[/^(?<level>\d)$/, (text, {groups: {level}}) => `${'一二三四五六'[level - 1]}级标题`]
37+
])]
38+
])
39+
40+
export function getText (text) {
41+
for (const language of navigator.languages) {
42+
for (const [langReg, textMapping] of dictionary) {
43+
if (langReg.test(language)) {
44+
let result
45+
let match = null
46+
if (textMapping.has(text)) {
47+
result = textMapping.get(text)
48+
} else {
49+
for (const [re, res] of textMapping) {
50+
if (_.isRegExp(re)) {
51+
match = re.exec(text)
52+
if (match !== null) {
53+
result = res
54+
break
55+
}
56+
}
57+
}
58+
}
59+
if (typeof result !== 'undefined') {
60+
if (typeof result === 'function') {
61+
result = result(text, match === null ? undefined : match)
62+
}
63+
return result
64+
} else {
65+
return text
66+
}
67+
}
68+
}
69+
}
70+
return text
71+
}

0 commit comments

Comments
 (0)