Когда вы разрабатываете многоязычное приложение, лучший способ обработать переводы в Android - это определить контент в категории локали. Позвольте мне объяснить это вам
1-й шаг
создать каталог с указанными именами локалей values- (language) например, values-ar
MyProject/
res/
values-es/
strings.xml
values-ar/
strings.xml
Значения-эс / 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>
значения ар / strings.xml
<resources>
<string name="ayah1">الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ</string>
<string name="ayah2">الرَّحْمَنِ الرَّحِيمِ</string>
</resources>
2-й шаг теперь используйте эти строки в ваших файлах просмотра как
<?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();
}
}