// 确保正确构建API请求URL的工具函数 export const buildApiUrl = (path: string): string => { // 确保路径以/开头 const normalizedPath = path.startsWith('/') ? path : `/${path}`; // 在浏览器环境中,使用当前域名构建完整URL if (typeof window !== 'undefined') { const protocol = window.location.protocol; const host = window.location.host; return `${protocol}//${host}${normalizedPath}`; } // 在服务端环境中,返回相对路径 return normalizedPath; }; // 专门用于加载翻译文件的函数 export const fetchTranslation = async (locale: string, namespace: string): Promise => { const url = buildApiUrl(`/locales/${locale}/${namespace}.json`); console.log(`Fetching translation from: ${url}`); // 调试日志 const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to fetch ${url}: ${response.status}`); } return response.json(); };