My framework contains a protocol that allows conforming types to describe themselves. One of its requirements is a static member containing the name of the type. This can be used as (for example) the header of a SwiftUI.List displaying a list of conforming types.
public protocol Describable: Identifiable {
static var className: LocalizedStringResource { get }
// ...
}
struct ItemList<I: Describable>: View {
let items: [I]
var body: some View {
Text(I.className)
.font(.title)
List(items) { item in
// ...
}
}
}
The above works fine, but I want to be able to annotate the key with a comment, and to be able to vary the key by plurality, so that a struct Building { ... } can have a localized key for the plural 'Buildings' (and equivalent plural forms in other languages).
Optimally, the protocol should provide this functionality automatically to conforming types, however I don't know how to achieve this properly with LocalizedStringResource and the new string catalog (Localizable.xcstrings). The keys being added to my project contain a second variable instead of the class name (e.g. %1$lld %2$@). Below are some of my attempts:
public protocol Describable: Identifiable {
static var localizedClassName: LocalizedStringResource { get }
static var className: String { get }
static var staticClassName: StaticString { get }
// ...
}
extension Describable {
static func classDescription(for count: Int) -> LocalizedStringResource {
"\(count) \(className)"
}
static func classDescription2(for count: Int) -> LocalizedStringResource {
"\(count) \(staticClassName.asString)"
}
static func classDescription3(for count: Int) -> LocalizedStringResource {
LocalizedStringResource("\(count) \(staticClassName.asString)", comment: "Type Description")
}
}
private extension StaticString {
var asString: String {
self.withUTF8Buffer { String(decoding: $0, as: UTF8.self) }
}
}
I've tried a few more variations, but the mixed usage of LocalizedStringResource, StaticString, String, String.LocalizationValue and other string literals makes my brain hurt and none of it looks correct anymore. What is the proper way to do this? Or am I trying to reinvent the wheel and does such functionality already exist within Swift?
Describableprotocol at all.%lldor similar placeholder in the key. So you will need to add an extra key, in addition to just the word itself.