Skip to content

Commit c26bc52

Browse files
committed
feat: i18n for biz error
Signed-off-by: Innei <i@innei.in>
1 parent 6ceb263 commit c26bc52

File tree

6 files changed

+78
-11
lines changed

6 files changed

+78
-11
lines changed

apps/renderer/src/@types/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ export const dayjsLocaleImportMap = {
1313
["zh-TW"]: ["zh-tw", () => import("dayjs/locale/zh-tw")],
1414
["zh-HK"]: ["zh-hk", () => import("dayjs/locale/zh-hk")],
1515
}
16-
export const ns = ["app", "common", "lang", "settings", "shortcuts"] as const
16+
export const ns = ["app", "common", "lang", "settings", "shortcuts", "errors"] as const
1717
export const defaultNS = "app" as const

apps/renderer/src/@types/default-resource.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import common_ja from "../../../../locales/common/ja.json"
99
import common_pt from "../../../../locales/common/pt.json"
1010
import common_zhCN from "../../../../locales/common/zh-CN.json"
1111
import common_zhTW from "../../../../locales/common/zh-TW.json"
12+
import errors_en from "../../../../locales/errors/en.json"
1213
import external_en from "../../../../locales/external/en.json"
1314
import lang_ardz from "../../../../locales/lang/ar-DZ.json"
1415
import lang_arma from "../../../../locales/lang/ar-MA.json"
@@ -37,6 +38,7 @@ export const defaultResources = {
3738
external: external_en,
3839
settings: settings_en,
3940
shortcuts: shortcuts_en,
41+
errors: errors_en,
4042
},
4143
"zh-CN": {
4244
lang: lang_zhCN,

apps/renderer/src/lib/error-parser.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { t } from "i18next"
12
import { FetchError } from "ofetch"
23
import { createElement } from "react"
34
import { toast } from "sonner"
@@ -9,10 +10,11 @@ export const getFetchErrorMessage = (error: Error) => {
910
if (error instanceof FetchError) {
1011
try {
1112
const json = JSON.parse(error.response?._data)
12-
// TODO get the biz code to show the error message, and for i18n
13-
// const bizCode = json.code
14-
const { reason } = json
15-
return `${json.message || error.message}${reason ? `: ${reason}` : ""}`
13+
14+
const { reason, code, message } = json
15+
const i18nKey = `errors:${code}` as any
16+
const i18nMessage = t(i18nKey) === i18nKey ? message : t(i18nKey)
17+
return `${i18nMessage}${reason ? `: ${reason}` : ""}`
1618
} catch {
1719
return error.message
1820
}
@@ -27,11 +29,11 @@ export const toastFetchError = (error: Error) => {
2729
try {
2830
const json = JSON.parse(error.response?._data)
2931

30-
// TODO get the biz code to show the error message, and for i18n
31-
// const bizCode = json.code
32-
const { reason } = json
32+
const { reason, code, message: _message } = json
33+
const i18nKey = `errors:${code}` as any
34+
const i18nMessage = t(i18nKey) === i18nKey ? message : t(i18nKey)
3335

34-
message = json.message || error.message
36+
message = i18nMessage
3537

3638
if (reason) {
3739
_reason = reason

apps/renderer/src/lib/native-menu.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export const showNativeMenu = async (
8383
}
8484
}
8585

86-
const unlisten = window.electron?.ipcRenderer.on("menu-click", (_, combinedIndex: string) => {
86+
const dispose = window.electron?.ipcRenderer.on("menu-click", (_, combinedIndex: string) => {
8787
const arr = combinedIndex.split("-")
8888
const accessors = [] as string[]
8989
for (let i = 0; i < arr.length; i++) {
@@ -101,10 +101,15 @@ export const showNativeMenu = async (
101101
})
102102

103103
window.electron?.ipcRenderer.once("menu-closed", () => {
104-
unlisten?.()
104+
dispose?.()
105105
if (el instanceof HTMLElement) {
106106
delete el.dataset.contextMenuOpen
107107
}
108+
109+
// dispatch mouse move event
110+
// NOTE: in order to remove the highlight of the trigger item
111+
// e.g. https://vscode.dev/github/RSSNext/follow/blob/dev/apps/renderer/src/modules/entry-column/layouts/EntryItemWrapper.tsx#L80
112+
document.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }))
108113
})
109114

110115
await tipcClient?.showContextMenu({

locales/errors/en.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"1000": "Unauthorized",
3+
"1001": "create session failed",
4+
"1002": "Invalid parameter",
5+
"1003": "Invalid invitation",
6+
"2000": "Only admins can refresh feeds",
7+
"2001": "Feed not found",
8+
"2002": "feedId or url required",
9+
"2003": "Feed fetch error",
10+
"2004": "Feed failed to parse",
11+
"2010": "Ownership challenge failed",
12+
"2011": "Subscription limit exceeded",
13+
"3000": "Entry not found",
14+
"3001": "Entry not belong to user",
15+
"4000": "Already claimed",
16+
"4001": "User wallet error",
17+
"4002": "Insufficient balance",
18+
"4003": "Feed insufficient withdrawable balance",
19+
"4004": "Target user wallet error",
20+
"5000": "Invitation limit exceeded. Please try again in a few days.",
21+
"5001": "Invitation already exists.",
22+
"5002": "Invitation code already used.",
23+
"5003": "Invitation code does not exist.",
24+
"6000": "User not found",
25+
"7000": "Setting not found",
26+
"7001": "Invalid setting tab",
27+
"7002": "Invalid setting payload",
28+
"7003": "Setting payload too large"
29+
}

locales/errors/ja.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"1000": "認証されていません",
3+
"1001": "セッションの作成に失敗しました",
4+
"1002": "無効なパラメータです",
5+
"1003": "無効な招待です",
6+
"2000": "フィードの更新は管理者のみが行えます",
7+
"2001": "フィードが見つかりません",
8+
"2002": "feedId または URL が必要です",
9+
"2003": "フィードの取得エラー",
10+
"2004": "フィードの解析に失敗しました",
11+
"2010": "所有権の確認に失敗しました",
12+
"2011": "サブスクリプションの上限に達しました",
13+
"3000": "エントリーが見つかりません",
14+
"3001": "エントリーがユーザーに属していません",
15+
"4000": "すでに請求済みです",
16+
"4001": "ユーザーウォレットエラー",
17+
"4002": "残高不足",
18+
"4003": "フィードの引き出し可能残高が不足しています",
19+
"4004": "対象ユーザーのウォレットエラー",
20+
"5000": "招待の上限に達しました。数日後にもう一度お試しください。",
21+
"5001": "招待がすでに存在します。",
22+
"5002": "招待コードはすでに使用されています。",
23+
"5003": "招待コードが存在しません。",
24+
"6000": "ユーザーが見つかりません",
25+
"7000": "設定が見つかりません",
26+
"7001": "無効な設定タブです",
27+
"7002": "無効な設定ペイロードです",
28+
"7003": "設定ペイロードが大きすぎます"
29+
}

0 commit comments

Comments
 (0)