Created
August 11, 2020 18:40
-
-
Save nullptr2this/a67c9fa054cf9ebb1f2da06c6c74153e to your computer and use it in GitHub Desktop.
Type safe text keys for localizable strings files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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