74

I originally called String.format this way:

return String.format("%s %f %f", anotherString, doubleA, doubleB);

Which made Android Lint generate this warning:

Implicitly using the default locale is a common source of bugs: Use String.format(Locale, ...) instead

So I changed it to use Locale.US explicitly, based on what I read at http://developer.android.com/reference/java/util/Locale.html under the "Be wary of the default locale" section:

return String.format(Locale.US, "%s %f %f", anotherString, doubleA, doubleB);

Why does Android Lint still generate the same warning? I have to clean the project in Eclipse to get rid of it, when most warnings just disappear as soon as the offending line is fixed. I'm not sure if I'm doing something wrong or not.

Nobody Special
  • 1,255
  • 1
  • 10
  • 14
  • 1
    Lint warning isn't error at all. It's only trying to tell you about performance, translation and similar issue. I also get this warning in apps which I want to target only on certain language but, you can always disable lint by going to Window>Preferences>Android>Lint Error Checking. I believe in worst situation there might be device which won't have Locale.US available at all and therefore it can cause some issues. I haven't faced any of such until now though. – Milan Jun 05 '13 at 03:05

4 Answers4

66

Cleaning and rebuilding the project should work.

BTW, you may want to use Locale.getDefault() to "take care" of texts not written in english.

Alejandro Colorado
  • 6,034
  • 2
  • 28
  • 39
  • 8
    He's specifically trying to avoid the default. – nitind Jun 04 '13 at 23:23
  • 5
    As to where I understood, he tries to avoid it just because of the "Be wary of the default locale" section in the documentation. In spite of that, I'm telling him that he "may want to use" it, as it really tells that you should be aware of using the default locale in machine to machine communication. – Alejandro Colorado Jun 05 '13 at 07:41
  • 4
    I always use Locale.ROOT for this case. And cleaning will get rid of the lint warning, as Alejandro stated. – suomi35 Jan 09 '14 at 06:29
  • 1
    FIY public static String format(String format, Object... args) { return format(Locale.getDefault(), format, args); } – Manza Jul 13 '16 at 10:00
17

when I mentioned the locale with the format, the lint warning just vanished.

String.format(Locale.US,"%02d", selectedInt);

Pratheesh
  • 764
  • 1
  • 11
  • 24
12

Implied default locale in case conversion

Calling String#toLowerCase() or #toUpperCase() without specifying an explicit locale is a common source of bugs. The reason for that is that those methods will use the current locale on the user's device, and even though the code appears to work correctly when you are developing the app, it will fail in some locales. For example, in the Turkish locale, the uppercase replacement for i is not I.

If you want the methods to just perform ASCII replacement, for example to convert an enum name, call String#toUpperCase(Locale.US) instead. If you really want to use the current locale, call String#toUpperCase(Locale.getDefault()) instead.

http://developer.android.com/reference/java/util/Locale.html#default_locale

Paulo Mendonça
  • 635
  • 12
  • 28
3

Simply add Your Locale; for English Locale,

return String.format(Locale.ENGLISH,"%s %f %f", anotherString, doubleA, doubleB);

W Roberts
  • 31
  • 4