From f9a0b395861ef05763b4efe1f4ece1c64bef47f8 Mon Sep 17 00:00:00 2001 From: bfcs <52602045+bfcs@users.noreply.github.com> Date: Sat, 28 Feb 2026 15:10:31 +0800 Subject: [PATCH] fix: respect fixed translation quota in UI stats and DeepL provider (#3404) --- .../src/services/translators/providers/deepl.ts | 5 ++--- apps/readest-app/src/utils/access.ts | 13 +++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/apps/readest-app/src/services/translators/providers/deepl.ts b/apps/readest-app/src/services/translators/providers/deepl.ts index c3b1cb25..a2710821 100644 --- a/apps/readest-app/src/services/translators/providers/deepl.ts +++ b/apps/readest-app/src/services/translators/providers/deepl.ts @@ -2,9 +2,8 @@ import { getAPIBaseUrl } from '@/services/environment'; import { stubTranslation as _ } from '@/utils/misc'; import { ErrorCodes, TranslationProvider } from '../types'; import { UserPlan } from '@/types/quota'; -import { getSubscriptionPlan } from '@/utils/access'; +import { getSubscriptionPlan, getTranslationQuota } from '@/utils/access'; import { normalizeToShortLang } from '@/utils/lang'; -import { DEFAULT_DAILY_TRANSLATION_QUOTA } from '@/services/constants'; import { saveDailyUsage } from '../utils'; const DEEPL_API_ENDPOINT = getAPIBaseUrl() + '/deepl/translate'; @@ -44,7 +43,7 @@ export const deeplProvider: TranslationProvider = { use_cache: useCache, }); - const quota = DEFAULT_DAILY_TRANSLATION_QUOTA[userPlan]; + const quota = getTranslationQuota(userPlan); try { const response = await fetch(DEEPL_API_ENDPOINT, { method: 'POST', headers, body }); diff --git a/apps/readest-app/src/utils/access.ts b/apps/readest-app/src/utils/access.ts index 2ea325fe..2bf03d54 100644 --- a/apps/readest-app/src/utils/access.ts +++ b/apps/readest-app/src/utils/access.ts @@ -47,11 +47,18 @@ export const getStoragePlanData = (token: string) => { }; }; +export const getTranslationQuota = (plan: UserPlan): number => { + const fixedQuota = parseInt(process.env['NEXT_PUBLIC_TRANSLATION_FIXED_QUOTA'] || '0'); + return ( + fixedQuota || DEFAULT_DAILY_TRANSLATION_QUOTA[plan] || DEFAULT_DAILY_TRANSLATION_QUOTA['free'] + ); +}; + export const getTranslationPlanData = (token: string) => { const data = jwtDecode(token) || {}; const plan: UserPlan = data['plan'] || 'free'; const usage = getDailyUsage() || 0; - const quota = DEFAULT_DAILY_TRANSLATION_QUOTA[plan]; + const quota = getTranslationQuota(plan); return { plan, @@ -63,9 +70,7 @@ export const getTranslationPlanData = (token: string) => { export const getDailyTranslationPlanData = (token: string) => { const data = jwtDecode(token) || {}; const plan = data['plan'] || 'free'; - const fixedQuota = parseInt(process.env['NEXT_PUBLIC_TRANSLATION_FIXED_QUOTA'] || '0'); - const quota = - fixedQuota || DEFAULT_DAILY_TRANSLATION_QUOTA[plan] || DEFAULT_DAILY_TRANSLATION_QUOTA['free']; + const quota = getTranslationQuota(plan); return { plan,