Created
May 22, 2021 23:44
-
-
Save shtnkgm/c6e411e9f2349072de33bca3f82d102f to your computer and use it in GitHub Desktop.
regionNameWithEmoji
This file contains 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
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