Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit fdb574b

Browse files
committed
feat: change target language directly in webpage
1 parent 2b287d3 commit fdb574b

File tree

7 files changed

+80
-52
lines changed

7 files changed

+80
-52
lines changed

src/common/constant.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const supportedLanguages = {
2+
ZH: '中文',
3+
'EN-US': 'English (American)',
4+
'EN-GB': 'English (British)',
5+
JA: '日本語',
6+
DE: 'German',
7+
FR: 'French',
8+
ES: 'Spanish',
9+
'PT-PT': 'Portuguese',
10+
'PT-BR': 'Portuguese (Brazilian)',
11+
IT: 'Italian',
12+
NL: 'Dutch',
13+
PL: 'Polish',
14+
RU: 'Russian',
15+
}

src/common/types.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1+
import { supportedLanguages } from './constant'
2+
13
export interface Config {
24
token: string
3-
targetLang: SupportLanguages
5+
targetLang: SupportLanguageKeys
46
region: APIRegions
57
}
68

7-
export type SupportLanguages =
8-
| 'ZH'
9-
| 'EN'
10-
| 'JA'
11-
| 'DE'
12-
| 'FR'
13-
| 'ES'
14-
| 'PT'
15-
| 'IT'
16-
| 'NL'
17-
| 'PL'
18-
| 'RU'
9+
export type SupportLanguageKeys = keyof typeof supportedLanguages
1910

2011
export type APIRegions = 'default' | 'global' | 'dev'
2112

2213
export type TranslateResult = {
2314
translations: Array<{
24-
detected_source_language: SupportLanguages
15+
detected_source_language: SupportLanguageKeys
2516
text: string
2617
}>
2718
}

src/pages/Content/common/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { SupportLanguages } from '../../../common/types'
1+
import { SupportLanguageKeys } from '../../../common/types'
22

33
export interface TextSelection {
44
text: string
55
selection: RangySelection
66
parentElement?: HTMLElement
7-
sourceLang?: SupportLanguages
7+
sourceLang?: SupportLanguageKeys
88
id?: string
99
anchorId?: string
1010
}
@@ -13,5 +13,5 @@ export interface TranslateJob {
1313
id: string
1414
anchorId: string
1515
text: string
16-
sourceLang?: SupportLanguages
16+
sourceLang?: SupportLanguageKeys
1717
}

src/pages/Content/common/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { SupportLanguages } from '../../../common/types'
1+
import { SupportLanguageKeys } from '../../../common/types'
22

33
export const getFirstRange = (sel: RangySelection): RangyRange | undefined => {
44
return sel.rangeCount ? sel.getRangeAt(0) : undefined
55
}
66

7-
export const getDocumentLang = (): SupportLanguages | undefined => {
7+
export const getDocumentLang = (): SupportLanguageKeys | undefined => {
88
const html = document.querySelector('html')
99

1010
if (!html) return

src/pages/Content/components/TranslationItem/index.tsx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { useSnackbar } from 'notistack'
44
import { Collapse } from 'react-collapse'
55
import scrollParent from 'scrollparent'
66
import tw, { css } from 'twin.macro'
7+
import { supportedLanguages } from '../../../../common/constant'
78

89
import logger from '../../../../common/logger'
9-
import { TranslateResult } from '../../../../common/types'
10+
import { SupportLanguageKeys, TranslateResult } from '../../../../common/types'
1011
import IconButton from '../../../../components/IconButton'
1112
import ArrowRight from '../../../../components/svg/ArrowRight'
1213
import ClipboardCopy from '../../../../components/svg/ClipboardCopy'
@@ -23,6 +24,7 @@ const TranslationItem: React.FC<{
2324
const [loading, setLoading] = useState(true)
2425
const [error, setError] = useState<string>()
2526
const [result, setResult] = useState<string[]>()
27+
const [overrideLang, setOverrideLang] = useState<string>()
2628
const [dirty, setDirty] = useState(0)
2729
const [collapse, setCollapse] = useState(true)
2830
const { enqueueSnackbar } = useSnackbar()
@@ -66,12 +68,16 @@ const TranslationItem: React.FC<{
6668
useEffect(() => {
6769
if (!config) return
6870

71+
setResult(undefined)
72+
setLoading(true)
73+
setError(undefined)
74+
6975
const res = client.send(
7076
'translate',
7177
{
7278
text: cleanText(job.text),
7379
id: job.id,
74-
targetLang: config.targetLang,
80+
targetLang: overrideLang || config.targetLang,
7581
},
7682
true,
7783
) as Promise<TranslateResult>
@@ -89,6 +95,7 @@ const TranslationItem: React.FC<{
8995
result.push(...item.text.split('\n'))
9096
})
9197

98+
setError(undefined)
9299
setResult(result)
93100
setLoading(false)
94101
})
@@ -102,7 +109,7 @@ const TranslationItem: React.FC<{
102109
setLoading(false)
103110
enqueueSnackbar(`翻译失败:${err.message}`, { variant: 'error' })
104111
})
105-
}, [job, config, enqueueSnackbar, dirty])
112+
}, [job, config, enqueueSnackbar, dirty, overrideLang])
106113

