Skip to content

Instantly share code, notes, and snippets.

@nullptr2this
Created August 11, 2020 18:40
Show Gist options
  • Save nullptr2this/a67c9fa054cf9ebb1f2da06c6c74153e to your computer and use it in GitHub Desktop.
Save nullptr2this/a67c9fa054cf9ebb1f2da06c6c74153e to your computer and use it in GitHub Desktop.
Type safe text keys for localizable strings files
protocol LocalizedText {
func asLocalizedText() -> String
}
protocol TextKey : LocalizedText {
var key: String { get }
}
extension TextKey {
func asLocalizedText() -> String {
return Bundle.main.localizedString(forKey: key, value: nil, table: nil)
}
}
protocol TextKeyEnum : TextKey {
associatedtype T : RawRepresentable where T == Self, T.RawValue == String
}
extension TextKeyEnum {
typealias T = Self
var key: String { get { self.rawValue } }
}
enum Text {
enum Common : String, TextKeyEnum {
case ok = "text.common.ok"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment