Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active December 19, 2023 19:21
Show Gist options
  • Select an option

  • Save mattt/6d022b66f08ea8c1b99ebe7e48b95c4b to your computer and use it in GitHub Desktop.

Select an option

Save mattt/6d022b66f08ea8c1b99ebe7e48b95c4b to your computer and use it in GitHub Desktop.

Revisions

  1. mattt revised this gist Feb 14, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions localizedCurrencySymbol.swift
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,14 @@ extension Locale {
    func localizedCurrencySymbol(forCurrencyCode currencyCode: String) -> String? {
    guard let languageCode = languageCode, let regionCode = regionCode else { return nil }

    /*
    Each currency can have a symbol ($, £, ¥),
    but those symbols may be shared with other currencies.
    For example, in Canadian and American locales,
    the $ symbol on its own implicitly represents CAD and USD, respectively.
    Including the language and region here ensures that
    USD is represented as $ in America and US$ in Canada.
    */
    let components: [String: String] = [
    NSLocale.Key.languageCode.rawValue: languageCode,
    NSLocale.Key.countryCode.rawValue: regionCode,
  2. mattt created this gist Feb 14, 2020.
    27 changes: 27 additions & 0 deletions localizedCurrencySymbol.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    import Foundation

    extension Locale {
    func localizedCurrencySymbol(forCurrencyCode currencyCode: String) -> String? {
    guard let languageCode = languageCode, let regionCode = regionCode else { return nil }

    let components: [String: String] = [
    NSLocale.Key.languageCode.rawValue: languageCode,
    NSLocale.Key.countryCode.rawValue: regionCode,
    NSLocale.Key.currencyCode.rawValue: currencyCode,
    ]

    let identifier = Locale.identifier(fromComponents: components)

    return Locale(identifier: identifier).currencySymbol
    }
    }

    Locale.isoCurrencyCodes.compactMap { (code) -> String? in
    guard let name = Locale.current.localizedString(forCurrencyCode: code),
    let symbol = Locale.current.localizedCurrencySymbol(forCurrencyCode: code)
    else {
    return nil
    }

    return "\(name) - \(symbol) (\(code))"
    }