Skip to content

Commit

Permalink
Make it possible to translate accessibility content descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteHamster committed Feb 15, 2025
1 parent 0c41bdd commit f02cda4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ And react to search results in your Activity:
## Translations

This library currently contains only a limited number of translations. If you want to translate
the texts shown by the library together with your app's other strings, you can just override
the strings defined in `lib/src/main/res/values/strings.xml` in your own application by copying
those lines to your app's `strings.xml`.
the texts shown by the library together with your app's other strings, you can override
the strings in the preference xml file using attributes like `search:textNoResults`.
Refer to [`attrs.xml`](lib/src/main/res/values/attrs.xml) for details.
You can also overwrite the strings when constructing the SearchConfiguration object.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class SearchConfiguration {
private static final String ARGUMENT_TEXT_HINT = "text_hint";
private static final String ARGUMENT_TEXT_CLEAR_HISTORY = "text_clear_history";
private static final String ARGUMENT_TEXT_NO_RESULTS = "text_no_results";
private static final String ARGUMENT_TEXT_CLEAR_INPUT = "text_clear_input";
private static final String ARGUMENT_TEXT_MORE = "text_more";

private ArrayList<SearchIndexItem> filesToIndex = new ArrayList<>();
private ArrayList<PreferenceItem> preferencesToIndex = new ArrayList<>();
Expand All @@ -43,6 +45,8 @@ public class SearchConfiguration {
private String textClearHistory;
private String textNoResults;
private String textHint;
private String textClearInput;
private String textMore;

SearchConfiguration() {

Expand Down Expand Up @@ -87,6 +91,8 @@ private Bundle toBundle() {
arguments.putString(ARGUMENT_TEXT_HINT, textHint);
arguments.putString(ARGUMENT_TEXT_CLEAR_HISTORY, textClearHistory);
arguments.putString(ARGUMENT_TEXT_NO_RESULTS, textNoResults);
arguments.putString(ARGUMENT_TEXT_CLEAR_INPUT, textClearInput);
arguments.putString(ARGUMENT_TEXT_MORE, textMore);
arguments.putString(ARGUMENT_HISTORY_ID, historyId);
return arguments;
}
Expand All @@ -103,6 +109,8 @@ static SearchConfiguration fromBundle(Bundle bundle) {
config.textHint = bundle.getString(ARGUMENT_TEXT_HINT);
config.textClearHistory = bundle.getString(ARGUMENT_TEXT_CLEAR_HISTORY);
config.textNoResults = bundle.getString(ARGUMENT_TEXT_NO_RESULTS);
config.textClearInput = bundle.getString(ARGUMENT_TEXT_CLEAR_INPUT);
config.textMore = bundle.getString(ARGUMENT_TEXT_MORE);
config.historyId = bundle.getString(ARGUMENT_HISTORY_ID);
return config;
}
Expand Down Expand Up @@ -298,6 +306,22 @@ public void setTextHint(String textHint) {
this.textHint = textHint;
}

public String getTextClearInput() {
return textClearInput;
}

public void setTextClearInput(String textClearInput) {
this.textClearInput = textClearInput;
}

public String getTextMore() {
return textMore;
}

public void setTextMore(String textMore) {
this.textMore = textMore;
}

/**
* Adds a given R.xml resource to the search index
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ private void parseAttrs(AttributeSet attrs) {
searchConfiguration.setTextNoResults(a.getText(0).toString());
}
a.recycle();
a = getContext().obtainStyledAttributes(attrs, new int[] {R.attr.textClearInput});
if (a.getText(0) != null) {
searchConfiguration.setTextClearInput(a.getText(0).toString());
}
a.recycle();
a = getContext().obtainStyledAttributes(attrs, new int[] {R.attr.textMore});
if (a.getText(0) != null) {
searchConfiguration.setTextMore(a.getText(0).toString());
}
a.recycle();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
if (searchConfiguration.getTextNoResults() != null) {
viewHolder.noResults.setText(searchConfiguration.getTextNoResults());
}
if (searchConfiguration.getTextClearInput() != null) {
viewHolder.clearButton.setContentDescription(searchConfiguration.getTextClearInput());
}
if (searchConfiguration.getTextMore() != null) {
viewHolder.moreButton.setContentDescription(searchConfiguration.getTextMore());
}
viewHolder.moreButton.setOnClickListener(v -> {
PopupMenu popup = new PopupMenu(getContext(), viewHolder.moreButton);
popup.getMenuInflater().inflate(R.menu.searchpreference_more, popup.getMenu());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:layout_marginStart="8dp"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp"
android:contentDescription="@string/searchpreference_history_entry"
android:importantForAccessibility="no"
app:tint="?android:attr/textColorPrimary"
app:srcCompat="@drawable/searchpreference_ic_history" />

Expand Down
2 changes: 2 additions & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<attr name="textHint" format="string" />
<attr name="textNoResults" format="string" />
<attr name="textClearHistory" format="string" />
<attr name="textClearInput" format="string" />
<attr name="textMore" format="string" />
</declare-styleable>
</resources>
1 change: 0 additions & 1 deletion lib/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<resources>
<string name="searchpreference_search">Search…</string>
<string name="searchpreference_clear">Clear</string>
<string name="searchpreference_history_entry">History entry</string>
<string name="searchpreference_more">More</string>
<string name="searchpreference_clear_history">Clear history</string>
<string name="searchpreference_no_results">No results</string>
Expand Down

0 comments on commit f02cda4

Please sign in to comment.