Reputation: 3346
---- 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)"
}
Upvotes: 5
Views: 1057
Reputation: 8739
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)
Upvotes: 4