我正在我的android项目中使用xml,我是初学者。我也有不同语言的xml
<sura index="1" name="الفاتحة">
<aya index="1" text="الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ" bismillah="بِسْمِ اللَّهِ الرَّحْمَنِ
الرَّحِيمِ" />
<aya index="2" text="الرَّحْمَنِ الرَّحِيمِ" />
<aya index="3" text="مَالِكِ يَوْمِ الدِّينِ" />
<aya index="4" text="إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ" />
<aya index="5" text="اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ" />
<aya index="6" text="صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ" />
</sura>
这个是阿拉伯语
<sura index="1" name="الفاتحة">
<aya index="1" text="In the name of Allah, most benevolent, ever-merciful."/>
<aya index="2" text="ALL PRAISE BE to Allah, Lord of all the worlds,"/>
<aya index="3" text="Most beneficent, ever-merciful,"/>
<aya index="4" text="King of the Day of Judgement."/>
<aya index="5" text="You alone we worship, and to You alone turn for help."/>
<aya index="6" text="Guide us (O Lord) to the path that is straight,"/>
<aya index="7" text="The path of those You have blessed, Not of those who have earned Your anger, nor those who have gone astray."/>
</sura>
这个用英文写的。
我已成功使用此代码解析阿拉伯语中的aya
private List<String> getAllAyaFromSuraIndex(String suraIndex) throws XmlPullParserException, IOException {
list = new ArrayList<>();
XmlPullParser parser = getResources().getXml(R.xml.quran_arabic);
while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {
if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("sura")) {
String index = parser.getAttributeValue(0);
// Match given sure index
if (index.equals(suraIndex)) {
// Move to first aya tag inside matched sura index
parser.next();
// This loop will exit when it reaches sura end tag </sura>
while (parser.getName().equals("aya")) {
if (parser.getEventType() == XmlPullParser.START_TAG) {
list.add(parser.getAttributeValue(null,"index") + ".\n" + parser.getAttributeValue(null,"text"));
}
// Move to next aya tag
parser.next();
}
break;
}
}
parser.next();
}
return list;
}
我有一个listview,有两个textviews,一个用于阿拉伯语,一个用于英语。我尝试了一切,但没有得到结果。
在开发多语言应用程序时,在android中处理翻译的最佳方法是在语言环境类别中定义内容。让我解释一下你
第1步创建目录,其中语言环境名称提到值 - (语言),例如values-ar
MyProject/
res/
values-es/
strings.xml
values-ar/
strings.xml
值-ES / strings.xml中
<resources>
<string name="ayah1">ALL PRAISE BE to Allah, Lord of all the worlds,.</string>
<string name="ayah2">Most beneficent, ever-merciful,</string>
</resources>
值-AR / strings.xml中
<resources>
<string name="ayah1">الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ</string>
<string name="ayah2">الرَّحْمَنِ الرَّحِيمِ</string>
</resources>
第二步现在在视图文件中使用这些字符串
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.javapapers.multilingualapp.MainActivity">
<TextView
android:id="@+id/string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ayah1" //here the values being used
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
现在,只要您想要更改语言,只需使用以下功能更改语言环境即可进行第3步和最后一步
public void setLocale(String localeName) {
if (!localeName.equals(currentLanguage)) {
myLocale = new Locale(localeName);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, MainActivity.class);
refresh.putExtra(currentLang, localeName);
startActivity(refresh);
} else {
Toast.makeText(MainActivity.this, "Language already selected!", Toast.LENGTH_SHORT).show();
}
}