Skip to content

Commit 2fd05cb

Browse files
authored
Merge pull request #7 from devchat-ai/feat/i18n
Feat/i18n
2 parents 5075804 + 2a490be commit 2fd05cb

File tree

18 files changed

+806
-623
lines changed

18 files changed

+806
-623
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"typescript"
4545
],
4646
"scripts": {
47-
"compile": "webpack 1",
47+
"compile": "webpack",
4848
"watch": "webpack --watch",
4949
"package": "webpack --mode production --devtool hidden-source-map",
5050
"compile-tests": "tsc -p . --outDir out",

src/util/bridge.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// 1. 打开设置
1414
// 2. 启动 ask code 安装
1515
// 3. 设置 access key
16-
- featureToggles ??
16+
- featureToggles // 判断有没有 ask code
1717
- isDevChatInstalled // 判断 ask code 是否安装
1818

1919
- historyMessages // 页面的历史消息
@@ -42,6 +42,10 @@
4242
- receiveMessage // 对话
4343
- systemMessage // 没用了
4444

45+
## 函数
46+
47+
onInitializationFinish // 初始化完成之后服务端掉用
48+
4549
# css
4650

4751
--vscode-editor-font-familyy

src/util/ideaBridge.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,19 @@ const JStoIdea = {
161161

162162
window.JSJavaBridge.callJava(JSON.stringify(params));
163163
},
164-
updateSetting: (value: string) => {
165-
// 因为现在只有更换模型,所以直接取 value
164+
updateSetting: (message: { value: string; key2: string }) => {
165+
if (message.key2 === "Language") {
166+
JStoIdea.setLanguage(message.value);
167+
return;
168+
}
166169
const params = {
167170
action: "updateSetting/request",
168171
metadata: {
169172
callback: "IdeaToJSMessage",
170173
},
171174
payload: {
172175
setting: {
173-
currentModel: value,
176+
currentModel: message.value,
174177
},
175178
},
176179
};
@@ -242,6 +245,19 @@ const JStoIdea = {
242245

243246
window.JSJavaBridge.callJava(JSON.stringify(params));
244247
},
248+
setLanguage: (language) => {
249+
const params = {
250+
action: "updateLanguage/request",
251+
metadata: {
252+
callback: "IdeaToJSMessage",
253+
},
254+
payload: {
255+
language: language || "en",
256+
},
257+
};
258+
console.log("setLanguage params: ", params);
259+
window.JSJavaBridge.callJava(JSON.stringify(params));
260+
},
245261
userInput: (message) => {
246262
const params = {
247263
action: "input/request",
@@ -372,10 +388,17 @@ class IdeaBridge {
372388
value: setting.currentModel,
373389
});
374390
this.handle.getUserAccessKey({
391+
accessKey: setting.apiKey,
392+
});
393+
this.handle.getUserSetting({
375394
endPoint: setting.apiBase,
376395
accessKey: setting.apiKey,
377396
keyType: setting.apiKey.startsWith("DC") ? "DevChat" : "OpenAi",
378397
});
398+
this.handle.getSetting({
399+
value: setting.language,
400+
key2: "Language",
401+
});
379402
}
380403

381404
resviceAccessKey(res: string = "") {
@@ -492,7 +515,7 @@ class IdeaBridge {
492515
JStoIdea.viewDiff(message.content);
493516
break;
494517
case "updateSetting":
495-
JStoIdea.updateSetting(message.value);
518+
JStoIdea.updateSetting(message);
496519
break;
497520
case "doCommit":
498521
JStoIdea.commit(message.content);
@@ -515,6 +538,9 @@ class IdeaBridge {
515538
case "userInput":
516539
JStoIdea.userInput(message);
517540
break;
541+
case "setLanguage":
542+
JStoIdea.setLanguage(message);
543+
break;
518544
default:
519545
break;
520546
}

src/views/components/BalanceTip/index.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Group,
1010
LoadingOverlay,
1111
} from "@mantine/core";
12+
import { Trans } from "react-i18next";
1213

1314
const currencyMap = {
1415
USD: "$",
@@ -19,7 +20,10 @@ function formatBalance(balance: number) {
1920
return Math.round(balance * 1000) / 1000;
2021
}
2122

22-
function formatCurrency(balance: number, currency: string) {
23+
function formatCurrency(balance: number | null, currency: string) {
24+
if (balance === null || balance === undefined) {
25+
return "";
26+
}
2327
return `${currencyMap[currency] || currency}${balance}`;
2428
}
2529

@@ -34,13 +38,13 @@ const envMap = {
3438
},
3539
};
3640

37-
// eslint-disable-next-line @typescript-eslint/naming-convention
3841
export default function WechatTip() {
3942
const [bindWechat, setBindWechat] = useState(false);
4043
const [balance, setBalance] = useState<null | number>(null);
4144
const [currency, setCurrency] = useState("USD");
4245
const [accessKey, setAccessKey] = useState("");
4346
const [env, setEnv] = useState("prod");
47+
4448
const [loading, setLoading] = useState(false);
4549
const platform = process.env.platform;
4650

@@ -82,7 +86,7 @@ export default function WechatTip() {
8286
useEffect(() => {
8387
getSettings();
8488
messageUtil.registerHandler(
85-
"getUserAccessKey",
89+
"getUserSetting",
8690
(message: { endPoint: string; accessKey: string; keyType: string }) => {
8791
if (message.keyType === "DevChat" && message.accessKey) {
8892
if (message.endPoint.includes("api-test.devchat.ai")) {
@@ -105,6 +109,8 @@ export default function WechatTip() {
105109
});
106110
};
107111

112+
const formatedCurrency = formatCurrency(balance, currency);
113+
108114
if (balance === null || balance === undefined) {
109115
return null;
110116
}
@@ -138,18 +144,19 @@ export default function WechatTip() {
138144
>
139145
<Group style={{ width: "90%" }}>
140146
<Text size="sm">
141-
Your remaining credit is {formatCurrency(balance, currency)}. Sign
142-
in to{" "}
143-
{platform === "idea" ? (
144-
<Text td="underline" c="blue" onClick={(e) => openLink(e)}>
145-
web.devchat.ai{" "}
146-
</Text>
147-
) : (
148-
<a href={envMap[env].link} target="_blank">
149-
web.devchat.ai{" "}
150-
</a>
151-
)}
152-
to {bindWechat ? "purchase more tokens." : "earn additional ¥8"}
147+
<Trans i18nKey="balance" formatedCurrency={formatedCurrency}>
148+
Your remaining credit is {{ formatedCurrency }}. Sign in to{" "}
149+
{platform === "idea" ? (
150+
<Text td="underline" c="blue" onClick={(e) => openLink(e)}>
151+
web.devchat.ai{" "}
152+
</Text>
153+
) : (
154+
<a href={envMap[env].link} target="_blank">
155+
web.devchat.ai{" "}
156+
</a>
157+
)}
158+
to purchase more tokens.
159+
</Trans>
153160
</Text>
154161
<LoadingOverlay visible={loading} />
155162
</Group>

src/views/components/Header/index.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22
import {
33
Header,
44
Avatar,
@@ -22,7 +22,28 @@ const useStyles = createStyles((theme) => ({
2222

2323
export default function Head() {
2424
const { classes } = useStyles();
25-
const { t, i18n } = useTranslation();
25+
const { i18n } = useTranslation();
26+
27+
useEffect(() => {
28+
messageUtil.sendMessage({
29+
command: "getSetting",
30+
key1: "DevChat",
31+
key2: "Language",
32+
});
33+
messageUtil.registerHandler(
34+
"getSetting",
35+
(data: { key2: string; value: string }) => {
36+
console.log("data: ", data);
37+
if (data.key2 === "Language") {
38+
if (data.value && data.value.toLocaleLowerCase() === "en") {
39+
i18n.changeLanguage("en");
40+
} else {
41+
i18n.changeLanguage("zh");
42+
}
43+
}
44+
}
45+
);
46+
}, []);
2647

2748
const openSetting = () => {
2849
messageUtil.sendMessage({
@@ -35,6 +56,13 @@ export default function Head() {
3556
const currentLang = i18n.language;
3657
const newLang = currentLang === "en" ? "zh" : "en";
3758
i18n.changeLanguage(newLang);
59+
60+
messageUtil.sendMessage({
61+
command: "updateSetting",
62+
key1: "DevChat",
63+
key2: "Language",
64+
value: newLang,
65+
});
3866
};
3967

4068
return (
@@ -68,11 +96,11 @@ export default function Head() {
6896
<IconSettings size="1.125rem" />
6997
</ActionIcon>
7098
</div>
71-
{/* <div>
99+
<div>
72100
<ActionIcon size="sm" onClick={switchLang}>
73101
<IconLanguage size="1.125rem" />
74102
</ActionIcon>
75-
</div> */}
103+
</div>
76104
</Flex>
77105
</Flex>
78106
</Header>

src/views/components/InputMessage/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import Topic from "./Topic";
3434
import { observer } from "mobx-react-lite";
3535
import { useMst } from "@/views/stores/RootStore";
3636
import { ChatContext } from "@/views/stores/InputStore";
37-
import { Message } from "@/views/stores/ChatStore";
37+
import { Trans, useTranslation } from "react-i18next";
3838

3939
const useStyles = createStyles((theme) => ({
4040
actionIcon: {
@@ -60,6 +60,7 @@ const useStyles = createStyles((theme) => ({
6060
const InputMessage = observer((props: any) => {
6161
const { classes } = useStyles();
6262
const { input, chat } = useMst();
63+
const { t } = useTranslation();
6364
const {
6465
contexts,
6566
menuOpend,
@@ -311,7 +312,7 @@ const InputMessage = observer((props: any) => {
311312
color: theme.colors.gray[6],
312313
}}
313314
>
314-
{description}
315+
<Trans>{description}</Trans>
315316
</Text>
316317
</Stack>
317318
</Flex>
@@ -456,7 +457,10 @@ const InputMessage = observer((props: any) => {
456457
<Menu.Dropdown>
457458
{modelMenus.map((modelName) => {
458459
return (
459-
<Menu.Item key={modelName} onClick={() => changeModel(modelName)}>
460+
<Menu.Item
461+
key={modelName}
462+
onClick={() => changeModel(modelName)}
463+
>
460464
{getModelShowName(modelName)}
461465
</Menu.Item>
462466
);
@@ -515,7 +519,7 @@ const InputMessage = observer((props: any) => {
515519
marginTop: 5,
516520
marginBottom: 5,
517521
}}
518-
placeholder="Ask DevChat a question or type ‘/’ for workflow"
522+
placeholder={t("Ask DevChat a question or type ‘/’ for workflow")}
519523
styles={{
520524
rightSection: {
521525
alignItems: "flex-end",

src/views/components/MessageAvatar/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const MessageAvatar = observer((props: IProps) => {
6060
) : (
6161
<Avatar color="cyan" size={25} radius="xl" src={SvgAvatarUser} />
6262
)}
63-
<Text weight="bold">{avatarType === "bot" ? "DevChat" : "User"}</Text>
63+
<Text weight="bold">{avatarType === "bot" ? "DevChat" : t("User")}</Text>
6464
{avatarType === "user" ? (
6565
<Flex
6666
gap="xs"
@@ -72,7 +72,7 @@ const MessageAvatar = observer((props: IProps) => {
7272
>
7373
<Tooltip
7474
sx={{ padding: "3px", fontSize: "var(--vscode-editor-font-size)" }}
75-
label={done ? "Refilled" : "Refill prompt"}
75+
label={done ? t("Refilled") : t("Refill prompt")}
7676
withArrow
7777
position="left"
7878
color="gray"
@@ -142,7 +142,7 @@ const MessageAvatar = observer((props: IProps) => {
142142
padding: "3px",
143143
fontSize: "var(--vscode-editor-font-size)",
144144
}}
145-
label={copied ? "Copied" : "Copy message"}
145+
label={copied ? t("Copied") : t("Copy message")}
146146
withArrow
147147
position="left"
148148
color="gray"

0 commit comments

Comments
 (0)