TextView Internationalization

TextView Internationalization

This is a warning.

Id

SetTextI18n

Summary

TextView Internationalization

Severity

Warning

Category

Internationalization

Platform

Android

Vendor

Android Open Source Project

Feedback

https://issuetracker.google.com/issues/new?component=192708

Since

1.5.0 (November 2015)

Affects

Kotlin and Java files

Editing

This check runs on the fly in the IDE editor

See

https://developer.android.com/guide/topics/resources/localization.html

Implementation

Source Code

Tests

Source Code

When calling TextView#setText

Example

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.

Suppressing

You can suppress false positives using one of the following mechanisms:

formatted by Markdeep 1.18