Skip to content

Commit 0c5dfc9

Browse files
committed
Extract CharSequence.enforceWesternArabicNumerals
1 parent 6388390 commit 0c5dfc9

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

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+
}

WordPress/src/main/java/org/wordpress/android/widgets/MaterialTextViewWithNumerals.kt

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.text.Spanned
66
import android.text.TextUtils
77
import android.util.AttributeSet
88
import com.google.android.material.textview.MaterialTextView
9+
import org.wordpress.android.util.extensions.enforceWesternArabicNumerals
910

1011
/**
1112
* MaterialTextView which enforces Western Arabic numerals.
@@ -18,21 +19,4 @@ class MaterialTextViewWithNumerals @JvmOverloads constructor(
1819
override fun setText(text: CharSequence?, type: BufferType?) {
1920
super.setText(text?.enforceWesternArabicNumerals(), type)
2021
}
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-
}
3822
}

0 commit comments

Comments
 (0)