2

All I really want to do is format a date using strftime("%x") in the right order. On most platforms a call to setlocale("") is enough. On Android I keep getting !@#$ US dates.

So, does Android not support locales?

david.pfx
  • 10,520
  • 3
  • 30
  • 63

2 Answers2

2

No. setlocale() and strftime() does not work in Android NDK, except if en_US layout is what you want.

I get the local language from the Java part by:

pLocaleData->pLocaleLanguage = new Locale("en").getDefault().getLanguage();

And the ANSI C localeconv() does not work in Android NDK:

struct lconv *localeconv(void);

Also the standard Unix way of getting user name, does not work in Android.

char *p = getenv("USER");

So you need to use the Java side to get the locale information.

String LocaleCountry = new Locale("en").getDefault().getCountry();
String LocaleLanguage = new Locale("en").getDefault().getLanguage();
String LocaleCurrency = new DecimalFormatSymbols().getCurrency().toString();
String LocaleShortDateTimePattern =new SimpleDateFormat().toPattern();
char LocaleDecimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
char LocaleThousandSeparator = new DecimalFormatSymbols().getGroupingSeparator();
String LocaleThousandGrouping = 
        String.format("%d;0", new DecimalFormat().getMaximumFractionDigits());
String LocaleNegativePrefix = new DecimalFormat().getNegativePrefix();
String LocaleNegativeSuffix =new DecimalFormat().getNegativeSuffix();
String Operator = Utils.getQwnername(context);
Keale
  • 3,924
  • 3
  • 29
  • 46
Jan Bergström
  • 736
  • 6
  • 15
  • Looks to be a major Android Java/Android implementation (phones) bug on the thousand separator issue. The = new DecimalFormatSymbols().getGroupingSeparator(); generates 0xa0 (ISO/IEC 8859-1 NBSP, not a UTF8) instead of 0x20 space separator for ISO standard/Germany-Sweden etc (non-US) thousand grouping. This causes the Java code to crash if used. To be used the 0xa0 has to be changed to 0x20 in the Java code after reading 0xa0 = new DecimalFormatSymbols().getGroupingSeparator(); See also http://stackoverflow.com/questions/32374466/replace-grouping-separator-of-decimalformat-in-formatted-value. – Jan Bergström Dec 04 '15 at 15:46
  • Is this answer still valid in 2022 ? – Lothar Jun 26 '22 at 19:07
1

According to this the answer is No.

There is no support for locales in the C library / from native code, and this is intentional. As Elliot pointed out, your only hope is to use JNI to get relevant values.

david.pfx
  • 10,520
  • 3
  • 30
  • 63