前端添加中英文切换

This commit is contained in:
Dev
2026-11-02 16:43:15 +08:00
parent 41ca22f59e
commit 061fa9d029
22 changed files with 817 additions and 246 deletions
+16 -14
View File
@@ -1,3 +1,5 @@
import { currentText } from '../i18n/translations';
const API_BASE_URL = '/api-proxy';
function getAuthHeaders(): Record<string, string> {
@@ -68,7 +70,7 @@ export const registerUser = async (name: string, email: string, password: string
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Registration failed');
throw new Error(error.detail || currentText('registerFail'));
}
return response.json();
@@ -83,7 +85,7 @@ export const loginUser = async (email: string, password: string): Promise<{ toke
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Login failed');
throw new Error(error.detail || currentText('loginFail'));
}
const data = await response.json();
@@ -158,7 +160,7 @@ export const generatePlan = async (
onProgress?: (event: GenerateProgressEvent) => void
): Promise<Plan> => {
const token = localStorage.getItem('token');
if (!token) throw new Error('Not authenticated');
if (!token) throw new Error(currentText('notAuthenticated'));
const response = await fetch(`${API_BASE_URL}/api/generate/`, {
method: 'POST',
@@ -168,11 +170,11 @@ export const generatePlan = async (
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to generate plan');
throw new Error(error.detail || currentText('generateFail'));
}
if (!response.body) {
throw new Error('ReadableStream not supported');
throw new Error(currentText('streamUnsupported'));
}
const reader = response.body.getReader();
@@ -216,7 +218,7 @@ export const generatePlan = async (
}
if (!resultData) {
throw new Error('No result received from server');
throw new Error(currentText('noResult'));
}
return {
@@ -235,7 +237,7 @@ export const getUserPlans = async (): Promise<Plan[]> => {
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to fetch plans');
throw new Error(error.detail || currentText('fetchPlansFail'));
}
const data = await response.json();
@@ -252,7 +254,7 @@ export const getPlan = async (planId: number): Promise<Plan> => {
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to fetch plan');
throw new Error(error.detail || currentText('fetchPlanFail'));
}
const data = await response.json();
@@ -270,7 +272,7 @@ export const deletePlan = async (planId: number): Promise<void> => {
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to delete plan');
throw new Error(error.detail || currentText('deletePlanFail'));
}
};
@@ -292,7 +294,7 @@ export const updateTask = async (planId: number, taskDbId: number, updates: Part
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to update task');
throw new Error(error.detail || currentText('updateTaskFail'));
}
const data = await response.json();
@@ -350,7 +352,7 @@ export const createTask = async (planId: number, params: CreateTaskParams): Prom
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to create task');
throw new Error(error.detail || currentText('createTaskFail'));
}
const data = await response.json();
@@ -365,7 +367,7 @@ export const deleteTask = async (planId: number, taskDbId: number): Promise<{ de
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to delete task');
throw new Error(error.detail || currentText('deleteTaskFail'));
}
const data = await response.json();
@@ -380,7 +382,7 @@ export const updateTaskStatus = async (planId: number, taskDbId: number, status:
if (!response.ok) {
const error = await response.json();
throw new Error(error.detail || 'Failed to update task status');
throw new Error(error.detail || currentText('updateStatusFail'));
}
const data = await response.json();
@@ -404,7 +406,7 @@ export const reorderTasks = async (planId: number, taskOrders: { id: number; sor
});
if (!response.ok) {
const error = await response.json().catch(() => ({ detail: '排序更新失败' }));
throw new Error(error.detail || '排序更新失败');
throw new Error(error.detail || currentText('reorderFail'));
}
};