SPB Git

Repository Public License

FeatureText.swift 2.9 KB 59 lines swift

1import Foundation23/// A user-facing string that knows which String Catalog it comes from. The key is the English text, as4/// everywhere in the app; `module` names the SwiftPM target whose `Resources/Localizable.xcstrings` holds the5/// translations, which is how the catalog audit (NavigatorFeaturesTests) finds them.6public struct LocalizedText: Sendable, Hashable, CustomStringConvertible {7    public let key: String8    /// Target that owns the catalog (`"NavigatorUI"`, `"FeatureOrbit"`…). Empty for verbatim text.9    public let module: String10    private let bundle: Bundle?1112    public init(key: String, module: String, bundle: Bundle) {13        self.key = key; self.module = module; self.bundle = bundle14    }1516    /// Text that is not translated (a proper name, a title typed by the user).17    public static func verbatim(_ text: String) -> LocalizedText { LocalizedText(verbatim: text) }1819    private init(verbatim text: String) { key = text; module = ""; bundle = nil }2021    public var isVerbatim: Bool { bundle == nil }2223    /// The string in the app's current language.24    public var string: String { bundle?.localizedString(forKey: key, value: key, table: nil) ?? key }25    public var description: String { string }2627    public static func == (lhs: LocalizedText, rhs: LocalizedText) -> Bool { lhs.key == rhs.key && lhs.module == rhs.module }28    public func hash(into hasher: inout Hasher) { hasher.combine(key); hasher.combine(module) }29}3031/// Localisation of one feature target: its catalog lives in its own bundle. Each feature declares one value and32/// two file-private shorthands (see docs/EXTENDING.md):33///34///     let strings = FeatureStrings(module: "FeatureOrbit", bundle: .module)35///     func L(_ key: String, _ arguments: any CVarArg...) -> String { strings.localized(key, arguments) }36///     func T(_ key: String) -> LocalizedText { strings.text(key) }37public struct FeatureStrings: Sendable {38    public let module: String39    public let bundle: Bundle4041    public init(module: String, bundle: Bundle) { self.module = module; self.bundle = bundle }4243    /// Looks `key` (the English text) up in the feature's catalog and fills `String(format:)` placeholders.44    public func localized(_ key: String, _ arguments: [any CVarArg] = []) -> String {45        let format = bundle.localizedString(forKey: key, value: key, table: nil)46        guard !arguments.isEmpty else { return format }47        return String(format: format, locale: .current, arguments: arguments)48    }4950    /// A deferred lookup, for titles the registry stores (commands, panels, settings sections).51    public func text(_ key: String) -> LocalizedText { LocalizedText(key: key, module: module, bundle: bundle) }52}5354/// The shell's own strings.55let shellStrings = FeatureStrings(module: "NavigatorUI", bundle: NavigatorStrings.bundle)5657/// A shell title for the registry. The key is the English text, as with `L`.58func T(_ key: String) -> LocalizedText { shellStrings.text(key) }59
Files main
Menu

Theme