Skip to content

Commit 2885159

Browse files
authored
Merge pull request #17217 from wordpress-mobile/fix/20.8-western-arabic-numerals
Update more in-app Stats texts to always use western arabic numerals
2 parents 0e6eed6 + 32cf991 commit 2885159

6 files changed

Lines changed: 36 additions & 23 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
-----
55
* [*] Updates splash screen for Android 12+ [https://github.com/wordpress-mobile/WordPress-Android/pull/17273]
66
* [*] Fix text color of success messages in the QR code login flow [https://github.com/wordpress-mobile/WordPress-Android/pull/17286]
7+
* [*] Stats: Fix Western Arabic Numerals not being shown on every text of the stats screens in Arabic languages [https://github.com/wordpress-mobile/WordPress-Android/pull/17217]
78

89
20.9
910
-----

WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/insights/usecases/ViewsAndVisitorsMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.wordpress.android.ui.stats.refresh.utils.MILLION
2323
import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
2424
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
2525
import org.wordpress.android.ui.utils.ListItemInteraction
26+
import org.wordpress.android.util.extensions.enforceWesternArabicNumerals
2627
import org.wordpress.android.viewmodel.ResourceProvider
2728
import javax.inject.Inject
2829

@@ -117,7 +118,7 @@ class ViewsAndVisitorsMapper
117118
val value = it.getValue(selectedType)
118119
val date = statsDateFormatter.parseStatsDate(statsGranularity, it.period)
119120
Line(
120-
statsDateFormatter.printDayWithoutYear(date),
121+
statsDateFormatter.printDayWithoutYear(date).enforceWesternArabicNumerals() as String,
121122
it.period,
122123
value.toInt()
123124
)

WordPress/src/main/java/org/wordpress/android/util/extensions/StringExtensions.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package org.wordpress.android.util.extensions
22

33
import android.annotation.SuppressLint
4+
import android.text.SpannableString
5+
import android.text.Spanned
6+
import android.text.TextUtils
47
import java.util.Locale
58

69
/**
@@ -17,3 +20,26 @@ import java.util.Locale
1720
fun String.capitalizeWithLocaleWithoutLint(locale: Locale): String {
1821
return this.capitalize(locale)
1922
}
23+
24+
/**
25+
* Converts digits to Western Arabic -- Workaround for an issue in Android that shows Eastern Arabic numerals.
26+
* There is an issue in Google's bug tracker for this: [SO Answer](https://stackoverflow.com/a/34612487/4129245).
27+
* The returned String uses the default Locale.
28+
* @return a String with numerals in Western Arabic format persisting spans if available.
29+
*/
30+
fun CharSequence.enforceWesternArabicNumerals(): CharSequence {
31+
val textWithArabicNumerals = this
32+
// Replace Eastern Arabic numerals
33+
.replace(Regex("[٠-٩]")) { match -> (match.value.single() - '٠').toString() }
34+
// Replace Persian/Urdu numerals
35+
.replace(Regex("[۰-۹]")) { match -> (match.value.single() - '۰').toString() }
36+
37+
// Restore spans if text is an instance of Spanned
38+
if (this is Spanned) {
39+
val spannableText = SpannableString(textWithArabicNumerals)
40+
TextUtils.copySpansFrom(this, 0, this.length, null, spannableText, 0)
41+
return spannableText
42+
}
43+
44+
return textWithArabicNumerals
45+
}
Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.wordpress.android.widgets
22

33
import android.content.Context
4-
import android.text.SpannableString
5-
import android.text.Spanned
6-
import android.text.TextUtils
74
import android.util.AttributeSet
85
import com.google.android.material.textview.MaterialTextView
6+
import org.wordpress.android.util.extensions.enforceWesternArabicNumerals
97

108
/**
119
* MaterialTextView which enforces Western Arabic numerals.
@@ -18,21 +16,4 @@ class MaterialTextViewWithNumerals @JvmOverloads constructor(
1816
override fun setText(text: CharSequence?, type: BufferType?) {
1917
super.setText(text?.enforceWesternArabicNumerals(), type)
2018
}
21-
22-
private fun CharSequence.enforceWesternArabicNumerals(): CharSequence {
23-
val textWithArabicNumerals = this
24-
// Replace Eastern Arabic numerals
25-
.replace(Regex("[٠-٩]")) { match -> (match.value.single() - '٠').toString() }
26-
// Replace Persian/Urdu numerals
27-
.replace(Regex("[۰-۹]")) { match -> (match.value.single() - '۰').toString() }
28-
29-
val spannableText = SpannableString(textWithArabicNumerals)
30-
31-
// Restore spans if text is an instance of Spanned
32-
if (this is Spanned) {
33-
TextUtils.copySpansFrom(this, 0, this.length, null, spannableText, 0)
34-
}
35-
36-
return spannableText
37-
}
3819
}

WordPress/src/main/res/layout/stats_block_text_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:paddingStart="@dimen/margin_extra_large"
99
android:paddingEnd="@dimen/margin_extra_large">
1010

11-
<com.google.android.material.textview.MaterialTextView
11+
<org.wordpress.android.widgets.MaterialTextViewWithNumerals
1212
android:id="@+id/text"
1313
style="@style/StatsBlockText"
1414
android:layout_width="wrap_content"

WordPress/src/main/res/layout/stats_block_value_item.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
android:paddingStart="@dimen/margin_extra_large"
99
android:paddingTop="@dimen/margin_extra_large"
1010
android:paddingEnd="@dimen/margin_extra_large"
11+
xmlns:tools="http://schemas.android.com/tools"
1112
android:paddingBottom="8dp">
1213

1314
<org.wordpress.android.widgets.MaterialTextViewWithNumerals
@@ -38,5 +39,8 @@
3839
android:layout_gravity="bottom"
3940
android:textColor="?attr/wpColorSuccess"
4041
android:textSize="16sp"
41-
android:visibility="gone" />
42+
android:visibility="gone"
43+
tools:text="-7 (-100%)"
44+
android:layoutDirection="ltr"
45+
tools:visibility="visible" />
4246
</LinearLayout>

0 commit comments

Comments
 (0)