This is a warning.
| |
TextView Internationalization | |
Warning | |
Internationalization | |
Android | |
Android Open Source Project | |
1.5.0 (November 2015) | |
Kotlin and Java files | |
This check runs on the fly in the IDE editor | |
https://developer.android.com/guide/topics/resources/localization.html | |
TextView#setText
Number#toString()
to format numbers; it will not handle
fraction separators and locale-specific digits properly. Consider
using String#format
with proper format specifications (%d
or
%f
) instead.
Here is an example of lint warnings produced by this check:
src/test/pkg/CustomScreen.java:13:Warning: String literal in setText can
not be translated. Use Android resources instead. [SetTextI18n]
view.setText("Hardcoded");
-----------
src/test/pkg/CustomScreen.java:17:Warning: Number formatting does not
take into account locale settings. Consider using String.format instead.
[SetTextI18n]
view.setText(Integer.toString(50) + "%");
--------------------
src/test/pkg/CustomScreen.java:18:Warning: String literal in setText can
not be translated. Use Android resources instead. [SetTextI18n]
view.setText(Double.toString(12.5) + " miles");
--------
src/test/pkg/CustomScreen.java:21:Warning: String literal in setText can
not be translated. Use Android resources instead. [SetTextI18n]
btn.setText("User " + getUserName());
-------
Here is the source file referenced above:
src/test/pkg/CustomScreen.java
:
package test.pkg;
import android.content.Context;
import android.widget.Button;
import android.widget.TextView;
class CustomScreen {
public CustomScreen(Context context) {
TextView view = new TextView(context);
// Should fail - hardcoded string
view.setText("Hardcoded");
// Should pass - no letters
view.setText("-");
// Should fail - concatenation and toString for numbers.
view.setText(Integer.toString(50) + "%");
view.setText(Double.toString(12.5) + " miles");
Button btn = new Button(context);
btn.setText("User " + getUserName());
btn.setText(String.format("%s of %s users", Integer.toString(5), Integer.toString(10)));
view.setText("");
}
private static String getUserName() {
return "stub";
}
}
You can also visit the source code for the unit tests for this check to see additional scenarios.
The above example was automatically extracted from the first unit test
found for this lint check, SetTextDetector.test
.
To report a problem with this extracted sample, visit
https://issuetracker.google.com/issues/new?component=192708.
You can suppress false positives using one of the following mechanisms:
// Kotlin
@Suppress("SetTextI18n")
fun method() {
setText(...)
}
or
// Java
@SuppressWarnings("SetTextI18n")
void method() {
setText(...);
}
//noinspection SetTextI18n
problematicStatement()
lint.xml
file in the source tree which turns off
the check in that folder and any sub folder. A simple file might look
like this:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="SetTextI18n" severity="ignore" />
</lint>
Instead of ignore
you can also change the severity here, for
example from error
to warning
. You can find additional
documentation on how to filter issues by path, regular expression and
so on
here.
lintOptions {
disable 'SetTextI18n'
}
In Android projects this should be nested inside an android { }
block.
lint
, using the --ignore
flag:
$ lint --ignore SetTextI18n ...`