Skip to content

Commit 02fbc37

Browse files
authored
Ensure correct font weight for boost/fav count in all locales (#5105)
This never worked as intended when the font weight was defined inside the resources. Seems like Weblate is not dealing with that correctly. If there is `<b>%1$s</b>` used inside the resource, it gets stripped by `getQuantityString`. To work around that, it needs to be HTML escaped `&lt;b>%1$s&lt;/b>` and then passed to `fromHtml`. Since for some reason not even the source strings got the `&lt;b>%1$s&lt;/b>` correct everywhere, I'm changing it to a completely code based version that will always be right no matter what is defined in the resource.
1 parent ce4f0b4 commit 02fbc37

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

app/src/main/java/com/keylesspalace/tusky/adapter/StatusBaseViewHolder.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
import static com.keylesspalace.tusky.viewdata.PollViewDataKt.buildDescription;
44

55
import android.content.Context;
6+
import android.graphics.Typeface;
67
import android.graphics.drawable.BitmapDrawable;
78
import android.graphics.drawable.ColorDrawable;
89
import android.graphics.drawable.Drawable;
910
import android.net.Uri;
11+
import android.text.Spannable;
12+
import android.text.SpannableStringBuilder;
1013
import android.text.Spanned;
1114
import android.text.TextUtils;
1215
import android.text.format.DateUtils;
16+
import android.text.style.StyleSpan;
1317
import android.view.Gravity;
1418
import android.view.View;
1519
import android.view.ViewGroup;
@@ -22,10 +26,10 @@
2226
import androidx.annotation.DrawableRes;
2327
import androidx.annotation.NonNull;
2428
import androidx.annotation.Nullable;
29+
import androidx.annotation.PluralsRes;
2530
import androidx.appcompat.content.res.AppCompatResources;
2631
import androidx.appcompat.widget.TooltipCompat;
2732
import androidx.constraintlayout.widget.ConstraintLayout;
28-
import androidx.core.text.HtmlCompat;
2933
import androidx.recyclerview.widget.DefaultItemAnimator;
3034
import androidx.recyclerview.widget.LinearLayoutManager;
3135
import androidx.recyclerview.widget.RecyclerView;
@@ -980,14 +984,21 @@ private CharSequence getPollDescription(@NonNull StatusViewData.Concrete status,
980984

981985
@NonNull
982986
protected CharSequence getFavsText(@NonNull Context context, int count) {
983-
String countString = numberFormat.format(count);
984-
return HtmlCompat.fromHtml(context.getResources().getQuantityString(R.plurals.favs, count, countString), HtmlCompat.FROM_HTML_MODE_LEGACY);
987+
return getMetaDataText(context, R.plurals.favs, count);
985988
}
986989

987990
@NonNull
988991
protected CharSequence getReblogsText(@NonNull Context context, int count) {
992+
return getMetaDataText(context, R.plurals.reblogs, count);
993+
}
994+
995+
private CharSequence getMetaDataText(@NonNull Context context, @PluralsRes int text, int count) {
989996
String countString = numberFormat.format(count);
990-
return HtmlCompat.fromHtml(context.getResources().getQuantityString(R.plurals.reblogs, count, countString), HtmlCompat.FROM_HTML_MODE_LEGACY);
997+
String textString = context.getResources().getQuantityString(text, count, countString);
998+
SpannableStringBuilder sb = new SpannableStringBuilder(textString);
999+
int countIndex = textString.indexOf(countString);
1000+
sb.setSpan(new StyleSpan(Typeface.BOLD), countIndex, countIndex + countString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
1001+
return sb;
9911002
}
9921003

9931004
private void setupPoll(PollViewData poll, List<Emoji> emojis,

app/src/main/res/values/strings.xml

+4-6
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,13 @@
556556
<string name="failed_to_unpin">Failed to Unpin</string>
557557

558558
<plurals name="favs">
559-
<item quantity="zero"><b>%1$s</b> Favorites</item>
560-
<item quantity="one">&lt;b>%1$s&lt;/b> Favorite</item>
561-
<item quantity="other">&lt;b>%1$s&lt;/b> Favorites</item>
559+
<item quantity="one">%1$s Favorite</item>
560+
<item quantity="other">%1$s Favorites</item>
562561
</plurals>
563562

564563
<plurals name="reblogs">
565-
<item quantity="zero"><b>%1$s</b> Boosts</item>
566-
<item quantity="one">&lt;b>%1$s&lt;/b> Boost</item>
567-
<item quantity="other">&lt;b>%1$s&lt;/b> Boosts</item>
564+
<item quantity="one">%1$s Boost</item>
565+
<item quantity="other">%1$s Boosts</item>
568566
</plurals>
569567

570568
<string name="label_translating">Translating…</string>

0 commit comments

Comments
 (0)