Reputation: 13
I want to show "繁體中文", "简体中文" in a view.
I use Locale.displayname to get displayname and my parameter is "zh-Hant" and "zh-Hans", the value will return "中文(繁體)"and "中文(简体)".
Here is parts of my code:
let loacleName = locale.displayName(forKey: NSLocale.Key.identifier, value: "zh-Hant")
Is anyone know how can I get "繁體中文", "简体中文" from iOS function?
Upvotes: 1
Views: 667
Reputation: 285079
This is a solution using native Locale
. For the identifiers zh-Hant
and zh-Hans
it removes the unwanted characters with Regular Expression (be aware that the characters are not parentheses and space characters) and swaps both pairs.
extension Locale {
var localizedFullDisplayName : String? {
if self.identifier.hasPrefix("zh-Han") {
guard let trimmed = self.localizedString(forIdentifier: self.identifier)?.replacingOccurrences(of: "[()]", with: "", options: .regularExpression) else { return nil }
return String(trimmed.suffix(2) + trimmed.prefix(2))
} else {
return self.localizedString(forIdentifier: locale.identifier)
}
}
}
let locale = Locale(identifier: "zh-Hans")
locale.localizedFullDisplayName
Upvotes: 2