diff --git a/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt b/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt index 602999043..0de31d6f7 100644 --- a/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt +++ b/app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt @@ -1,6 +1,7 @@ package org.mifos.mobile import android.content.Context +import android.content.res.Configuration import androidx.appcompat.app.AppCompatDelegate import androidx.multidex.MultiDex import androidx.multidex.MultiDexApplication @@ -11,6 +12,7 @@ import com.raizlabs.android.dbflow.config.FlowManager import dagger.hilt.android.HiltAndroidApp import org.mifos.mobile.api.local.PreferencesHelper import org.mifos.mobile.ui.fragments.applySavedTheme +import org.mifos.mobile.utils.LanguageHelper import org.mifos.mobile.utils.LanguageHelper.onAttach import java.util.Locale @@ -45,7 +47,9 @@ class MifosSelfServiceApp : MultiDexApplication() { PreferencesHelper(this).applySavedTheme() } - override fun attachBaseContext(base: Context) { - super.attachBaseContext(onAttach(base, Locale.getDefault().language)) + override fun onConfigurationChanged(newConfig: Configuration) { + super.onConfigurationChanged(newConfig) + context?.let { onAttach(it) } } + } diff --git a/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt b/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt index 352a370a9..fc8574053 100644 --- a/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt +++ b/app/src/main/java/org/mifos/mobile/ui/fragments/SettingsFragment.kt @@ -21,6 +21,7 @@ import org.mifos.mobile.utils.ConfigurationDialogFragmentCompat import org.mifos.mobile.utils.ConfigurationPreference import org.mifos.mobile.utils.Constants import org.mifos.mobile.utils.LanguageHelper +import java.util.Locale /** * Created by dilpreet on 02/10/17. @@ -132,7 +133,23 @@ class SettingsFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeLis override fun onSharedPreferenceChanged(p0: SharedPreferences?, p1: String?) { val preference = findPreference(p1) if (preference is ListPreference) { - LanguageHelper.setLocale(context, preference.value) + val isSystemLanguage = + (preference.value == resources.getStringArray(R.array.languages_value)[0]) + prefsHelper.putBoolean( + context?.getString(R.string.default_system_language), + isSystemLanguage + ) + if (!isSystemLanguage) { + LanguageHelper.setLocale(context, preference.value) + } else { + if (!resources.getStringArray(R.array.languages_value) + .contains(Locale.getDefault().language) + ) { + LanguageHelper.setLocale(context, "en") + } else { + LanguageHelper.setLocale(context, Locale.getDefault().language) + } + } val intent = Intent(activity, activity?.javaClass) intent.putExtra(Constants.HAS_SETTINGS_CHANGED, true) startActivity(intent) diff --git a/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt b/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt index d10bd1522..df2493ebf 100644 --- a/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt +++ b/app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt @@ -13,8 +13,17 @@ import java.util.* object LanguageHelper { // https://gunhansancar.com/change-language-programmatically-in-android/ fun onAttach(context: Context): Context? { - val lang = getPersistedData(context, Locale.getDefault().language) - return lang?.let { setLocale(context, it) } + val preferences = PreferenceManager.getDefaultSharedPreferences(context) + return if (preferences.getBoolean(context.getString(R.string.default_system_language), true)) { + if(!context.resources.getStringArray(R.array.languages_value).contains(Locale.getDefault().language)){ + setLocale(context, "en") + }else{ + setLocale(context, Locale.getDefault().language) + } + } else { + val lang = getPersistedData(context, Locale.getDefault().language) + lang?.let { setLocale(context, it) } + } } @JvmStatic @@ -52,9 +61,7 @@ object LanguageHelper { val resources = context?.resources val configuration = resources?.configuration configuration?.locale = locale - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - configuration?.setLayoutDirection(locale) - } + configuration?.setLayoutDirection(locale) resources?.updateConfiguration(configuration, resources.displayMetrics) return context } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 06e8f8b0e..c96a356ef 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -535,6 +535,7 @@ language_type + default_system_language theme_type Total Savings Balance Total Loan Balance @@ -600,6 +601,7 @@ Change the application theme + System Language English हिंदी عربى @@ -620,6 +622,7 @@ + System_Language en hi ar diff --git a/app/src/main/res/xml/settings_preference.xml b/app/src/main/res/xml/settings_preference.xml index 483335fe9..554063636 100644 --- a/app/src/main/res/xml/settings_preference.xml +++ b/app/src/main/res/xml/settings_preference.xml @@ -19,7 +19,7 @@