Android中阿拉伯文本中的阿拉伯数字 [英] Arabic number in Arabic text in Android

查看:108
本文介绍了Android中阿拉伯文本中的阿拉伯数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

我正在将我的应用移植到阿拉伯语语言环境.我有一些 getString() 参数如下:

I'm porting my app to Arabic locale. I have some getString() with parameters like:

getString(R.string.distance, distance)

where %1d km

where <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.

推荐答案

Google 的 bugtracker 中存在这样的问题:阿拉伯语中的阿拉伯数字,而不是印度-阿拉伯数字系统

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")
                    .....;
}

(以及围绕它的 Unicode 匹配的变体 (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