From a61ca078fc1ee39f7c93303361845735250f07ba Mon Sep 17 00:00:00 2001 From: fengsilin Date: Wed, 1 Jul 2026 16:13:33 +0800 Subject: [PATCH] fix: localize display pricing units --- web/src/helpers/utils.jsx | 23 ++++++++++++++++++++--- web/src/helpers/utils.test.js | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/web/src/helpers/utils.jsx b/web/src/helpers/utils.jsx index 8253b1b..f41adc4 100644 --- a/web/src/helpers/utils.jsx +++ b/web/src/helpers/utils.jsx @@ -644,13 +644,14 @@ export const formatDisplayPricingItem = ({ t = (key) => key, }) => { const unit = item.unit || 'call'; + const unitText = formatDisplayPricingUnit(unit, t); const effectivePrice = Number(item.effectivePrice ?? item.price ?? 0); const hasDiscount = item.discount_rate > 0 && item.discount_rate < 1; return { ...item, - formattedPrice: `${displayPrice(effectivePrice)} / ${t(unit)}`, + formattedPrice: `${displayPrice(effectivePrice)} / ${unitText}`, originalPrice: hasDiscount - ? `${displayPrice(Number(item.price || 0))} / ${t(unit)}` + ? `${displayPrice(Number(item.price || 0))} / ${unitText}` : '', discountText: hasDiscount ? `${Math.round(item.discount_rate * 100) / 10}折` @@ -658,6 +659,22 @@ export const formatDisplayPricingItem = ({ }; }; +const DISPLAY_PRICING_UNIT_FALLBACKS = { + second: '秒', + call: '次', + image: '张图片', + video: '个视频', +}; + +export const formatDisplayPricingUnit = (unit, t = (key) => key) => { + const normalizedUnit = unit || 'call'; + const translated = t(normalizedUnit); + if (translated && translated !== normalizedUnit) { + return translated; + } + return DISPLAY_PRICING_UNIT_FALLBACKS[normalizedUnit] || normalizedUnit; +}; + export const summarizeDisplayPricing = ({ record, displayPrice, @@ -682,7 +699,7 @@ export const summarizeDisplayPricing = ({ min === max ? displayPrice(min) : `${displayPrice(min)} - ${displayPrice(max)}`; - return `${priceText} / ${t(unit)}`; + return `${priceText} / ${formatDisplayPricingUnit(unit, t)}`; }); return { diff --git a/web/src/helpers/utils.test.js b/web/src/helpers/utils.test.js index 1b4e0e3..f831d3d 100644 --- a/web/src/helpers/utils.test.js +++ b/web/src/helpers/utils.test.js @@ -17,6 +17,8 @@ globalThis.localStorage = { const { calculateModelPrice, + formatDisplayPricingItem, + formatDisplayPricingUnit, formatMatrixPrice, hasDisplayPricing, hasMatrixPricing, @@ -151,6 +153,14 @@ describe('model pricing helpers', () => { ]); expect(items[0].effectivePrice).toBeCloseTo(0.266664); expect(items[1].discount_rate).toBe(1); + expect( + formatDisplayPricingItem({ + item: items[0], + displayPrice, + t: (key) => key, + }).discountText, + ).toBe('8折'); + expect(formatDisplayPricingUnit('second', (key) => key)).toBe('秒'); }); it('summarizes display pricing by unit', () => { @@ -178,7 +188,7 @@ describe('model pricing helpers', () => { }); expect(summary.isDisplayPricing).toBe(true); - expect(summary.price).toBe('$0.333 - $0.583 / second'); + expect(summary.price).toBe('$0.333 - $0.583 / 秒'); expect(summary.details).toHaveLength(2); }); @@ -209,6 +219,6 @@ describe('model pricing helpers', () => { }); expect(result.isDisplayPricing).toBe(true); - expect(result.price).toBe('$0.333 / second'); + expect(result.price).toBe('$0.333 / 秒'); }); });