For String, we can perform localization in the following manner.
Localizable.strings
-------------------
"created_time" = "Created time";
Source code
-----------
extension String: Localizable {
public var localized: String {
return NSLocalizedString(self, comment: "")
}
}
let localizedString = "created_time".localize
But, what about a boolean value? For instance, I want the defaultValue
to be true
, for Chinese speaking users, but false
for others.
static var isShowLunarCalendar: Bool {
set {
UserDefaults.standard.set(newValue, forKey: "SHOW_LUNAR_CALENDAR")
}
get {
// TODO: How to localize this boolean variable?
let defaultValue = false
UserDefaults.standard.bool(forKey: "SHOW_LUNAR_CALENDAR", defaultValue)
}
}
May I know, what are some good ways to localize a boolean value?
CodePudding user response:
You can do it like this
let res = Bool("BoolLocalize".localize)! // res is a true Bool in ch
ch.local
"BoolLocalize" = "true";
other.local
"BoolLocalize" = "false";