2

I've been researching for a solution to this issue for a while and could not find a proper solution. The closest I've got was this entry from stackoverflow. But since this was asked 4 years ago (and is not actually my issue although it could help to solve my problem), I 'll ask again to see if now there is a better workaround.

Context: I want to release my android app in several countries. Currently only available in Spain. I've read all android developers documentation about localization. It is pretty clear that all dynamic localized information on code should be based on "system locale" when presenting it to the user.

Problem: Problem comes when user changes language on settings manually and I want to display a price (generated by the app) that bases it's currency on system locale preference. For example lets say I live in Spain and I set up my device Language & input/Language to English(United States). When I do this my app shows prices in US dollars ($) but I want it to keep showing EUR (€) since I'm in Spain. How do I achieve this?

This is mi actual code for showing a "localized" price to the user based on system locale:

    Locale currentLocale = Locale.getDefault();
    priceFormat = NumberFormat.getInstance(currentLocale);
    localCurrency = Currency.getInstance(current);
    //...

    //doubleValueCost is independent from currency but the I want to add the proper currency symbol
    costTextView.setText(priceFormat.format(doubleValueCost) + " " + localCurrency.getSymbol());

This code will add the currency symbol for the system locale selected by user on preferences and not the actual currency symbol of the country where the device is.

Any advice is welcomed. Thanks in advance.

Community
  • 1
  • 1
JorgeMuci
  • 658
  • 9
  • 22

2 Answers2

3

After all the research I decided to use TelephonyManager simCountryIso to localize currency in the concrete scenario I mention in my problem. Not sure if it is the best solution to solve my proble but is the best option I've found. This would be the code to get the currency I need:

        String countryIso = telephonyManager.getSimCountryIso();
        localCurrency = Currency.getInstance(new Locale(countryIso,countryIso));
        //doubleValueCost is independent from currency but the I want to add the proper currency symbol ...
        costTextView.setText(priceFormat.format(doubleValueCost) + " " + localCurrency.getSymbol());

If anyone comes up with a better solution, I would be glad to hear. Thanks

JorgeMuci
  • 658
  • 9
  • 22
  • Looks like this is the best approach to use for this context. Using the phone default locale is not enough. I couldn't find a way to place it in Peruvian soles for instance, since the only two language options for spanish are United States (?) and Spain. I'm not sure about what to do with tablets though. – Bilthon Dec 09 '15 at 00:10
0

specifying the desired locale, for example this code to force the locale to Spain:

priceFormat = NumberFormat.getInstance(new Locale("es", "ES");

should do what you need.

Locale.getDefault() is loading the user defined locale

CSmith
  • 13,318
  • 3
  • 39
  • 42
  • This would solve the problem in the concrete example I gave. What I want to achieve must work for any country (general purpose) no only Spain. So if I use this code, first I need to know in which country is the user device located (which is kind of the actual problem). That leads me to [this](http://stackoverflow.com/a/19415296/2979970) type of answers based on simCountryIso or networkCountryISO ( from TelephonyManager) but that doesn't work always, so I wanted to know if there was a better way to do this. Because this is a quite common issue. Thanks anyway! – JorgeMuci Sep 11 '14 at 08:09