Currency code - scarab (ยค) for es-419
---- UPDATE March 28, 2017 ----
When you set the language and region for an application via Edit Scheme in Xcode, you get a merged locale ID es-419_MX
. However, when you change the actual language and device / simulator scope by going into settings, you get the "correct" locale ID es_MX
while still retaining the language code es-419
that effectively solves the problem in almost every 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 am localizing my application in Latin American Spanish ( es-419
). When I try to display the localized currency using NumberFormatter
iOS returns the scarab ยค
instead of the dollar sign $
.
For example, if the custom region is Mexico, they will have a 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 "419" I get the corresponding 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 it Locale.current
returns es-419_MX
? Or will I have to resort to a hacker where I remove instances 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)"
}
source to share
You will need to do a hack, or you will have to be specific to each country.
Since this es_419
is a common specification for all Latin American countries, it cannot "guess" what you want to display $
.
Different countries es_419
have different standards for currency symbols under the parent word . For example, the currency symbol for Bolivia Bs
.
source to share