Skip to content

Commit 8a5f92d

Browse files
authored
refactor: translation language -> action language (#3154)
* refactor: action language -> action language * type * mobile * type * fix * prefer local settings, drop backward compatibility * update * update * update * chore: auto-fix linting and formatting issues * update * update * update * follow ui * update * add description * fix build * update * update * update
1 parent 6f5f9e8 commit 8a5f92d

File tree

30 files changed

+240
-225
lines changed

30 files changed

+240
-225
lines changed

apps/desktop/locales/settings/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@
187187
"general.startup_screen.timeline": "Timeline",
188188
"general.startup_screen.title": "Startup Screen",
189189
"general.timeline": "Timeline",
190-
"general.translation_language": "Translation Language",
190+
"general.action_language.label": "Action Language",
191+
"general.action_language.default": "Default (UI Language)",
192+
"general.action_language.description": "The language used for translation and summary",
191193
"general.unread": "Unread",
192194
"general.voices": "Voices",
193195
"integration.eagle.enable.description": "Display 'Save media to Eagle' button when available.",

apps/desktop/locales/settings/ja.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@
167167
"general.startup_screen.timeline": "タイムライン",
168168
"general.startup_screen.title": "スタートアップスクリーン",
169169
"general.timeline": "タイムライン",
170-
"general.translation_language": "翻訳する言語",
171170
"general.unread": "未読",
172171
"general.voices": "音声",
173172
"integration.eagle.enable.description": "利用可能な場合、'Eagle にメディアを保存' ボタンを表示します。",

apps/desktop/locales/settings/ko.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@
172172
"general.startup_screen.timeline": "타임라인",
173173
"general.startup_screen.title": "시작 화면",
174174
"general.timeline": "타임라인",
175-
"general.translation_language": "번역 언어",
176175
"general.unread": "읽지 않음",
177176
"general.voices": "음성",
178177
"integration.eagle.enable.description": "가능할 때 '미디어를 Eagle 에 저장' 버튼 표시.",

apps/desktop/locales/settings/zh-CN.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@
187187
"general.startup_screen.timeline": "时间线",
188188
"general.startup_screen.title": "起始界面",
189189
"general.timeline": "时间线",
190-
"general.translation_language": "翻译语言",
191190
"general.unread": "未读",
192191
"general.voices": "声音",
193192
"integration.eagle.enable.description": "显示「保存到 Eagle」按钮(如果可用)。",

apps/desktop/locales/settings/zh-HK.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@
187187
"general.startup_screen.timeline": "時間軸",
188188
"general.startup_screen.title": "啟動畫面",
189189
"general.timeline": "時間軸",
190-
"general.translation_language": "翻譯語言",
191190
"general.unread": "未讀",
192191
"general.voices": "語音",
193192
"integration.eagle.enable.description": "顯示「將媒體儲存到 Eagle」按鈕(如果可用)",

apps/desktop/locales/settings/zh-TW.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@
172172
"general.startup_screen.timeline": "時間軸",
173173
"general.startup_screen.title": "開始畫面",
174174
"general.timeline": "時間軸",
175-
"general.translation_language": "翻譯語言",
176175
"general.unread": "未讀",
177176
"general.voices": "聲音",
178177
"integration.eagle.enable.description": "顯示「將媒體儲存到 Eagle」按鈕(如果可用)。",

apps/desktop/src/renderer/src/atoms/settings/general.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { createSettingAtom } from "@follow/atoms/helper/setting.js"
2+
import type { SupportedLanguages } from "@follow/models"
23
import type { GeneralSettings } from "@follow/shared/interface/settings"
34

45
import { jotaiStore } from "~/lib/jotai"
56

7+
export const DEFAULT_ACTION_LANGUAGE = "default"
8+
69
const createDefaultSettings = (): GeneralSettings => ({
710
// App
811
appLaunchOnStartup: false,
912
language: "en",
10-
translationLanguage: "zh-CN",
13+
actionLanguage: DEFAULT_ACTION_LANGUAGE,
1114

1215
// mobile app
1316
startupScreen: "timeline",
@@ -47,6 +50,14 @@ export const {
4750
settingAtom: __generalSettingAtom,
4851
} = createSettingAtom("general", createDefaultSettings)
4952

53+
export function useActionLanguage() {
54+
const actionLanguage = useGeneralSettingSelector((s) => s.actionLanguage)
55+
const language = useGeneralSettingSelector((s) => s.language)
56+
return (
57+
actionLanguage === DEFAULT_ACTION_LANGUAGE ? language : actionLanguage
58+
) as SupportedLanguages
59+
}
60+
5061
export const subscribeShouldUseIndexedDB = (callback: (value: boolean) => void) =>
5162
jotaiStore.sub(__generalSettingAtom, () => callback(getGeneralSettings().dataPersist))
5263

apps/desktop/src/renderer/src/lib/immersive-translate.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ export function immersiveTranslate({
3636
return
3737
}
3838

39-
const translation = entry.settings?.translation ?? targetLanguage
40-
4139
const immersiveTranslateMark = html.querySelectorAll("[data-immersive-translate-mark=true]")
4240
if (immersiveTranslateMark.length > 0) {
43-
if (translation) {
41+
if (targetLanguage) {
4442
return
4543
}
4644

@@ -49,7 +47,7 @@ export function immersiveTranslate({
4947
}
5048
}
5149

52-
if (!translation) {
50+
if (!targetLanguage) {
5351
return
5452
}
5553

@@ -61,7 +59,7 @@ export function immersiveTranslate({
6159

6260
translate({
6361
entry,
64-
language: translation,
62+
language: targetLanguage,
6563
part: textNode.textContent,
6664
extraFields: ["content"],
6765
}).then((transformed) => {
@@ -96,7 +94,7 @@ export function immersiveTranslate({
9694
if (tag.textContent) {
9795
const isLanguageMatch = checkLanguage({
9896
content: tag.textContent,
99-
language: translation,
97+
language: targetLanguage,
10098
})
10199
if (isLanguageMatch) {
102100
continue
@@ -151,7 +149,7 @@ export function immersiveTranslate({
151149

152150
translate({
153151
entry,
154-
language: translation,
152+
language: targetLanguage,
155153
part: textContent,
156154
extraFields: ["content"],
157155
}).then((transformed) => {

apps/desktop/src/renderer/src/lib/translate.ts

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
import { parseHtml } from "@follow/components/ui/markdown/parse-html.js"
22
import { views } from "@follow/constants"
33
import type { SupportedLanguages } from "@follow/models/types"
4+
import { LANGUAGE_MAP } from "@follow/shared"
45
import { franc } from "franc-min"
56

67
import type { FlatEntryModel } from "~/store/entry"
78

89
import { apiClient } from "./api-fetch"
910

10-
export const LanguageMap: Record<
11-
SupportedLanguages,
12-
{
13-
label: string
14-
value: string
15-
code: string
16-
}
17-
> = {
18-
en: {
19-
value: "en",
20-
label: "English",
21-
code: "eng",
22-
},
23-
ja: {
24-
value: "ja",
25-
label: "Japanese",
26-
code: "jpn",
27-
},
28-
"zh-CN": {
29-
value: "zh-CN",
30-
label: "Simplified Chinese",
31-
code: "cmn",
32-
},
33-
"zh-TW": {
34-
value: "zh-TW",
35-
label: "Traditional Chinese (Taiwan)",
36-
code: "cmn",
37-
},
38-
}
39-
4011
export const checkLanguage = ({
4112
content,
4213
language,
@@ -48,10 +19,12 @@ export const checkLanguage = ({
4819
const pureContent = parseHtml(content)
4920
.toText()
5021
.replaceAll(/https?:\/\/\S+|www\.\S+/g, " ")
22+
const { code } = LANGUAGE_MAP[language]
23+
if (!code) return false
5124
const sourceLanguage = franc(pureContent, {
52-
only: [LanguageMap[language].code],
25+
only: [code],
5326
})
54-
if (sourceLanguage === LanguageMap[language].code) {
27+
if (sourceLanguage === code) {
5528
return true
5629
} else {
5730
return false

apps/desktop/src/renderer/src/modules/action/target-action-list.tsx

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ActionButton, Button } from "@follow/components/ui/button/index.js"
22
import { Divider } from "@follow/components/ui/divider/index.js"
33
import { Input } from "@follow/components/ui/input/index.js"
4-
import { ResponsiveSelect } from "@follow/components/ui/select/responsive.js"
54
import {
65
Table,
76
TableBody,
@@ -10,7 +9,7 @@ import {
109
TableHeader,
1110
TableRow,
1211
} from "@follow/components/ui/table/index.jsx"
13-
import type { ActionModel, SupportedLanguages } from "@follow/models/types"
12+
import type { ActionModel } from "@follow/models/types"
1413
import { Fragment, useMemo } from "react"
1514
import { useTranslation } from "react-i18next"
1615

@@ -31,28 +30,6 @@ type Action = {
3130
onRemove: (data: ActionModel) => void
3231
}
3332

34-
const TransitionOptions: {
35-
label: string
36-
value: SupportedLanguages
37-
}[] = [
38-
{
39-
label: "English",
40-
value: "en",
41-
},
42-
{
43-
label: "日本語",
44-
value: "ja",
45-
},
46-
{
47-
label: "简体中文",
48-
value: "zh-CN",
49-
},
50-
{
51-
label: "繁體中文",
52-
value: "zh-TW",
53-
},
54-
]
55-
5633
const AddTableRow = ({ onClick, disabled }: { onClick?: () => void; disabled?: boolean }) => {
5734
const { t } = useTranslation("settings")
5835
return (
@@ -108,26 +85,11 @@ export const TargetActionList = ({ index }: { index: number }) => {
10885
title: t("actions.action_card.translate_into"),
10986
enabled: !!translation,
11087
onInit: (data) => {
111-
data.result.translation = "zh-CN"
88+
data.result.translation = true
11289
},
11390
onRemove: (data) => {
11491
delete data.result.translation
11592
},
116-
config: () => (
117-
<ResponsiveSelect
118-
disabled={disabled}
119-
size="sm"
120-
value={translation}
121-
onValueChange={(value) => {
122-
onChange((data) => {
123-
data.result.translation = value
124-
})
125-
}}
126-
items={TransitionOptions}
127-
triggerClassName="w-fit max-w-44 text-sm"
128-
/>
129-
),
130-
configInline: true,
13193
},
13294
{
13395
title: t("actions.action_card.enable_readability"),

0 commit comments

Comments
 (0)