Android: Handling Integer (%1$d) And String (%1$s) Arguments For Right To Left Languages Like Arabic
My strings.xml has a few strings in the following format No new message in last %1$d days. I use them in Java code as follows: String.format(getString(R.string.msg_str), 3); And I
Solution 1:
I took the text from your question and pasted it into my strings.xml
file:
When I run my app, I get a crash right away. All it does is:
TextView textView = (TextView) findViewById(R.id.text);
textView.setText(getString(R.string.formatting, 6));
This is the crash:
E/AndroidRuntime( 5739): Caused by: java.util.UnknownFormatConversionException: Conversion: أ
E/AndroidRuntime( 5739): at java.util.Formatter$FormatToken.unknownFormatConversionException(Formatter.java:1399)
E/AndroidRuntime( 5739): at java.util.Formatter$FormatToken.checkFlags(Formatter.java:1336)
E/AndroidRuntime( 5739): at java.util.Formatter.transform(Formatter.java:1442)
E/AndroidRuntime( 5739): at java.util.Formatter.doFormat(Formatter.java:1081)
E/AndroidRuntime( 5739): at java.util.Formatter.format(Formatter.java:1042)
E/AndroidRuntime( 5739): at java.util.Formatter.format(Formatter.java:1011)
E/AndroidRuntime( 5739): at java.lang.String.format(String.java:1999)
Next, I highlighted the format argument (d$1%
), deleted it, and retyped it (by pressing %
1
$
d
). My string now looks like this:
Now when I run my app, it works just fine:
Since the two strings look identical to me, I assume that there are some non-printing characters in the first one that relate to text direction. These non-printing characters are probably confusing the parsing of the string's format arguments.
Post a Comment for "Android: Handling Integer (%1$d) And String (%1$s) Arguments For Right To Left Languages Like Arabic"