Android中阿拉伯文字中的阿拉伯数字 [英] Arabic number in Arabic text in Android
问题描述
编辑
我要将我的应用程序移植到阿拉伯语言环境.我有一些参数类似的getString():
I'm porting my app to Arabic locale. I have some getString() with parameters like:
getString(R.string.distance, distance)
其中<string name="distance">%1d km</string>
要求是阿拉伯语我应该这样显示:"2.3كم".
The requirement is that in Arabic I should show it like this: "2.3 كم".
如果我将其设置为沙特阿拉伯(国家="sa")或阿联酋(国家="ae")的语言环境,则该数字以东阿拉伯语显示,但我的客户希望以西阿拉伯语显示.
If I set as the locale for Saudi Arabia (country = "sa") or UAE (country = "ae") the number are shown in Eastern-Arabic but my client wants them in Western-Arabic.
在 此处 使用的解决方案埃及是当地的一个国家,但对我而言这是不可能的.
The solution here is to use Egypt as a country in the locale but this is not possible for me.
我尝试过:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void setAppContextLocale(Locale savedLocale) {
Locale.Builder builder = new Locale.Builder();
builder.setLocale(savedLocale).setExtension(Locale.UNICODE_LOCALE_EXTENSION, "nu-latn");
Locale locale = builder.build();
Configuration config = new Configuration();
config.locale = locale;
config.setLayoutDirection(new Locale(savedLocale.getLanguage()));
mAppContext.getResources().updateConfiguration(config, mContext.getResources().getDisplayMetrics());
}
如此问题中所建议,但是此后会忽略该国家/地区,因此SA和AE语言环境都将使用默认文件中的字符串.
as suggested in this question but after that the country is ignored so both SA and AE locales use the strings in the default file.
推荐答案
There's such issue in Google's bugtracker: Arabic numerals in arabic language intead of Hindu-Arabic numeral system
如果特别是埃及语言环境由于某些客户的问题而无法工作(我能理解),则可以将字符串格式化为任何其他西方语言环境.例如:
If particularly Egypt locale doesn't work due to some customer's issue(I can understand it), then you can format your string to any other western locales. For example:
NumberFormat nf = NumberFormat.getInstance(new Locale("en","US")); //or "nb","No" - for Norway
String sDistance = nf.format(distance);
distanceTextView.setText(String.format(getString(R.string.distance), sDistance));
如果使用新Locale
的解决方案根本不起作用,则有一个丑陋的解决方法:
If solution with new Locale
doesn't work at all, there's an ugly workaround:
public String replaceArabicNumbers(String original) {
return original.replaceAll("١","1")
.replaceAll("٢","2")
.replaceAll("٣","3")
.....;
}
(及其周围的变体以及Unicodes匹配(U + 0661,U + 0662,...).请参阅更多类似的想法
(and variations around it with Unicodes matching (U+0661,U+0662,...). See more similar ideas here)
更新1: 为了避免到处一一调用格式化字符串,我建议创建一个微小的Tool方法:
Upd1: To avoid calling formatting strings one by one everywhere, I'd suggest to create a tiny Tool method:
public final class Tools {
static NumberFormat numberFormat = NumberFormat.getInstance(new Locale("en","US"));
public static String getString(Resources resources, int stringId, Object... formatArgs) {
if (formatArgs == null || formatArgs.length == 0) {
return resources.getString(stringId, formatArgs);
}
Object[] formattedArgs = new Object[formatArgs.length];
for (int i = 0; i < formatArgs.length; i++) {
formattedArgs[i] = (formatArgs[i] instanceof Number) ?
numberFormat.format(formatArgs[i]) :
formatArgs[i];
}
return resources.getString(stringId, formattedArgs);
}
}
....
distanceText.setText(Tools.getString(getResources(), R.string.distance, 24));
或者覆盖默认的TextView
并在setText(CharSequence text, BufferType type)
public class TextViewWithArabicDigits extends TextView {
public TextViewWithArabicDigits(Context context) {
super(context);
}
public TextViewWithArabicDigits(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(replaceArabicNumbers(text), type);
}
private String replaceArabicNumbers(CharSequence original) {
if (original != null) {
return original.toString().replaceAll("١","1")
.replaceAll("٢","2")
.replaceAll("٣","3")
....;
}
return null;
}
}
我希望这会有所帮助
这篇关于Android中阿拉伯文字中的阿拉伯数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!