5

---- UPDATE March 28, 2017 ----

When you set the language and region for the app via "Edit Scheme" in Xcode, you get the combined locale identifier of es-419_MX. However, when you change the actual language and region of the device/simulator by going into the settings, you get the "correct" locale identifier es_MX, while maintaining the language code of es-419, which effectively solves the issue for almost every use case.

// After setting language and region in Edit Scheme from Xcode
print(Bundle.main.preferredLocalizations) // ["es-419", "es"]
print(Locale.current) // es-419_MX (current)

// After setting the language and region from Settings
print(Bundle.main.preferredLocalizations) // ["es-419", "es"]
print(Locale.current) // es_MX (current)

---- /UPDATE ----

I'm localizing my app into Latin American Spanish (es-419). When I try to display a localized currency using the NumberFormatter, iOS returns a scarab ¤ instead of a dollar sign $.

For example, if the user's region is Mexico, they will have the region code es-419_MX. Instead of returning $, the following code returns ¤

let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "es-419_MX")
formatter.numberStyle = .currency
formatter.currencySymbol // ¤

If I remove the "419", I get the proper currency symbol:

let formatter = NumberFormatter()
formatter.locale = Locale(identifier: "es_MX")
formatter.numberStyle = .currency
formatter.currencySymbol // $

Is it possible to get the correct currency symbol when Locale.current returns es-419_MX? Or will I have to resort to a hack where I remove instances of 419 from the locale code?

Actual code:

func localizedCurrency(value: Double) -> String {
    let formatter = NumberFormatter()
    formatter.numberStyle = .currency
    formatter.locale = Locale.current
    return formatter.string(from: NSNumber(value: value)) ?? "$\(value)"
}
Naveed J.
  • 3,176
  • 2
  • 12
  • 11
  • 2
    I don't know anything about latin american spanish, but http://www.localeplanet.com/icu/es-419/index.html lists "¤" as currency symbol. – Martin R Mar 24 '17 at 19:32
  • What is the 419 from? – rmaddy Mar 24 '17 at 20:43
  • `es-419` is the code for Spanish - Latin America. – Naveed J. Mar 24 '17 at 20:44
  • But why use such a vague locale? On a typical iOS device a user will set their language and a country giving a more specific locale such as `es_MX` or `es_CO`, etc. – rmaddy Mar 24 '17 at 21:01
  • Localizing for `es-419` should handle translations for most regions in Latin American (instead of localizing for each specific country) – Naveed J. Mar 24 '17 at 21:05
  • What translations are you referring to? You are talking about number (currency) formatting. There is no reason at all to generalize that to some vague "Latin American" locale. Using the device's current locale lets the user see numbers and dates in their familiar format. – rmaddy Mar 24 '17 at 22:13

1 Answers1

4

You'll have to do the hack, or you'll have to be specific for each country.

Since es_419 is a generic specification for all Latin American countries, it can't "guess" that you want a $ to display.

Different countries under the es_419 locale "parent" have different currency character standards. For example, Bolivia's currency symbol is Bs.

ICU Locale “Spanish (Bolivia)” (es_BO)

leanne
  • 7,940
  • 48
  • 77