Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Sources/CodexBar/Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ func L(_ key: String, language: String) -> String {
return codexBarLocalizedString(key, bundle: bundle, resourceBundle: resourceBundle)
}

/// Uses an explicit duration for Simplified Chinese quota surfaces while preserving the generic
/// `Session` translation for conversations and other non-quota UI.
func localizedSessionQuotaLabel(_ label: String, windowMinutes: Int?) -> String {
let localizedLabel = L(label)
guard label == "Session",
localizedBundle().bundleURL.lastPathComponent.caseInsensitiveCompare("zh-Hans.lproj") == .orderedSame,
let windowMinutes
else { return localizedLabel }

if windowMinutes == 7 * 24 * 60 {
return L("Weekly")
}
guard (60...(12 * 60)).contains(windowMinutes), windowMinutes.isMultiple(of: 60) else {
return localizedLabel
}
return "\(codexBarLocalizedInteger(windowMinutes / 60)) \(L("Hour"))"
}

func codexBarLocalizedLocale() -> Locale {
codexBarLocale(forLanguage: resolvedAppLanguage())
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/CodexBar/MenuCardView+ModelHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ extension UsageMenuCardView.Model {
? L("Monthly")
: input.metadata.opusLabel.map(L) ?? L("Sonnet")
return (
L(primaryLabel),
localizedSessionQuotaLabel(primaryLabel, windowMinutes: snapshot.primary?.windowMinutes),
L(secondaryLabel),
tertiaryLabel,
input.metadata.supportsOpus)
Expand Down
12 changes: 9 additions & 3 deletions Sources/CodexBar/PlanUtilizationHistoryChartMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ struct PlanUtilizationHistoryChartMenuView: View {
.map { history in
VisibleSeries(
selection: SeriesSelection(name: history.name, windowMinutes: history.windowMinutes),
title: self.seriesTitle(name: history.name, metadata: metadata),
title: self.seriesTitle(
name: history.name,
metadata: metadata,
windowMinutes: history.windowMinutes),
history: history)
}
}
Expand Down Expand Up @@ -621,11 +624,12 @@ struct PlanUtilizationHistoryChartMenuView: View {

private nonisolated static func seriesTitle(
name: PlanUtilizationSeriesName,
metadata: ProviderMetadata?) -> String
metadata: ProviderMetadata?,
windowMinutes: Int) -> String
{
switch name {
case .session:
L(metadata?.sessionLabel ?? "Session")
localizedSessionQuotaLabel(metadata?.sessionLabel ?? "Session", windowMinutes: windowMinutes)
case .weekly:
L(metadata?.weeklyLabel ?? "Weekly")
case .monthly:
Expand Down Expand Up @@ -673,6 +677,7 @@ struct PlanUtilizationHistoryChartMenuView: View {
let xDomain: ClosedRange<Double>?
let selectedSeries: String?
let visibleSeries: [String]
let visibleSeriesTitles: [String]
let usedPercents: [Double]
let pointDates: [String]
}
Expand All @@ -696,6 +701,7 @@ struct PlanUtilizationHistoryChartMenuView: View {
xDomain: model.xDomain,
selectedSeries: selectedSeries?.id,
visibleSeries: visibleSeries.map(\.id),
visibleSeriesTitles: visibleSeries.map(\.title),
usedPercents: model.points.map(\.usedPercent),
pointDates: model.points.map { point in
let formatter = DateFormatter()
Expand Down
88 changes: 88 additions & 0 deletions Tests/CodexBarTests/PopupLocalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,62 @@ import Testing
@MainActor
@Suite(.serialized)
struct PopupLocalizationTests {
@Test
func `simplified Chinese derives session quota titles from their duration`() throws {
try CodexBarLocalizationOverride.$appLanguage.withValue("zh-Hans") {
for (windowMinutes, expectedTitle) in [(60, "1 小时"), (300, "5 小时"), (720, "12 小时")] {
let model = try Self.makeClaudeMenuCardModel(primaryWindowMinutes: windowMinutes)

#expect(model.metrics.first?.title == expectedTitle)
}
}
}

@Test
func `simplified Chinese labels a Claude weekly primary fallback accurately`() throws {
try CodexBarLocalizationOverride.$appLanguage.withValue("zh-Hans") {
let model = try Self.makeClaudeMenuCardModel(primaryWindowMinutes: 7 * 24 * 60)

#expect(model.metrics.first?.title == "每周")
}
}

@Test
func `simplified Chinese history selector uses quota duration without changing conversations`() {
CodexBarLocalizationOverride.$appLanguage.withValue("zh-Hans") {
let now = Date(timeIntervalSince1970: 1_700_000_000)
let histories = [
PlanUtilizationSeriesHistory(
name: .session,
windowMinutes: 300,
entries: [PlanUtilizationHistoryEntry(capturedAt: now, usedPercent: 10, resetsAt: nil)]),
PlanUtilizationSeriesHistory(
name: .weekly,
windowMinutes: 7 * 24 * 60,
entries: [PlanUtilizationHistoryEntry(capturedAt: now, usedPercent: 20, resetsAt: nil)]),
]
let snapshot = UsageSnapshot(
primary: RateWindow(
usedPercent: 10,
windowMinutes: 300,
resetsAt: nil,
resetDescription: nil),
secondary: RateWindow(
usedPercent: 20,
windowMinutes: 7 * 24 * 60,
resetsAt: nil,
resetDescription: nil),
updatedAt: now)
let model = PlanUtilizationHistoryChartMenuView._modelSnapshotForTesting(
histories: histories,
provider: .claude,
snapshot: snapshot)

#expect(model.visibleSeriesTitles == ["5 小时", "每周"])
#expect(String(format: L("Session %@"), "abc123") == "会话 abc123")
}
}

@Test
func `descriptor account labels use selected localization`() throws {
try CodexBarLocalizationOverride.$appLanguage.withValue("zh-Hant") {
Expand Down Expand Up @@ -194,4 +250,36 @@ struct PopupLocalizationTests {
return text
}
}

private static func makeClaudeMenuCardModel(primaryWindowMinutes: Int) throws -> UsageMenuCardView.Model {
let now = Date(timeIntervalSince1970: 1_700_000_000)
let metadata = try #require(ProviderDefaults.metadata[.claude])
let snapshot = UsageSnapshot(
primary: RateWindow(
usedPercent: 10,
windowMinutes: primaryWindowMinutes,
resetsAt: now.addingTimeInterval(3600),
resetDescription: nil),
secondary: nil,
updatedAt: now)
return UsageMenuCardView.Model.make(.init(
provider: .claude,
metadata: metadata,
snapshot: snapshot,
credits: nil,
creditsError: nil,
dashboard: nil,
dashboardError: nil,
tokenSnapshot: nil,
tokenError: nil,
account: AccountInfo(email: nil, plan: nil),
isRefreshing: false,
lastError: nil,
usageBarsShowUsed: false,
resetTimeDisplayStyle: .countdown,
tokenCostUsageEnabled: false,
showOptionalCreditsAndExtraUsage: true,
hidePersonalInfo: false,
now: now))
}
}