diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorLocale.kt b/vector/src/main/java/im/vector/app/features/settings/VectorLocale.kt index 575245724b..eddc3457f1 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorLocale.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorLocale.kt @@ -18,6 +18,8 @@ package im.vector.app.features.settings import android.content.Context import android.content.res.Configuration +import android.content.res.Resources +import android.os.Build import androidx.core.content.edit import im.vector.app.BuildConfig import im.vector.app.R @@ -51,6 +53,18 @@ object VectorLocale { */ var applicationLocale = defaultLocale private set + get() { + return if (followSystemLocale) { + Locale.getDefault() + } else { + field + } + } + + /** + * Whether to always follow the system locale + */ + var followSystemLocale: Boolean = false private lateinit var context: Context @@ -61,6 +75,27 @@ object VectorLocale { this.context = context val preferences = DefaultSharedPreferences.getInstance(context) + followSystemLocale = preferences.getBoolean(VectorPreferences.SETTINGS_FOLLOW_SYSTEM_LOCALE, false) + reloadLocale() + } + + fun reloadLocale() { + val preferences = DefaultSharedPreferences.getInstance(context) + + if (followSystemLocale) { + // Locale.getDefault() may have been changed by us, so we need to restore it from the system configuration explicitly + val systemLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + Resources.getSystem().configuration.locales[0] + } else { + @Suppress("DEPRECATION") // Deprecated in API level 24, for which we use above ^ + Resources.getSystem().configuration.locale + } + if (systemLocale != Locale.getDefault()) { + Locale.setDefault(systemLocale) + } + return + } + if (preferences.contains(APPLICATION_LOCALE_LANGUAGE_KEY)) { applicationLocale = Locale( preferences.getString(APPLICATION_LOCALE_LANGUAGE_KEY, "")!!, @@ -85,6 +120,7 @@ object VectorLocale { */ fun saveApplicationLocale(locale: Locale) { applicationLocale = locale + followSystemLocale = false DefaultSharedPreferences.getInstance(context).edit { val language = locale.language diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt index 2e07347aac..f3f71fcd76 100755 --- a/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorPreferences.kt @@ -226,6 +226,7 @@ class VectorPreferences @Inject constructor( private const val SETTINGS_SHOW_OPEN_ANONYMOUS = "SETTINGS_SHOW_OPEN_ANONYMOUS" private const val SETTINGS_FLOATING_DATE = "SETTINGS_FLOATING_DATE" private const val SETTINGS_SPACE_BACK_NAVIGATION = "SETTINGS_SPACE_BACK_NAVIGATION" + const val SETTINGS_FOLLOW_SYSTEM_LOCALE = "SETTINGS_FOLLOW_SYSTEM_LOCALE" private const val DID_ASK_TO_ENABLE_SESSION_PUSH = "DID_ASK_TO_ENABLE_SESSION_PUSH" @@ -1199,6 +1200,7 @@ class VectorPreferences @Inject constructor( .putBoolean(SETTINGS_READ_RECEIPT_FOLLOWS_READ_MARKER, true) .putBoolean(SETTINGS_SHOW_OPEN_ANONYMOUS, true) .putBoolean(SETTINGS_FLOATING_DATE, true) + .putBoolean(SETTINGS_FOLLOW_SYSTEM_LOCALE, true) .apply() } diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt index 24b64d3eab..9c7eaa74b4 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsPreferencesFragment.kt @@ -18,6 +18,8 @@ package im.vector.app.features.settings import android.app.Activity import android.content.Context +import android.content.res.Resources +import android.os.Build import android.os.Bundle import android.widget.CheckedTextView import androidx.core.view.children @@ -40,6 +42,7 @@ import im.vector.app.features.themes.BubbleThemeUtils import im.vector.app.features.themes.ThemeUtils import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.presence.model.PresenceEnum +import java.util.Locale import javax.inject.Inject class VectorSettingsPreferencesFragment @Inject constructor( @@ -78,6 +81,21 @@ class VectorSettingsPreferencesFragment @Inject constructor( // user interface preferences setUserInterfacePreferences() + // Language: follow system + findPreference(VectorPreferences.SETTINGS_FOLLOW_SYSTEM_LOCALE)?.onPreferenceChangeListener = + Preference.OnPreferenceChangeListener { _, newValue -> + if (newValue is Boolean) { + VectorLocale.followSystemLocale = newValue + VectorLocale.reloadLocale() + vectorConfiguration.applyToApplicationContext() + // Restart the Activity + activity?.restart() + true + } else { + false + } + } + // Themes val lightThemePref = findPreference(ThemeUtils.APPLICATION_THEME_KEY)!! val darkThemePref = findPreference(ThemeUtils.APPLICATION_DARK_THEME_KEY)!! diff --git a/vector/src/main/res/values/strings_sc.xml b/vector/src/main/res/values/strings_sc.xml index f39028109d..86b3cadba3 100644 --- a/vector/src/main/res/values/strings_sc.xml +++ b/vector/src/main/res/values/strings_sc.xml @@ -211,4 +211,7 @@ Spaces back stack Press back to show the previously selected space + Follow system language + When changing the system language, also change the app\'s language + diff --git a/vector/src/main/res/xml/vector_settings_preferences.xml b/vector/src/main/res/xml/vector_settings_preferences.xml index 18fac8ae74..17fa8ee44e 100644 --- a/vector/src/main/res/xml/vector_settings_preferences.xml +++ b/vector/src/main/res/xml/vector_settings_preferences.xml @@ -11,8 +11,16 @@ android:key="SETTINGS_INTERFACE_LANGUAGE_PREFERENCE_KEY" android:persistent="false" android:title="@string/settings_interface_language" + android:dependency="SETTINGS_FOLLOW_SYSTEM_LOCALE" app:fragment="im.vector.app.features.settings.locale.LocalePickerFragment" /> + +