我们的应用程序支持不同的语言环境。其中有一个字符串,中间有两个可替换的值(带货币符号的金额,USD$XXXXX)。但是,当语言环境为阿拉伯语时,会出现奇怪的行为。文本方向在文本超过一行时会改变。只有第一行的文本是正确的,而其他行的格式会被某些内容覆盖!如您在屏幕截图中所见,绿色的行是正确的,而红色的行是错误的。
到目前为止,我尝试过以下方法:
- BidFormatter
- Unicode
问题在于,在使用双向文本格式后,第一个数字是正确的,但第二个数字不正确。
而在使用BidiFormat和Unicode后,所有数字都没问题,但当文本变得很长并且变成多行时,只有第一行是正确的,其他行再次出现错误。
对于Unicode,我查看了:Unicode® Standard Annex #9 UNICODE BIDIRECTIONAL ALGORITHM(如果您只对主要内容感兴趣,可以直接查看此部分)
您可以查看这个repo:Github链接
以下是我用于快速参考的代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setTextViewTexts(R.id.tvLtrOneLine, formatWithCurrency(), R.string.text_short_english)
setTextViewTexts(R.id.tvLtrTwoLines, formatWithCurrency(), R.string.text_long_english)
setTextViewTexts(R.id.tvRtlOneLine, formatWithCurrency(), R.string.text_short_arabic)
setTextViewTexts(R.id.tvRtlOneLineBidi, bidiFormatter(formatWithCurrency()), R.string.text_short_arabic)
setTextViewTexts(R.id.tvRtlOneLineRtlFormatter, rtlMaker(formatWithCurrency()), R.string.text_short_arabic)
setTextViewTexts(R.id.tvRtlTwoLines, formatWithCurrency(), R.string.text_long_arabic)
setTextViewTexts(R.id.tvRtlTwoLinesBidi, bidiFormatter(formatWithCurrency()), R.string.text_long_arabic)
setTextViewTexts(R.id.tvRtlTwoLinesRtlFormatter, rtlMaker(formatWithCurrency()), R.string.text_long_arabic)
}
private fun setTextViewTexts(textViewId: Int, text: String, stringResource: Int) {
findViewById<TextView>(textViewId).text = getString(stringResource, text, text)
}
private fun formatWithCurrency(): String {
val currency = "USD$"
val price = 200
val priceBuilder = StringBuilder("")
priceBuilder.append(currency)
priceBuilder.append(getDecimalFormattedPrice(price))
return priceBuilder.toString()
}
private fun getDecimalFormattedPrice(price: Int): String {
return DecimalFormat("0.00").format(price)
}
private fun rtlMaker(text: String): String {
return "\u2066" + bidiFormatter(text) + "\u2069"
}
private fun bidiFormatter(text: String): String {
return BidiFormatter.getInstance().unicodeWrap(text)
}
这是一个安卓的bug还是有解决方法?
要查看这个bug,请下载存储库并在任何设备上运行它,并将设备语言更改为阿拉伯语(埃及)
编辑: 我提交了错误报告