Swifty and modern NSLocalizedString
Localizability is a simple Swift library to localize strings easily.
Inspired by sindresorhus/Defaults.
Add https://github.com/Fus1onDev/Localizability
in Swift Package Manager.
First, add some keys and values to Localizable.strings
.
"thisIsASampleText" = "This is a sample text.";
"iLikeString" = "I like %@.";
Next, declare the keys.
import Localizability
extension Localized.Keys {
static let thisIsASampleText = Key("thisIsASampleText")
static let iLikeString = Key("iLikeString")
}
Finally, you can access it like this:
let str = Localized[.thisIsASampleText]
print(str)
// "This is a sample text." (in your language)
If arguments are added, the localized string is used as a template.
let str = Localized[.iLikeString, "apples"]
print(str)
// "I like apples." (in your language)
These SwiftUI components are also available.
var body: some View {
LocalizedText(key: .thisIsASampleText)
// or
LocalizedText[.thisIsASampleText]
}
MIT