Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: #2397 Added System Language Option #2399

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/src/main/java/org/mifos/mobile/MifosSelfServiceApp.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/org/mifos/mobile/utils/LanguageHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@
</string-array>

<string name="language_type" translatable="false">language_type</string>
<string name="default_system_language" translatable="false">default_system_language</string>
<string name="theme_type" translatable="false">theme_type</string>
<string name="total_saving_balance">Total Savings Balance</string>
<string name="total_loan_balance">Total Loan Balance</string>
Expand Down Expand Up @@ -600,6 +601,7 @@
<string name="change_app_theme">Change the application theme</string>

<string-array name="languages" translatable="false">
<item>System Language</item>
<item>English</item>
<item>हिंदी</item>
<item>عربى</item>
Expand All @@ -620,6 +622,7 @@
</string-array>

<string-array name="languages_value" translatable="false">
<item>System_Language</item>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to add this option if we are changing the language dynamically? I mean the moment we set our mobile to follow a particular language it should be reflected in our app at that very instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need it because if we are dependent on the System's Language then having a System Language value will be helpful. And sometimes if the System's Language is not supported in our App then English is made as Default. I have attached the videos for it also on the top.

This is also similar to the app theme preference on System Theme, Dark Theme, and Light Theme.

<item>en</item>
<item>hi</item>
<item>ar</item>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/settings_preference.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<PreferenceCategory android:title="@string/other">
<androidx.preference.ListPreference
android:defaultValue="en"
android:defaultValue="System_Language"
android:entries="@array/languages"
android:entryValues="@array/languages_value"
android:icon="@drawable/ic_translate"
Expand Down
Loading