Skip to content

Commit

Permalink
Add Locale extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Jul 15, 2023
1 parent c3eac1d commit 59707da
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Sources/OversizeCore/Extensions/Locale/Currency+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Copyright © 2023 Alexander Romanov
// CurrencyExtension.swift, created on 15.07.2023
//

import Foundation

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
public extension Locale.Currency {
var dispalyName: String? {
Locale.current.localizedString(forCurrencyCode: identifier)
}

var dispalyLocalizedSymbol: String? {
Locale.current.localizedCurrencySymbol(forCurrencyCode: identifier)
}

var dispalySymbol: String? {
let allLocalCurrencies = Locale.availableIdentifiers.compactMap { Locale(identifier: $0) }
return allLocalCurrencies.first { $0.currency == self }?.currencySymbol
}

var locale: Locale? {
let allLocalCurrencies = Locale.availableIdentifiers.compactMap { Locale(identifier: $0) }
return allLocalCurrencies.first { $0.currency == self }
}

static var countryCurrencies: [Locale.Currency] {
Locale.availableIdentifiers
.compactMap { Locale(identifier: $0).currency }
.removeDuplicates()
.sorted { $0.dispalyName ?? "" < $1.dispalyName ?? "" }
}
}
23 changes: 23 additions & 0 deletions Sources/OversizeCore/Extensions/Locale/Locale+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright © 2023 Alexander Romanov
// LocaleExtension.swift, created on 15.07.2023
//

import Foundation

@available(macOS 13, iOS 16, tvOS 16, watchOS 9, *)
extension Locale {
func localizedCurrencySymbol(forCurrencyCode currencyCode: String) -> String? {
guard
let languageCode = language.languageCode?.identifier,
let regionCode = language.region?.identifier
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
}
}

0 comments on commit 59707da

Please sign in to comment.