From 2d0a21205f5b3328a088ffa0b7875482b82d339f Mon Sep 17 00:00:00 2001 From: imeepos Date: Tue, 13 Jan 2026 16:05:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20i18n=20=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加常见认证错误代码的翻译(中英文) - 修改 AuthForm 组件处理错误代码 - 支持的错误类型: - INVALID_USERNAME_OR_PASSWORD: 用户名或密码错误 - USER_NOT_FOUND: 用户不存在 - EMAIL_ALREADY_EXISTS: 邮箱已被注册 - USERNAME_ALREADY_EXISTS: 用户名已被占用 - WEAK_PASSWORD: 密码强度不够 - INVALID_EMAIL: 邮箱格式不正确 - VALIDATION_ERROR: 输入信息格式错误 - NETWORK_ERROR: 网络连接失败 - UNKNOWN_ERROR: 未知错误 Co-Authored-By: Claude --- components/blocks/AuthForm.tsx | 24 +++++++++++++++++++++--- locales/en-US.json | 13 ++++++++++++- locales/zh-CN.json | 13 ++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/components/blocks/AuthForm.tsx b/components/blocks/AuthForm.tsx index 5cc757b..0d5ad59 100644 --- a/components/blocks/AuthForm.tsx +++ b/components/blocks/AuthForm.tsx @@ -8,7 +8,7 @@ import { Block } from "../ui"; type AuthMode = "login" | "register"; -type AuthResponse = { data: unknown; error: { message: string } | null }; +type AuthResponse = { data: unknown; error: { message: string; code?: string } | null }; type SignInFn = (params: { username: string; password: string }) => Promise; type SignUpFn = (params: { email: string; password: string; name: string }) => Promise; @@ -32,6 +32,18 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr const isLogin = currentMode === "login"; + // 根据错误代码获取翻译后的错误信息 + const getErrorMessage = (error: { message: string; code?: string }): string => { + if (error.code) { + const translated = t(`authForm.errors.${error.code}`); + // 如果翻译不存在,返回原始 message + if (!translated.includes('authForm.errors')) { + return translated; + } + } + return error.message; + }; + const handleSubmit = async () => { if (!username.trim() || !password.trim()) { setError(t("authForm.fillCompleteInfo")); @@ -49,10 +61,16 @@ export function AuthForm({ mode = "login", onSuccess, onModeChange }: AuthFormPr try { if (isLogin) { const res = await signIn.username({ username, password }); - if (res.error) throw new Error(res.error.message); + if (res.error) { + setError(getErrorMessage(res.error)); + return; + } } else { const res = await signUp.email({ email, password, name: username }); - if (res.error) throw new Error(res.error.message); + if (res.error) { + setError(getErrorMessage(res.error)); + return; + } } onSuccess?.(); } catch (e: unknown) { diff --git a/locales/en-US.json b/locales/en-US.json index 39fe342..33ca717 100644 --- a/locales/en-US.json +++ b/locales/en-US.json @@ -181,7 +181,18 @@ "loginFailed": "Login failed", "registerFailed": "Registration failed", "noAccountRegister": "No account? Register", - "haveAccountLogin": "Already have an account? Login" + "haveAccountLogin": "Already have an account? Login", + "errors": { + "INVALID_USERNAME_OR_PASSWORD": "Invalid username or password", + "USER_NOT_FOUND": "User not found", + "EMAIL_ALREADY_EXISTS": "Email already exists", + "USERNAME_ALREADY_EXISTS": "Username already taken", + "WEAK_PASSWORD": "Password is too weak", + "INVALID_EMAIL": "Invalid email format", + "VALIDATION_ERROR": "Validation error", + "NETWORK_ERROR": "Network connection failed", + "UNKNOWN_ERROR": "Unknown error, please try again later" + } } } diff --git a/locales/zh-CN.json b/locales/zh-CN.json index d5f8f03..5cee6df 100644 --- a/locales/zh-CN.json +++ b/locales/zh-CN.json @@ -181,7 +181,18 @@ "loginFailed": "登录失败", "registerFailed": "注册失败", "noAccountRegister": "没有账号?去注册", - "haveAccountLogin": "已有账号?去登录" + "haveAccountLogin": "已有账号?去登录", + "errors": { + "INVALID_USERNAME_OR_PASSWORD": "用户名或密码错误", + "USER_NOT_FOUND": "用户不存在", + "EMAIL_ALREADY_EXISTS": "邮箱已被注册", + "USERNAME_ALREADY_EXISTS": "用户名已被占用", + "WEAK_PASSWORD": "密码强度不够", + "INVALID_EMAIL": "邮箱格式不正确", + "VALIDATION_ERROR": "输入信息格式错误", + "NETWORK_ERROR": "网络连接失败", + "UNKNOWN_ERROR": "未知错误,请稍后重试" + } } }