Nslocale Swift 3

NSLocale Swift 3

NSLocale was not renamed, it still exists. Locale is a
new type introduced in Swift 3 as a value type wrapper
(compare SE-0069 Mutability and Foundation Value Types).

Apparently Locale has no displayName(forKey:value:) method,
but you can always convert it to its Foundation counterpart

public var symbol: String {
return (Locale.current as NSLocale).displayName(forKey: .currencySymbol, value: code) ?? ""

More examples:

// Dollar symbol in the german locale:
let s1 = (Locale(identifier:"de") as NSLocale).displayName(forKey: .currencySymbol, value: "USD")!
print(s1) // $

// Dollar symbol in the italian locale:
let s2 = (Locale(identifier:"it") as NSLocale).displayName(forKey: .currencySymbol, value: "USD")!
print(s2) // US$

How to get country code using NSLocale in Swift 3

See Wojciech N.'s answer
for a simpler solution!

Similarly as in NSLocale Swift 3, you have to cast the overlay type Locale back to its
Foundation counterpart NSLocale in order to retrieve the country code:

if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {

How to get systemLocaleCountryCode in swift 3?

In Swift3
change to

let systemLocaleCountryCode = (NSLocale.system as NSLocale).object(forKey: .countryCode) as? String

NSCurrentLocaleDidChangeNotification in Swift 3?

According to the NSLocale reference there is

class let currentLocaleDidChangeNotification: NSNotification.Name
// Notification that indicates that the user’s locale changed.

which you can use as

let notification = NSLocale.currentLocaleDidChangeNotification

Port method swizzle of NSLocale.currentLocale from swift 2.3 to swift 3

current is a (read-only) computed property of NSLocale:

open class var current: Locale { get }

You build a selector for Objectice-C property getter with

let originalSelector = #selector(getter: NSLocale.current)

See also SE-0064 Referencing the Objective-C selector of property getters and setters.

How to retrieve a value for NSLocale .scriptCode in Swift

Not all locales have a script code. See the Language and Locale IDs section of the Internationalization and Localization Guide.

Locale identifiers can contain various parts such as the language code, script code, and region code. The script and region codes are optional.

Look at the documentation for Locale scriptCode for an example:

For example, for the locale “zh-Hant-HK”, returns “Hant”.

Simpler locales such as en_US or de_DE don't have a script code.

Related Topics

Leave a reply