Why does NSLocale.current.identifier include currency on macOS?

562 Views Asked by At

If I request the current locales identifier on iOS, it returns just the identifier string:

let identifier = NSLocale.current.identifier // en_GB

However, on macOS 10.12.2 it also returns the currency:

let identifier = NSLocale.current.identifier // en_GB@currency=GBP

Is this a bug or expected behaviour?

2

There are 2 best solutions below

0
On BEST ANSWER

I think the best option for me here is to generate the code myself. To help with this I have created an extension on Locale:

extension Locale {
    var iso3166code: String {
        guard
            let language = languageCode,
            let region = regionCode
            else { return "en-US" }
        return "\(language)-\(region)"
    }
}

While this is accurate enough for my purposes, you should probably ensure it returns expected values for your project.

0
On

I ran into this recently. I'm not sure why, but apparently as of 10.12, localeIdentifier can include a bunch of stuff besides country and language.

Unfortunately, the documentation doesn't elaborate on the cases in which other metadata is included.

However, also as of 10.12, there's another property languageCode, which, in conjunction with countryCode, you can use to generate en_US.

This is what iTerm2 does.