diff --git a/miniprogram/README.md b/miniprogram/README.md index 823a65f2..ca05e496 100644 --- a/miniprogram/README.md +++ b/miniprogram/README.md @@ -3,6 +3,7 @@ This directory contains a WeChat Mini Program plugin for WeKnora. It gives mobile users a lightweight entry point to: - configure a WeKnora API endpoint and tenant API key; +- switch UI language between Chinese and English; - list available knowledge bases; - import a URL into a selected knowledge base; - ask a selected knowledge base through WeKnora knowledge chat. @@ -13,10 +14,17 @@ This directory contains a WeChat Mini Program plugin for WeKnora. It gives mobil 2. Copy `project.private.config.json.example` to `project.private.config.json` and set your real Mini Program AppID. The shared `project.config.json` intentionally does not include an AppID to avoid forcing maintainers into a placeholder project. 3. Open the **Settings** tab and fill in: - API Base URL, for example `https://weknora.example.com`; - - API Key from the WeKnora tenant settings page. + - API Key from the WeKnora tenant settings page; + - Language (`中文` / `English`, default `中文`). 4. Open the **Knowledge** tab, refresh knowledge bases, and select the target knowledge base. 5. Import a URL or switch to **Chat** to ask questions. +## Language + +- Locale strings live in `utils/i18n.js`. +- The selected language is stored in local settings as `locale` (`zh` | `en`). +- Changing language in Settings updates page copy, navigation titles, and tab bar labels immediately. + ## Local development notes - WeChat DevTools may block `localhost` requests when URL validation is enabled. For local testing, either disable domain validation in DevTools or expose WeKnora through a HTTPS development domain. diff --git a/miniprogram/app.js b/miniprogram/app.js index 9543f8e5..309c7ae1 100644 --- a/miniprogram/app.js +++ b/miniprogram/app.js @@ -5,7 +5,8 @@ App({ wx.setStorageSync("weknora_settings", { baseUrl: "http://localhost:8080", apiKey: "", - selectedKnowledgeBaseId: "" + selectedKnowledgeBaseId: "", + locale: "zh" }); } } diff --git a/miniprogram/app.json b/miniprogram/app.json index ac8c34a7..d9b9a772 100644 --- a/miniprogram/app.json +++ b/miniprogram/app.json @@ -18,15 +18,21 @@ "list": [ { "pagePath": "pages/index/index", - "text": "Knowledge" + "text": "知识库", + "iconPath": "assets/tab/knowledge.png", + "selectedIconPath": "assets/tab/knowledge-active.png" }, { "pagePath": "pages/chat/chat", - "text": "Chat" + "text": "问答", + "iconPath": "assets/tab/chat.png", + "selectedIconPath": "assets/tab/chat-active.png" }, { "pagePath": "pages/settings/settings", - "text": "Settings" + "text": "设置", + "iconPath": "assets/tab/settings.png", + "selectedIconPath": "assets/tab/settings-active.png" } ] } diff --git a/miniprogram/app.wxss b/miniprogram/app.wxss index 9fbd9c44..c0ef05ba 100644 --- a/miniprogram/app.wxss +++ b/miniprogram/app.wxss @@ -72,3 +72,9 @@ button { button[disabled] { background: #c5c5c5; } + +.picker-value { + display: flex; + align-items: center; + color: #1a1a1a; +} diff --git a/miniprogram/assets/tab/chat-active.png b/miniprogram/assets/tab/chat-active.png new file mode 100644 index 00000000..d26c2e47 Binary files /dev/null and b/miniprogram/assets/tab/chat-active.png differ diff --git a/miniprogram/assets/tab/chat.png b/miniprogram/assets/tab/chat.png new file mode 100644 index 00000000..9844ebfc Binary files /dev/null and b/miniprogram/assets/tab/chat.png differ diff --git a/miniprogram/assets/tab/knowledge-active.png b/miniprogram/assets/tab/knowledge-active.png new file mode 100644 index 00000000..c8faa004 Binary files /dev/null and b/miniprogram/assets/tab/knowledge-active.png differ diff --git a/miniprogram/assets/tab/knowledge.png b/miniprogram/assets/tab/knowledge.png new file mode 100644 index 00000000..04d64f3c Binary files /dev/null and b/miniprogram/assets/tab/knowledge.png differ diff --git a/miniprogram/assets/tab/settings-active.png b/miniprogram/assets/tab/settings-active.png new file mode 100644 index 00000000..340ee37f Binary files /dev/null and b/miniprogram/assets/tab/settings-active.png differ diff --git a/miniprogram/assets/tab/settings.png b/miniprogram/assets/tab/settings.png new file mode 100644 index 00000000..61124098 Binary files /dev/null and b/miniprogram/assets/tab/settings.png differ diff --git a/miniprogram/pages/chat/chat.js b/miniprogram/pages/chat/chat.js index 095d6a1e..5e60831f 100644 --- a/miniprogram/pages/chat/chat.js +++ b/miniprogram/pages/chat/chat.js @@ -1,16 +1,54 @@ const { getSettings } = require("../../utils/config"); const { createSession, knowledgeChat } = require("../../utils/request"); const { collectAnswerFromSSE } = require("../../utils/sse"); +const { applyNavTitle, applyTabBar, getMessages, t } = require("../../utils/i18n"); + +function buildViewModel() { + const messages = getMessages(); + return { + chatTitle: messages.chatTitle, + chatSubtitle: messages.chatSubtitle, + questionLabel: messages.question, + questionPlaceholder: messages.questionPlaceholder, + askText: messages.askWeKnora, + answerLabel: messages.answer + }; +} Page({ data: { + chatTitle: "知识问答", + chatSubtitle: "向当前选中的 WeKnora 知识库提问。", + questionLabel: "问题", + questionPlaceholder: "输入你的问题…", + askText: "向 WeKnora 提问", + answerLabel: "回答", answer: "", + displayAnswer: "", loading: false, query: "", rawResponse: "", sessionId: "" }, + applyI18n() { + this.setData(buildViewModel()); + }, + + onLoad() { + this.applyI18n(); + }, + + onShow() { + try { + applyTabBar(); + applyNavTitle("navChat"); + this.applyI18n(); + } catch (error) { + // keep fallback Chinese copy + } + }, + onQueryInput(event) { this.setData({ query: event.detail.value }); }, @@ -22,30 +60,36 @@ Page({ const settings = getSettings(); const response = await createSession(settings.selectedKnowledgeBaseId); - const sessionId = response.data?.id; + const sessionId = response && response.data ? response.data.id : ""; if (!sessionId) { - throw new Error("The session API did not return a session id."); + throw new Error(t("sessionIdMissing")); } - this.setData({ sessionId }); + this.setData({ sessionId: sessionId }); return sessionId; }, async ask() { - this.setData({ answer: "", rawResponse: "", loading: true }); + this.setData({ answer: "", rawResponse: "", displayAnswer: "", loading: true }); try { const sessionId = await this.ensureSession(); const settings = getSettings(); - const response = await knowledgeChat(sessionId, this.data.query.trim(), settings.selectedKnowledgeBaseId); + const response = await knowledgeChat( + sessionId, + this.data.query.trim(), + settings.selectedKnowledgeBaseId + ); const rawResponse = typeof response === "string" ? response : JSON.stringify(response); const answer = collectAnswerFromSSE(rawResponse); + const displayAnswer = answer || rawResponse; this.setData({ - answer, - rawResponse: answer ? "" : rawResponse + answer: answer, + rawResponse: answer ? "" : rawResponse, + displayAnswer: displayAnswer }); } catch (error) { wx.showModal({ - title: "Chat failed", - content: error.message, + title: t("chatFailed"), + content: (error && error.message) || "", showCancel: false }); } finally { diff --git a/miniprogram/pages/chat/chat.json b/miniprogram/pages/chat/chat.json index baf81b0e..21a8583f 100644 --- a/miniprogram/pages/chat/chat.json +++ b/miniprogram/pages/chat/chat.json @@ -1,3 +1,3 @@ { - "navigationBarTitleText": "Chat" + "navigationBarTitleText": "问答" } diff --git a/miniprogram/pages/chat/chat.wxml b/miniprogram/pages/chat/chat.wxml index f0f0b7dc..71958700 100644 --- a/miniprogram/pages/chat/chat.wxml +++ b/miniprogram/pages/chat/chat.wxml @@ -1,18 +1,18 @@ - Knowledge Chat - Ask the selected WeKnora knowledge base. + {{chatTitle}} + {{chatSubtitle}} - Question -