Skip to content

Commit

Permalink
patch to find usa for usd instead of ecuador
Browse files Browse the repository at this point in the history
resolves #2
  • Loading branch information
Sega-Zero committed Nov 8, 2020
1 parent c9a6a27 commit 5bbe893
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions iso4217/Classes/CurrencyCode+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ private extension CurrencyCode {
}

var locale: Locale {
// try to find the mosta appropriate locale for currency, this solves the issue when USD may be resolved to Equador.
// Here we're trying to search through user's preferred locales first. In 99% of cases this will locate more appropriate locale for user.
let preferredLocales: [Locale] = Locale.preferredLanguages.compactMap {
let locale = Locale(identifier: $0.replacingOccurrences(of: "-", with: "_"))
guard let code = locale.currencyCode, code == self.rawValue else { return nil }
return locale
}

if let locale = preferredLocales.first(where: { $0.currencySymbol != $0.currencyCode }) {
return locale
}

let appropriateLocales: [Locale] = Locale.availableIdentifiers.compactMap {
let locale = Locale(identifier: $0)
guard let code = locale.currencyCode, code == self.rawValue else { return nil }
return locale
}

return appropriateLocales.first(where: { $0.currencySymbol != $0.currencyCode }) ?? appropriateLocales.first ?? self.canonicalLocale
}

Expand Down

0 comments on commit 5bbe893

Please sign in to comment.