Skip to content

Commit a2d339f

Browse files
committed
Bugfix: zh_CN broken
1 parent fa2bae1 commit a2d339f

File tree

5 files changed

+61
-27
lines changed

5 files changed

+61
-27
lines changed

src/util/i18n/chrome/compile.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Genearate the messages used by Chrome
2+
function compile(obj: any, parentKey = ''): any {
3+
const result = {}
4+
if (typeof obj === 'object') {
5+
for (const key in obj) {
6+
const val = obj[key]
7+
// key of Chrome message
8+
const messageKey = !!parentKey ? `${parentKey}_${key}` : key
9+
const children = compile(val, messageKey)
10+
// copy from child
11+
for (const childKey in children) {
12+
result[childKey] = children[childKey]
13+
}
14+
}
15+
} else {
16+
result[parentKey] = {
17+
message: obj + '',
18+
description: 'None'
19+
}
20+
}
21+
return result
22+
}
23+
24+
25+
export default compile

src/util/i18n/chrome/index.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,11 @@
11
import { Locale } from ".."
2+
import compile from "./compile"
23
import messages from "./message"
34

4-
// Genearate the messages used by Chrome
5-
function translateForChrome(obj: any, parentKey = ''): any {
6-
const result = {}
7-
if (typeof obj === 'object') {
8-
for (const key in obj) {
9-
const val = obj[key]
10-
// key of Chrome message
11-
const messageKey = !!parentKey ? `${parentKey}_${key}` : key
12-
const children = translateForChrome(val, messageKey)
13-
// copy from child
14-
for (const childKey in children) {
15-
result[childKey] = children[childKey]
16-
}
17-
}
18-
} else {
19-
result[parentKey] = {
20-
message: obj + '',
21-
description: 'None'
22-
}
23-
}
24-
return result
25-
}
26-
275
const _default: { [locale in Locale]: any } = {
28-
zh_CN: translateForChrome(messages.zh_CN),
29-
en: translateForChrome(messages.en),
30-
ja: translateForChrome(messages.ja)
6+
zh_CN: compile(messages.zh_CN),
7+
en: compile(messages.en),
8+
ja: compile(messages.ja)
319
}
3210

3311
export default _default

src/util/i18n/chrome/message.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ const messages: Messages<ChromeMessage> = {
2929

3030
export default messages
3131

32+
const placeholder: ChromeMessage = {
33+
app: {
34+
name: '',
35+
description: '',
36+
marketName: '',
37+
currentVersion: ''
38+
},
39+
message: {
40+
openTimesConsoleLog: '',
41+
usedTimeInConsoleLog: '',
42+
timeWithHour: '',
43+
timeWithMinute: '',
44+
timeWithSecond: ''
45+
},
46+
contextMenus: {
47+
add2Whitelist: '',
48+
removeFromWhitelist: ''
49+
}
50+
}
51+
3252
function routerPath(root: any, parentPath = undefined) {
3353
Object.entries(root)
3454
.forEach(([key, value]) => {
@@ -42,4 +62,4 @@ function routerPath(root: any, parentPath = undefined) {
4262
return root
4363
}
4464

45-
export const router: ChromeMessage = routerPath(messages.zh_CN) as unknown as ChromeMessage
65+
export const router: ChromeMessage = routerPath(placeholder) as unknown as ChromeMessage

test/util/chrome/compile.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import compile from "../../../src/util/i18n/chrome/compile"
2+
3+
test('1', () => {
4+
const messages = { app: '123', foo: { bar: '234' } }
5+
const chromeMessages = compile(messages)
6+
console.log(chromeMessages)
7+
expect(chromeMessages.app.message).toEqual('123')
8+
expect(chromeMessages.foo_bar.message).toEqual('234')
9+
})

webpack/webpack.common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import webpack from 'webpack'
66
import manifest from '../src/manifest'
77
import i18nChrome from '../src/util/i18n/chrome'
88

9+
console.log(i18nChrome)
10+
911
const generateJsonPlugins = [
1012
new GenerateJsonPlugin('manifest.json', manifest) as unknown as webpack.WebpackPluginInstance
1113
]

0 commit comments

Comments
 (0)