我想更改安卓应用程序本地化阿拉伯语 - 英语。
但是当我将语言更改为阿拉伯语时,它会将所有数字更改为阿拉伯语,因此应用程序崩溃了,我想将语言更改为阿拉伯语并防止将数字语言从英语更改为阿拉伯语。
Locale locale = new Locale(AppConfig.Language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = "ar";
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.activity_main);
当我想使用GPS获取位置时,它是阿拉伯语的返回号码我如何防止它更改数字语言?
这个答案为时已晚,但它可以帮助将来的某人。我为此苦苦挣扎了几天,但我找到了一个简单的解决方案。只需将国家/地区设置为第二个参数,因为一些国家使用阿拉伯数字,而其他国家/地区使用所谓的印度数字
Locale locale = new Locale(LanguageToLoad,"MA");//For Morocco to use 0123...
或
Locale locale = new Locale(LanguageToLoad,"SA");//For Saudi Arabia to use ٠١٢٣...
在这里成立有一个补充,因此您不必更改整个代码。
谷歌的错误跟踪器中存在这样的问题:阿拉伯语中的阿拉伯数字与印度教 - 阿拉伯数字系统不同
如果由于某些客户的问题(我可以理解)而导致埃及区域设置不起作用,那么您可以将字符串格式化为任何其他西方语言环境。例如:
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
的解决方案根本不起作用,则有一个丑陋的解决方法:
public String replaceArabicNumbers(String original) {
return original.replaceAll("١","1")
.replaceAll("٢","2")
.replaceAll("٣","3")
.....;
}
(以及它周围的Unicode匹配(U + 0661,U + 0662,...)的变化)。在此处查看更多类似的想法)
更新1:为了避免在任何地方一个接一个地调用格式化字符串,我建议创建一个很小的 Tool 方法:
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;
}
}
我希望,它有帮助
可以为应用程序中扩展它的单个TextView
或元素设置区域设置。
有关更多信息,请参阅 http://developer.android.com/reference/android/widget/TextView.html#setTextLocale(java.util.Locale)
更新
您可以使用以下方法将数字解析为所需的
区域设置
public static String nFormate(double d) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setMaximumFractionDigits(10);
String st= nf.format(d);
return st;
}
然后你可以再次将数字解析为双倍
最好和最简单的方法是在所有本地化字符串中保持所有字符串文件中的数字不变。或者您需要将每个数字字符串转换为数字
我遇到了同样的问题,解决方案是用空字符串连接数字变量。例如像这样:
public void displayPoints(int:points){
TextView scoreA = findViewById(R.id.score_id);
scoreA.setText(""+points);
}
我用了这个
scoreA.setText(""+points);
而不是这个
scoreA.setText(String.format("%d",points));
这甚至会给您一个警告,即硬编码文本无法正确翻译成其他语言,这正是我们在这里想要的 :) .