codeburn/mac/Sources/CodeBurnMenubar/Localization.swift
ozymandiashh d62857ea4e feat(menubar): route every user-facing string through a strings catalog
Groundwork for #1219. The menubar app hardcoded ~530 English strings across
Views/, the presentation structs, the status-item menu and the update alerts,
so there was nothing for a translator to translate.

Every user-facing literal now goes through `L(_:)` / `L(_:_:)`, which look the
string up in `Localizable.strings` in the SwiftPM target resource bundle. The
key *is* the English copy, so English stays the development language and a
missing translation degrades to correct English instead of a dotted
identifier. `en.lproj` is therefore an identity table; it exists so the bundle
advertises `en` and so a translation can be diffed against it.

Why the lookups are explicit rather than relying on SwiftUI's implicit
`LocalizedStringKey`: SwiftPM emits target resources into a sibling bundle
(`CodeBurnMenubar_CodeBurnMenubar.bundle`) that the packaging scripts copy into
`Contents/Resources`. `Bundle.main` has no `.lproj` at all, so `Text("literal")`
would always miss. Naming `Bundle.module` is the one form that resolves the
same way in `swift run`, in `swift test` and in the packaged `.app`.

Enum raw values that double as identity (`Period`, `MenubarScope`,
`InsightMode`, `AccentPreset`, `ProviderFilter`) keep their raw value and gain
a `displayLabel`, so persistence and cache keys are untouched by translation.

Three display-only date formatters that were pinned to `en_US_POSIX` with fixed
patterns now use `setLocalizedDateFormatFromTemplate`, and the calendar
popover's weekday row comes from the locale's own symbols. The `yyyy-MM-dd`
formatter stays POSIX: it parses and builds data keys, not display text.

Not extracted, deliberately: provider, model and plan names; units; currency
codes; shell commands and paths; quota window labels that policy code matches
on by English substring (`QuotaSummary.headlineWindow`); and anything the
`codeburn` CLI produces.

Also copies the SwiftPM resource bundle into the app in build-local.sh, using
the named-bundle + `[[ -d ]]` pattern package-app.sh already uses. Without it
the assembled app finds no strings table (and already trapped on first icon
load, the resource-bundle half of #1262).
2026-09-12 15:21:49 -07:00

62 lines
2.9 KiB
Swift

import Foundation
/// Localization for the menubar app (#1219). No third-party library: one
/// `Localizable.strings` table per locale, shipped as SwiftPM target resources
/// and resolved by AppKit against the user's system language.
///
/// # Why every lookup is explicit
///
/// SwiftPM emits target resources into a *sibling* bundle
/// (`CodeBurnMenubar_CodeBurnMenubar.bundle`, copied into the app's
/// `Contents/Resources` by the packaging scripts), never into the app
/// bundle's resource root. `Bundle.main` therefore has no `.lproj` at all, so
/// the implicit `LocalizedStringKey` path that SwiftUI uses for
/// `Text("literal")` would always miss. Routing every string through `L(_:)`,
/// which names `Bundle.module`, is the one form that resolves identically in
/// `swift run`, in `swift test`, and in the packaged `.app`.
///
/// # Keys are the English copy
///
/// The key *is* the English string (`"Refresh Now"`, `"%lld sessions"`), so
/// English stays the development language: a key with no translation renders
/// as correct English rather than a visible identifier, and `en.lproj` is an
/// identity table kept only so the bundle advertises `en` as a localization
/// and so the catalog can be diffed against a translation.
///
/// # What is not translated
///
/// Provider and model names (`Claude`, `Codex`, `Gemini`, `Sonnet`), units
/// (`tok/s`, `ACU`, `%`), currency codes, shell commands, and anything the
/// `codeburn` CLI itself produces (payload labels, activity and project names,
/// error text forwarded from the subprocess) stay verbatim. Numbers, dates,
/// and currency keep going through the locale-aware formatters they already
/// used — `L(_:_:)` only substitutes already-formatted values.
enum L10n {
/// The bundle that carries the `<locale>.lproj` tables.
static let bundle: Bundle = .module
/// Table name, i.e. `Localizable.strings`.
static let table = "Localizable"
/// Locales shipped today. Mirrored by `CFBundleLocalizations` in the two
/// packaging scripts once a second language exists.
static let supportedLocalizations = ["en"]
}
/// Localized copy for `key`, falling back to the key (its English text) when a
/// translation is missing.
func L(_ key: String) -> String {
L10n.bundle.localizedString(forKey: key, value: key, table: L10n.table)
}
/// Localized format string for `key`, filled with `arguments`.
///
/// The specifiers in the key are part of the contract between the tables:
/// `%@` for an already-formatted value (currency, token count, provider name),
/// `%lld` for a plain `Int`. Deliberately formatted without a locale so the
/// substituted values keep exactly the grouping the existing formatters chose
/// — re-grouping a `%lld` here would disagree with the
/// `asCurrency()` / `asThousandsSeparated()` output next to it.
func L(_ key: String, _ arguments: CVarArg...) -> String {
String(format: L(key), arguments: arguments)
}