在我的应用中,对于本地化我有一个values-ar
阿拉伯字符串文件夹。问题在于阿拉伯语字母在没有安装阿拉伯语的手机中显示为未连接。我使用ArabicReshape class加入阿拉伯字母,但由于字符串太多,我无法一个接一个地手动继续应用整形类。获取strings.xml中的所有字符串
我想知道我是否可以从strings.xml
文件中获得全部字符串,并通过循环遍历它们将该类应用于字符串。这可能吗?
在我的应用中,对于本地化我有一个values-ar
阿拉伯字符串文件夹。问题在于阿拉伯语字母在没有安装阿拉伯语的手机中显示为未连接。我使用ArabicReshape class加入阿拉伯字母,但由于字符串太多,我无法一个接一个地手动继续应用整形类。获取strings.xml中的所有字符串
我想知道我是否可以从strings.xml
文件中获得全部字符串,并通过循环遍历它们将该类应用于字符串。这可能吗?
我不明白你为什么需要遍历所有的字符串,而不是在你需要一个特定的字符串时调用类。
但是当你问...
我想这可能会帮助你;-)
Field[] fields = R.string.class.getFields();
for(final Field field : fields) {
String name = field.getName(); //name of string
try{
int id = field.getInt(R.string.class); //id of string
}catch (Exception ex) {
//do smth
}
}
什么你就要做的就是设置你的LOCALE到AR从而使资源管理器会让你从values-ar/strings.xml中取出字符串,然后使用反射R.string.class.getFields()得到一个Field的列表。然后遍历你的字符串,并进行重塑。
尝试像下面的代码片段:
Resources resources = getResources();
int resourceId = 0;
String resourceString;
Field[] stringFields = R.string.class.getFields();
for(Field stringField : stringFields) {
try {
resourceId = stringField.getInt(R.string.class);
} catch (IllegalArgumentException e) {
e.printStackTrace();
continue;
} catch (IllegalAccessException e) {
e.printStackTrace();
continue;
}
resourceString = resources.getString(resourceId);
if(resourceString != null && resourceString.length() > 0) {
// do your reshaping on the resoureString
}
}
我不认为这是可能的不幸...... –
@Orabîg抱歉让你们失望,但是这是可能的,看到的答案;-) –
好点:)没有想到使用反射。格拉茨... –