Procházet zdrojové kódy

Implement internationalization for error handling in request utilities

- Added translation support for error messages in createRequest and createRequestNative functions.
- Updated error handling to utilize localized messages for various scenarios, including login expiration, permissions, and network issues.
- Introduced new error messages in English, Indonesian, and Chinese localization files.
0es před 2 měsíci
rodič
revize
c962ab4a24
5 změnil soubory, kde provedl 61 přidání a 22 odebrání
  1. 12 9
      app/utils/request.ts
  2. 13 10
      app/utils/requestNative.ts
  3. 12 1
      i18n/locales/en.json
  4. 12 1
      i18n/locales/id.json
  5. 12 1
      i18n/locales/zh.json

+ 12 - 9
app/utils/request.ts

@@ -85,6 +85,9 @@ const generateSign = (id: string, time: number, token: string, secret: string, b
 export const createRequest = () => {
   const runtimeConfig = useRuntimeConfig()
   const { token, logout } = useAuth()
+  const nuxtApp = useNuxtApp()
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  const t = ((nuxtApp as any)?.$i18n?.t?.bind((nuxtApp as any)?.$i18n) as ((key: string) => string) | undefined) || ((key: string) => key)
 
   // API base configuration (read within a valid Nuxt context)
   const API_CONFIG = {
@@ -185,7 +188,7 @@ export const createRequest = () => {
 
       // Handle business errors
       handleApiError(data.code, data.msg)
-      throw new Error(data.msg || 'Request failed')
+      throw new Error(data.msg || t('common.errors.requestFailed'))
     }
   }
 
@@ -206,29 +209,29 @@ export const createRequest = () => {
           handleUnauthorized()
           break
         case 403:
-          showError('No permission to access this resource')
+          showError(t('common.errors.noPermission'))
           break
         case 404:
-          showError('Resource not found')
+          showError(t('common.errors.notFound'))
           break
         case 500:
-          showError('Server error, please try again later')
+          showError(t('common.errors.serverError'))
           break
         default:
-          showError(data?.msg || error?.message || 'Request failed')
+          showError(data?.msg || error?.message || t('common.errors.requestFailed'))
       }
     }
     else {
       // Network error or timeout
       const message = error?.message || ''
       if (message.includes('timeout')) {
-        showError('Request timeout, please try again')
+        showError(t('common.errors.timeout'))
       }
       else if (message.includes('Network Error')) {
-        showError('Network error, please check your connection')
+        showError(t('common.errors.network'))
       }
       else {
-        showError(message || 'Unknown error occurred')
+        showError(message || t('common.errors.unknownError'))
       }
     }
 
@@ -261,7 +264,7 @@ export const createRequest = () => {
       const route = useRoute()
       const redirect = route.fullPath
 
-      showError('Login expired, please login again')
+      showError(t('common.errors.loginExpired'))
       navigateTo({
         path: '/login',
         query: redirect && redirect !== '/login' ? { redirect } : undefined,

+ 13 - 10
app/utils/requestNative.ts

@@ -91,6 +91,9 @@ const generateSign = (id: string, time: number, token: string, secret: string, b
 export const createRequestNative = () => {
   const runtimeConfig = useRuntimeConfig()
   const { token, logout } = useAuth()
+  const nuxtApp = useNuxtApp()
+  // eslint-disable-next-line @typescript-eslint/no-explicit-any
+  const t = ((nuxtApp as any)?.$i18n?.t?.bind((nuxtApp as any)?.$i18n) as ((key: string) => string) | undefined) || ((key: string) => key)
 
   const API_CONFIG = {
     baseURL: runtimeConfig.public.apiBase || '/api',
@@ -108,7 +111,7 @@ export const createRequestNative = () => {
     const raw = window.GAMI_BRIDGE?.requestHeader
     if (!raw || typeof raw !== 'object') {
       console.error('Must in native context')
-      showError('Must in native context')
+      showError(t('common.errors.nativeOnly'))
 
       return {}
     }
@@ -194,7 +197,7 @@ export const createRequestNative = () => {
       }
 
       handleApiError(data.code, data.msg)
-      throw new Error(data.msg || 'Request failed')
+      throw new Error(data.msg || t('common.errors.requestFailed'))
     }
   }
 
@@ -211,23 +214,23 @@ export const createRequestNative = () => {
           handleUnauthorized()
           break
         case 403:
-          showError('No permission to access this resource')
+          showError(t('common.errors.noPermission'))
           break
         case 404:
-          showError('Resource not found')
+          showError(t('common.errors.notFound'))
           break
         case 500:
-          showError('Server error, please try again later')
+          showError(t('common.errors.serverError'))
           break
         default:
-          showError(data?.msg || error?.message || 'Request failed')
+          showError(data?.msg || error?.message || t('common.errors.requestFailed'))
       }
     }
     else {
       const message = error?.message || ''
-      if (message.includes('timeout')) showError('Request timeout, please try again')
-      else if (message.includes('Network Error')) showError('Network error, please check your connection')
-      else showError(message || 'Unknown error occurred')
+      if (message.includes('timeout')) showError(t('common.errors.timeout'))
+      else if (message.includes('Network Error')) showError(t('common.errors.network'))
+      else showError(message || t('common.errors.unknownError'))
     }
 
     return Promise.reject(error)
@@ -249,7 +252,7 @@ export const createRequestNative = () => {
       const route = useRoute()
       const redirect = route.fullPath
 
-      showError('Login expired, please login again')
+      showError(t('common.errors.loginExpired'))
       navigateTo({
         path: '/login',
         query: redirect && redirect !== '/login' ? { redirect } : undefined,

+ 12 - 1
i18n/locales/en.json

@@ -5,7 +5,18 @@
     "copied": "Copied to clipboard",
     "copyFailed": "Copy failed",
     "logOut": "Log out",
-    "unknown": "Unknown"
+    "unknown": "Unknown",
+    "errors": {
+      "loginExpired": "Login expired, please login again",
+      "nativeOnly": "Please open in the app",
+      "noPermission": "No permission",
+      "notFound": "Not found",
+      "serverError": "Server error, try again later",
+      "requestFailed": "Request failed",
+      "timeout": "Request timeout, try again",
+      "network": "Network error, check your connection",
+      "unknownError": "Unknown error"
+    }
   },
   "order": {
     "common": {

+ 12 - 1
i18n/locales/id.json

@@ -5,7 +5,18 @@
     "copied": "Disalin ke papan klip",
     "copyFailed": "Gagal menyalin",
     "logOut": "Keluar",
-    "unknown": "Tidak diketahui"
+    "unknown": "Tidak diketahui",
+    "errors": {
+      "loginExpired": "Sesi habis, login lagi",
+      "nativeOnly": "Buka di aplikasi",
+      "noPermission": "Tidak ada izin",
+      "notFound": "Tidak ditemukan",
+      "serverError": "Server bermasalah, coba lagi",
+      "requestFailed": "Permintaan gagal",
+      "timeout": "Waktu habis, coba lagi",
+      "network": "Gangguan jaringan",
+      "unknownError": "Terjadi kesalahan"
+    }
   },
   "order": {
     "common": {

+ 12 - 1
i18n/locales/zh.json

@@ -5,7 +5,18 @@
     "copied": "已复制到剪贴板",
     "copyFailed": "复制失败",
     "logOut": "退出登录",
-    "unknown": "未知"
+    "unknown": "未知",
+    "errors": {
+      "loginExpired": "登录已过期,请重新登录",
+      "nativeOnly": "请在App内打开",
+      "noPermission": "暂无权限",
+      "notFound": "资源不存在",
+      "serverError": "服务器异常,请稍后再试",
+      "requestFailed": "请求失败",
+      "timeout": "请求超时,请重试",
+      "network": "网络异常,请检查网络",
+      "unknownError": "未知错误"
+    }
   },
   "order": {
     "common": {