From 0ed2613297f8242cc1f03f4a2f788eea5787a137 Mon Sep 17 00:00:00 2001 From: Mohamed Abuobaida Date: Tue, 28 Mar 2017 14:12:47 +0300 Subject: [PATCH 1/2] localized string for country and currency codes --- Sources/Localize.swift | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sources/Localize.swift b/Sources/Localize.swift index 278240f..744d524 100644 --- a/Sources/Localize.swift +++ b/Sources/Localize.swift @@ -80,6 +80,26 @@ public extension String { func localizedPlural(_ argument: CVarArg) -> String { return NSString.localizedStringWithFormat(localized() as NSString, argument) as String } + + /** + Swift 2 friendly localization syntax for country code + - returns: The localized string for country code + */ + @available(iOS 10.0, *) + func localizedCountry() -> String { + let locale : NSLocale = NSLocale(localeIdentifier: Localize.currentLanguage()) + return locale.localizedString(forCountryCode: self) ?? self + } + + /** + Swift 2 friendly localization syntax for currency code + - returns: The localized string for currency code + */ + @available(iOS 10.0, *) + func localizedCurrency() -> String { + let locale : NSLocale = NSLocale(localeIdentifier: Localize.currentLanguage()) + return locale.localizedString(forCurrencyCode: self) ?? self + } } From ee51eeac981f5ec2a56125e723e2316cb717e110 Mon Sep 17 00:00:00 2001 From: Mohamed Abuobaida Date: Fri, 5 May 2017 13:36:09 +0300 Subject: [PATCH 2/2] move currency and country localization to Localize class --- Sources/Localize.swift | 46 ++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/Sources/Localize.swift b/Sources/Localize.swift index 744d524..27f56f8 100644 --- a/Sources/Localize.swift +++ b/Sources/Localize.swift @@ -80,26 +80,6 @@ public extension String { func localizedPlural(_ argument: CVarArg) -> String { return NSString.localizedStringWithFormat(localized() as NSString, argument) as String } - - /** - Swift 2 friendly localization syntax for country code - - returns: The localized string for country code - */ - @available(iOS 10.0, *) - func localizedCountry() -> String { - let locale : NSLocale = NSLocale(localeIdentifier: Localize.currentLanguage()) - return locale.localizedString(forCountryCode: self) ?? self - } - - /** - Swift 2 friendly localization syntax for currency code - - returns: The localized string for currency code - */ - @available(iOS 10.0, *) - func localizedCurrency() -> String { - let locale : NSLocale = NSLocale(localeIdentifier: Localize.currentLanguage()) - return locale.localizedString(forCurrencyCode: self) ?? self - } } @@ -183,5 +163,31 @@ open class Localize: NSObject { } return String() } + + /// Returns current locale. + /// + /// - Returns: current locale. + final class func currentLocale() -> Locale { + return Locale(identifier: Localize.currentLanguage()) + } + + /// Returns a localized string for a specified ISO 4217 currency code. + /// + /// - Parameter currencyCode: ISO 4217 currency code the currency whose name you want. + /// - Returns: Localized string for a specified ISO 4217 currency code. + final class func localizedCurrency(_ currencyCode: String) -> String { + return currentLocale() + .localizedString(forCurrencyCode: currencyCode) ?? currencyCode + } + + /// Returns the localized string for the specified country code. + /// + /// - Parameter countryCode: The country code indicating the country whose name you want. + /// - Returns: Localized string for the specified country code. + @available(iOS 10.0, *) + final class func localizedCountry(_ countryCode: String) -> String { + return (currentLocale() as NSLocale) + .localizedString(forCountryCode: countryCode) ?? countryCode + } }