mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-16 10:56:50 -04:00
#1738 Android OS language changes are ignored - add device default case to respect device settings
This commit is contained in:
parent
d0f14172e6
commit
578f73a5d1
@ -28,7 +28,7 @@
|
||||
<activity android:name=".intro.IntroActivity" />
|
||||
<activity
|
||||
android:name=".main.KiwixMainActivity"
|
||||
android:configChanges="orientation|keyboardHidden|screenSize|locale"
|
||||
android:configChanges="orientation|keyboardHidden|screenSize"
|
||||
android:label="@string/app_name"
|
||||
android:launchMode="singleTop"
|
||||
android:windowSoftInputMode="adjustPan">
|
||||
|
@ -1404,8 +1404,7 @@ public abstract class CoreMainActivity extends BaseActivity
|
||||
break;
|
||||
case REQUEST_PREFERENCES:
|
||||
if (resultCode == RESULT_RESTART) {
|
||||
startActivity(Intents.internal(CoreMainActivity.class));
|
||||
finish();
|
||||
recreate();
|
||||
}
|
||||
if (resultCode == RESULT_HISTORY_CLEARED) {
|
||||
webViewList.clear();
|
||||
|
@ -44,6 +44,7 @@ import java.util.Locale;
|
||||
import javax.inject.Inject;
|
||||
import kotlin.Unit;
|
||||
import kotlin.io.FilesKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.kiwix.kiwixmobile.core.CoreApp;
|
||||
import org.kiwix.kiwixmobile.core.NightModeConfig;
|
||||
import org.kiwix.kiwixmobile.core.R;
|
||||
@ -119,25 +120,20 @@ public abstract class CorePrefsFragment extends PreferenceFragment implements
|
||||
|
||||
protected void setUpLanguageChooser(String preferenceId) {
|
||||
ListPreference languagePref = (ListPreference) findPreference(preferenceId);
|
||||
String selectedLang = sharedPreferenceUtil.getPrefLanguage(Locale.getDefault().toString());
|
||||
List<String> languageCodeList = new LanguageUtils(getActivity()).getKeys();
|
||||
selectedLang = languageCodeList.contains(selectedLang) ? selectedLang : "en";
|
||||
String code[] = languageCodeList.toArray(new String[0]);
|
||||
String[] entries = new String[code.length];
|
||||
for (int index = 0; index < code.length; index++) {
|
||||
Locale locale = new Locale(code[index]);
|
||||
entries[index] =
|
||||
locale.getDisplayLanguage() + " (" + locale.getDisplayLanguage(locale) + ") ";
|
||||
}
|
||||
languagePref.setEntries(entries);
|
||||
languagePref.setEntryValues(code);
|
||||
languageCodeList.add(0, Locale.ROOT.getLanguage());
|
||||
final String selectedLang =
|
||||
selectedLanguage(languageCodeList, sharedPreferenceUtil.getPrefLanguage());
|
||||
languagePref.setEntries(languageDisplayValues(languageCodeList));
|
||||
languagePref.setEntryValues(languageCodeList.toArray(new String[0]));
|
||||
languagePref.setDefaultValue(selectedLang);
|
||||
languagePref.setValue(selectedLang);
|
||||
languagePref.setTitle(new Locale(selectedLang).getDisplayLanguage());
|
||||
languagePref.setTitle(selectedLang.equals(Locale.ROOT.toString())
|
||||
? getString(R.string.device_default)
|
||||
: new Locale(selectedLang).getDisplayLanguage());
|
||||
languagePref.setOnPreferenceChangeListener((preference, newValue) -> {
|
||||
String languageCode = (String) newValue;
|
||||
LanguageUtils.handleLocaleChange(getActivity(), languageCode);
|
||||
preference.setTitle(new Locale(languageCode).getLanguage());
|
||||
sharedPreferenceUtil.putPrefLanguage(languageCode);
|
||||
restartActivity();
|
||||
return true;
|
||||
@ -150,6 +146,20 @@ public abstract class CorePrefsFragment extends PreferenceFragment implements
|
||||
getActivity().startActivity(new Intent(getActivity(), getActivity().getClass()));
|
||||
}
|
||||
|
||||
@NotNull private String selectedLanguage(List<String> languageCodeList, String langPref) {
|
||||
return languageCodeList.contains(langPref) ? langPref : "en";
|
||||
}
|
||||
|
||||
@NotNull private String[] languageDisplayValues(List<String> languageCodeList) {
|
||||
String[] entries = new String[languageCodeList.size()];
|
||||
entries[0] = getString(R.string.device_default);
|
||||
for (int i = 1; i < languageCodeList.size(); i++) {
|
||||
Locale locale = new Locale(languageCodeList.get(i));
|
||||
entries[i] = locale.getDisplayLanguage() + " (" + locale.getDisplayLanguage(locale) + ") ";
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
private void setAppVersionNumber() {
|
||||
EditTextPreference versionPref = (EditTextPreference) findPreference(PREF_VERSION);
|
||||
versionPref.setSummary(getVersionName() + " Build: " + getVersionCode());
|
||||
|
@ -30,6 +30,7 @@ import android.view.InflateException
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import androidx.core.os.ConfigurationCompat
|
||||
import org.kiwix.kiwixmobile.core.extensions.locale
|
||||
import org.kiwix.kiwixmobile.core.utils.Constants.TAG_KIWIX
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
|
||||
@ -65,7 +66,7 @@ class LanguageUtils(private val context: Context) {
|
||||
}
|
||||
|
||||
private fun haveToChangeFont(sharedPreferenceUtil: SharedPreferenceUtil): Boolean {
|
||||
if (sharedPreferenceUtil.getPrefLanguage("").isEmpty()) {
|
||||
if (sharedPreferenceUtil.prefLanguage == Locale.ROOT.toString()) {
|
||||
return false
|
||||
}
|
||||
return Locale.getAvailableLocales().firstOrNull { locale ->
|
||||
@ -183,16 +184,16 @@ class LanguageUtils(private val context: Context) {
|
||||
context: Context,
|
||||
sharedPreferenceUtil: SharedPreferenceUtil
|
||||
) {
|
||||
val language = sharedPreferenceUtil.getPrefLanguage("")
|
||||
if (language.isEmpty()) {
|
||||
return
|
||||
sharedPreferenceUtil.prefLanguage.takeIf { it != Locale.ROOT.toString() }?.let {
|
||||
handleLocaleChange(context, it)
|
||||
}
|
||||
handleLocaleChange(context, language)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun handleLocaleChange(context: Context, language: String) {
|
||||
val locale = Locale(language)
|
||||
val locale =
|
||||
if (language != Locale.ROOT.toString()) Locale(language)
|
||||
else ConfigurationCompat.getLocales(context.applicationContext.resources.configuration)[0]
|
||||
Locale.setDefault(locale)
|
||||
val config = Configuration()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
|
@ -28,6 +28,7 @@ import io.reactivex.Flowable;
|
||||
import io.reactivex.processors.PublishProcessor;
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@ -104,8 +105,8 @@ public class SharedPreferenceUtil {
|
||||
return sharedPreferences.getFloat(PREF_ZOOM, 100.0f);
|
||||
}
|
||||
|
||||
public String getPrefLanguage(String defaultLanguage) {
|
||||
return sharedPreferences.getString(PREF_LANG, defaultLanguage);
|
||||
public String getPrefLanguage() {
|
||||
return sharedPreferences.getString(PREF_LANG, Locale.ROOT.toString());
|
||||
}
|
||||
|
||||
public String getPrefStorage() {
|
||||
|
@ -295,6 +295,7 @@
|
||||
<string name="no_results">No Results</string>
|
||||
<string name="no_bookmarks">No Bookmarks</string>
|
||||
<string name="no_history">No History</string>
|
||||
<string name="device_default">Device Default</string>
|
||||
<string-array name="pref_night_modes_entries">
|
||||
<item>On</item>
|
||||
<item>Off</item>
|
||||
|
Loading…
x
Reference in New Issue
Block a user