107114
return (
108115
<div tw="p-3 text-gray-800 space-y-3 select-text">
@@ -131,19 +138,19 @@ const TranslationItem: React.FC<{
131138
</Collapse>
132139
</div>
133140

134-
{loading ? (
135-
<div tw="rounded bg-yellow-50 p-3 space-y-2 leading-normal">
136-
翻译中…
137-
</div>
138-
) : undefined}
141+
<div tw="rounded bg-yellow-50 p-3 space-y-2 leading-normal">
142+
{error ? <span>{error}</span> : undefined}
139143

140-
{result ? (
141-
<div tw="rounded bg-yellow-50 p-3 space-y-2 leading-normal">
142-
{result.map((item, index) => (
143-
<div key={index}>{item}</div>
144-
))}
145-
</div>
146-
) : undefined}
144+
{loading ? <span>{'翻译中…'}</span> : undefined}
145+
146+
{result ? (
147+
<>
148+
{result.map((item, index) => (
149+
<div key={index}>{item}</div>
150+
))}
151+
</>
152+
) : undefined}
153+
</div>
147154
</div>
148155
<div
149156
css={[
@@ -154,12 +161,34 @@ const TranslationItem: React.FC<{
154161
}
155162
`,
156163
]}>
157-
<div>
164+
<div
165+
tw="flex items-center space-x-3"
166+
css={css`
167+
height: 35px;
168+
`}>
158169
{job.sourceLang && (
159-
<div tw="px-2 py-1 bg-green-50 text-green-600 text-sm rounded">
170+
<div tw="px-3 py-0 h-full flex items-center bg-green-50 text-green-600 text-sm rounded">
160171
{job.sourceLang}
161172
</div>
162173
)}
174+
<div tw="h-full">
175+
{config ? (
176+
<select
177+
tw="px-2 py-0 h-full rounded-md truncate border-none bg-blue-50 text-gray-800"
178+
css={css`
179+
width: 100px;
180+
`}
181+
name="target-lang"
182+
value={overrideLang || config.targetLang}
183+
onChange={(e) => setOverrideLang(e.target.value)}>
184+
{Object.keys(supportedLanguages).map((lang, index) => (
185+
<option value={lang} key={index}>
186+
{supportedLanguages[lang as SupportLanguageKeys]}
187+
</option>
188+
))}
189+
</select>
190+
) : undefined}
191+
</div>
163192
</div>
164193
<div>
165194
{result ? (

src/pages/Content/providers/config.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React, { createContext, useContext } from 'react'
22

3-
import { SupportLanguages } from '../../../common/types'
3+
import { SupportLanguageKeys } from '../../../common/types'
44

55
export type ConfigState = {
6-
targetLang: SupportLanguages
6+
targetLang: SupportLanguageKeys
77
}
88

99
export const ConfigContext = createContext<ConfigState | undefined>(undefined)

src/pages/Options/Options.tsx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { Global } from '@emotion/react'
99
import cc from 'chrome-call'
1010

1111
import Client from '../../common/api'
12-
import { APIRegions, Config } from '../../common/types'
12+
import { supportedLanguages } from '../../common/constant'
13+
import { APIRegions, Config, SupportLanguageKeys } from '../../common/types'
1314

1415
import OptionSection from './components/OptionSection'
1516

@@ -96,19 +97,11 @@ const Options: React.FC = () => {
9697
name="target-lang"
9798
value={targetLang}
9899
onChange={(e) => setTargetLang(e.target.value)}>
99-
<option value="ZH">中文</option>
100-
<option value="EN-US">English (American)</option>
101-
<option value="EN-GB">English (British)</option>
102-
<option value="JA">日本語</option>
103-
<option value="DE">German</option>
104-
<option value="FR">French</option>
105-
<option value="ES">Spanish</option>
106-
<option value="PT-PT">Portuguese</option>
107-
<option value="PT-BR">Portuguese (Brazilian)</option>
108-
<option value="IT">Italian</option>
109-
<option value="NL">Dutch</option>
110-
<option value="PL">Polish</option>
111-
<option value="RU">Russian</option>
100+
{Object.keys(supportedLanguages).map((lang, index) => (
101+
<option value={lang} key={index}>
102+
{supportedLanguages[lang as SupportLanguageKeys]}
103+
</option>
104+
))}
112105
</select>
113106
</OptionSection>
114107

0 commit comments

Comments
 (0)