Skip to content

Instantly share code, notes, and snippets.

@shtnkgm
Created May 22, 2021 23:44
Show Gist options
  • Save shtnkgm/c6e411e9f2349072de33bca3f82d102f to your computer and use it in GitHub Desktop.
Save shtnkgm/c6e411e9f2349072de33bca3f82d102f to your computer and use it in GitHub Desktop.
regionNameWithEmoji
import Foundation
extension Locale {
var regionEmoji: String? {
guard let regionCode = regionCode else { return nil }
return regionCode
.unicodeScalars
.map({ 127397 + $0.value })
.compactMap(UnicodeScalar.init)
.map(String.init)
.joined()
}
func regionNameWithEmoji(for languageCode: String) -> String? {
guard let regionCode = regionCode,
let localizedRegionName = Locale(identifier: languageCode).localizedString(forRegionCode: regionCode),
let emoji = regionEmoji else { return nil }
return localizedRegionName + emoji
}
}
print(Locale(identifier: "ja_JP").regionNameWithEmoji(for: "ja") ?? "-") // 日本🇯🇵
print(Locale(identifier: "en_US").regionNameWithEmoji(for: "ja") ?? "-") // アメリカ合衆国🇺🇸
print(Locale(identifier: "fr_FR").regionNameWithEmoji(for: "ja") ?? "-") // フランス🇫🇷
print(Locale(identifier: "ja_JP").regionNameWithEmoji(for: "en") ?? "-") // Japan🇯🇵
print(Locale(identifier: "en_US").regionNameWithEmoji(for: "en") ?? "-") // United States🇺🇸
print(Locale(identifier: "fr_FR").regionNameWithEmoji(for: "en") ?? "-") // France🇫🇷
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment