diff --git a/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt b/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt index abbcc8a7ea95..a5af3e64975a 100644 --- a/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt +++ b/WordPress/src/main/java/org/wordpress/android/AppInitializer.kt @@ -949,8 +949,7 @@ class AppInitializer @Inject constructor( */ private inner class MemoryAndConfigChangeMonitor : ComponentCallbacks2 { override fun onConfigurationChanged(newConfig: Configuration) { - // Make sure the in-app locale is correct - perAppLocaleManager.checkAndUpdateOldLanguagePrefKey() + // Do nothing } override fun onLowMemory() { diff --git a/WordPress/src/main/java/org/wordpress/android/localcontentmigration/UserFlagsProviderHelper.kt b/WordPress/src/main/java/org/wordpress/android/localcontentmigration/UserFlagsProviderHelper.kt index 5d8a75087824..747a7cefeca9 100644 --- a/WordPress/src/main/java/org/wordpress/android/localcontentmigration/UserFlagsProviderHelper.kt +++ b/WordPress/src/main/java/org/wordpress/android/localcontentmigration/UserFlagsProviderHelper.kt @@ -8,14 +8,12 @@ import org.wordpress.android.localcontentmigration.LocalContentEntityData.UserFl import org.wordpress.android.ui.prefs.AppPrefs.DeletablePrefKey import org.wordpress.android.ui.prefs.AppPrefs.UndeletablePrefKey import org.wordpress.android.ui.prefs.AppPrefsWrapper -import org.wordpress.android.util.LocaleManagerWrapper import org.wordpress.android.viewmodel.ContextProvider import javax.inject.Inject class UserFlagsProviderHelper @Inject constructor( private val appPrefsWrapper: AppPrefsWrapper, contextProvider: ContextProvider, - localeManagerWrapper: LocaleManagerWrapper ) : LocalDataProviderHelper { override fun getData(localEntityId: Int?): LocalContentEntityData = UserFlagsData( @@ -73,6 +71,5 @@ class UserFlagsProviderHelper @Inject constructor( contextProvider.getContext().getString(R.string.pref_key_app_theme), contextProvider.getContext().getString(R.string.pref_key_initial_screen), contextProvider.getContext().getString(R.string.pref_key_send_crash), - localeManagerWrapper.getLocalePrefKeyString() ) } diff --git a/WordPress/src/main/java/org/wordpress/android/push/NotificationsProcessingService.java b/WordPress/src/main/java/org/wordpress/android/push/NotificationsProcessingService.java index 54db41f9624e..2e2922fd02a6 100644 --- a/WordPress/src/main/java/org/wordpress/android/push/NotificationsProcessingService.java +++ b/WordPress/src/main/java/org/wordpress/android/push/NotificationsProcessingService.java @@ -532,7 +532,7 @@ private void getNoteFromNoteId(String noteId, RestRequest.Listener listener, } HashMap params = new HashMap<>(); - params.put("locale", LocaleManager.getLanguage(mContext)); + params.put("locale", LocaleManager.getLanguage()); WordPress.getRestClientUtils().getNotification(params, noteId, listener, errorListener); } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationViewModel.kt index 8df784930ae2..082efe6b6de0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationViewModel.kt @@ -7,7 +7,6 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.CoroutineDispatcher import org.wordpress.android.modules.BG_THREAD import org.wordpress.android.ui.mysite.SelectedSiteRepository -import org.wordpress.android.util.LocaleManagerWrapper import org.wordpress.android.viewmodel.ScopedViewModel import javax.inject.Inject import javax.inject.Named @@ -20,7 +19,6 @@ class PersonalizationViewModel @Inject constructor( private val selectedSiteRepository: SelectedSiteRepository, private val shortcutsPersonalizationViewModelSlice: ShortcutsPersonalizationViewModelSlice, private val dashboardCardPersonalizationViewModelSlice: DashboardCardPersonalizationViewModelSlice, - private val localeManagerWrapper: LocaleManagerWrapper ) : ScopedViewModel(bgDispatcher) { val uiState = dashboardCardPersonalizationViewModelSlice.uiState val shortcutsState = shortcutsPersonalizationViewModelSlice.uiState @@ -32,7 +30,6 @@ class PersonalizationViewModel @Inject constructor( val appLanguage = _appLanguage as LiveData init { - emitLanguageRefreshIfNeeded(localeManagerWrapper.getLanguage()) shortcutsPersonalizationViewModelSlice.initialize(viewModelScope) dashboardCardPersonalizationViewModelSlice.initialize(viewModelScope) } @@ -67,13 +64,4 @@ class PersonalizationViewModel @Inject constructor( val siteId = selectedSiteRepository.getSelectedSite()!!.siteId shortcutsPersonalizationViewModelSlice.addShortcut(shortcutState,siteId) } - - private fun emitLanguageRefreshIfNeeded(languageCode: String) { - if (languageCode.isNotEmpty()) { - val shouldEmitLanguageRefresh = !localeManagerWrapper.isSameLanguage(languageCode) - if (shouldEmitLanguageRefresh) { - _appLanguage.value = languageCode - } - } - } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateJobService.java b/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateJobService.java index f2ed15e62076..6a40704dc184 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateJobService.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateJobService.java @@ -2,7 +2,6 @@ import android.app.job.JobParameters; import android.app.job.JobService; -import android.content.Context; import org.wordpress.android.ui.notifications.NotificationsListFragment; import org.wordpress.android.util.AppLog; @@ -14,11 +13,6 @@ public class NotificationsUpdateJobService extends JobService implements NotificationsUpdateLogic.ServiceCompletionListener { private NotificationsUpdateLogic mNotificationsUpdateLogic; - @Override - protected void attachBaseContext(Context newBase) { - super.attachBaseContext(LocaleManager.setLocale(newBase)); - } - @Override public boolean onStartJob(JobParameters params) { String noteId = null; @@ -42,7 +36,7 @@ public boolean onStopJob(JobParameters params) { public void onCreate() { super.onCreate(); AppLog.i(AppLog.T.NOTIFS, "notifications update job service > created"); - mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(this), this); + mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(), this); } @Override diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateService.java b/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateService.java index a674050a24cc..d1a037e0644f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateService.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/services/NotificationsUpdateService.java @@ -1,7 +1,6 @@ package org.wordpress.android.ui.notifications.services; import android.app.Service; -import android.content.Context; import android.content.Intent; import android.os.IBinder; @@ -14,11 +13,6 @@ public class NotificationsUpdateService extends Service implements NotificationsUpdateLogic.ServiceCompletionListener { private NotificationsUpdateLogic mNotificationsUpdateLogic; - @Override - protected void attachBaseContext(Context newBase) { - super.attachBaseContext(LocaleManager.setLocale(newBase)); - } - @Override public IBinder onBind(Intent intent) { return null; @@ -28,7 +22,7 @@ public IBinder onBind(Intent intent) { public void onCreate() { super.onCreate(); AppLog.i(AppLog.T.NOTIFS, "notifications update service > created"); - mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(this), this); + mNotificationsUpdateLogic = new NotificationsUpdateLogic(LocaleManager.getLanguage(), this); } @Override diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt index 8a3dd57e2a5a..a0909e541fdd 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.kt @@ -2571,7 +2571,7 @@ class EditPostActivity : AppCompatActivity(), EditorFragmentActivity, EditorImag get() { val postType = if (isPage) "page" else "post" val featuredImageId = editPostRepository.featuredImageId.toInt() - val languageString = LocaleManager.getLanguage(this@EditPostActivity) + val languageString = LocaleManager.getLanguage() val wpcomLocaleSlug = languageString.replace("_", "-").lowercase() // this.mIsXPostsCapable may return true for non-WP.com sites, but the app only supports xPosts for P2-based diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt index 2300c40b1437..4c0e962cc801 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppPrefsWrapper.kt @@ -460,8 +460,8 @@ class AppPrefsWrapper @Inject constructor(val buildConfigWrapper: BuildConfigWra return AppPrefs.prefs().getString(key, defValue) } - fun setPrefString(key: String, value: String) { - AppPrefs.prefs().edit().putString(key, value).apply() + fun removePref(key: String) { + AppPrefs.prefs().edit().remove(key).apply() } fun getDebugBooleanPref(key: String, default: Boolean = false) = diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java index 2bc319f1c6c2..6d9fcf91c0f2 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java @@ -248,7 +248,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, mLanguagePreference = (WPPreference) findPreference(getString(R.string.pref_key_language)); mLanguagePreference.setOnPreferenceChangeListener(this); mLanguagePreference.setOnPreferenceClickListener(this); - mLanguagePreference.setSummary(mPerAppLocaleManager.getCurrentLocaleDisplayName()); + mLanguagePreference.setSummary(PerAppLocaleManager.Companion.getCurrentLocaleDisplayName()); return view; } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/interests/ReaderInterestsFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/interests/ReaderInterestsFragment.kt index cad0696f0433..20f5935adcdf 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/interests/ReaderInterestsFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/interests/ReaderInterestsFragment.kt @@ -109,7 +109,7 @@ class ReaderInterestsFragment : Fragment(R.layout.reader_interests_fragment_layo } viewModel.start( - LocaleManager.getLanguage(WordPress.getContext()), + LocaleManager.getLanguage(), parentViewModel, entryPoint ) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverJobService.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverJobService.kt index 418b14ae67f5..c248d653939c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverJobService.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverJobService.kt @@ -2,7 +2,6 @@ package org.wordpress.android.ui.reader.services.discover import android.app.job.JobParameters import android.app.job.JobService -import android.content.Context import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope @@ -12,7 +11,6 @@ import org.wordpress.android.ui.reader.services.ServiceCompletionListener import org.wordpress.android.ui.reader.services.discover.ReaderDiscoverLogic.DiscoverTasks import org.wordpress.android.util.AppLog import org.wordpress.android.util.AppLog.T.READER -import org.wordpress.android.util.LocaleManager import javax.inject.Inject import javax.inject.Named import kotlin.coroutines.CoroutineContext @@ -31,10 +29,6 @@ class ReaderDiscoverJobService : JobService(), ServiceCompletionListener, Corout override val coroutineContext: CoroutineContext get() = ioDispatcher + job - override fun attachBaseContext(newBase: Context) { - super.attachBaseContext(LocaleManager.setLocale(newBase)) - } - override fun onStartJob(params: JobParameters): Boolean { AppLog.i(READER, "reader discover job service > started") diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverService.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverService.kt index d34ca7f8da7e..e480981ff643 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverService.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/discover/ReaderDiscoverService.kt @@ -1,7 +1,6 @@ package org.wordpress.android.ui.reader.services.discover import android.app.Service -import android.content.Context import android.content.Intent import android.os.IBinder import dagger.hilt.android.AndroidEntryPoint @@ -14,7 +13,6 @@ import org.wordpress.android.ui.reader.services.discover.ReaderDiscoverLogic.Dis import org.wordpress.android.ui.reader.services.discover.ReaderDiscoverServiceStarter.ARG_DISCOVER_TASK import org.wordpress.android.util.AppLog import org.wordpress.android.util.AppLog.T.READER -import org.wordpress.android.util.LocaleManager import org.wordpress.android.util.extensions.getSerializableExtraCompat import javax.inject.Inject import javax.inject.Named @@ -41,10 +39,6 @@ class ReaderDiscoverService : Service(), ServiceCompletionListener, CoroutineSco return null } - override fun attachBaseContext(newBase: Context) { - super.attachBaseContext(LocaleManager.setLocale(newBase)) - } - override fun onCreate() { super.onCreate() AppLog.i(READER, "reader discover service > created") diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateJobService.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateJobService.java index 64058405812e..a2f153cf2c3f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateJobService.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateJobService.java @@ -2,12 +2,10 @@ import android.app.job.JobParameters; import android.app.job.JobService; -import android.content.Context; import org.wordpress.android.WordPress; import org.wordpress.android.ui.reader.services.ServiceCompletionListener; import org.wordpress.android.util.AppLog; -import org.wordpress.android.util.LocaleManager; import java.util.EnumSet; @@ -16,11 +14,6 @@ public class ReaderUpdateJobService extends JobService implements ServiceCompletionListener { private ReaderUpdateLogic mReaderUpdateLogic; - @Override - protected void attachBaseContext(Context newBase) { - super.attachBaseContext(LocaleManager.setLocale(newBase)); - } - @Override public boolean onStartJob(JobParameters params) { AppLog.i(AppLog.T.READER, "reader job service > started"); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java index 9af7b113c7db..d6ea3d0a2455 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java @@ -64,7 +64,7 @@ public enum UpdateTask { public ReaderUpdateLogic(Context context, WordPress app, ServiceCompletionListener listener) { mCompletionListener = listener; app.component().inject(this); - mLanguage = LocaleManager.getLanguage(app); + mLanguage = LocaleManager.getLanguage(); mContext = context; } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateService.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateService.java index 5569b40bfc58..aa4451f84640 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateService.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateService.java @@ -1,14 +1,12 @@ package org.wordpress.android.ui.reader.services.update; import android.app.Service; -import android.content.Context; import android.content.Intent; import android.os.IBinder; import org.wordpress.android.WordPress; import org.wordpress.android.ui.reader.services.ServiceCompletionListener; import org.wordpress.android.util.AppLog; -import org.wordpress.android.util.LocaleManager; import java.util.EnumSet; @@ -28,11 +26,6 @@ public IBinder onBind(Intent intent) { return null; } - @Override - protected void attachBaseContext(Context newBase) { - super.attachBaseContext(LocaleManager.setLocale(newBase)); - } - @Override public void onCreate() { super.onCreate(); diff --git a/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.kt b/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.kt index 324f113a0a63..ff1c1534e89d 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.kt @@ -1,26 +1,16 @@ package org.wordpress.android.util -import android.annotation.SuppressLint import android.content.Context -import android.content.res.Configuration import android.text.TextUtils -import androidx.preference.PreferenceManager import org.wordpress.android.R import java.text.Collator import java.util.Locale import java.util.regex.Pattern /** - * Helper class for working with localized strings. Ensures updates to the users - * selected language is properly saved and resources appropriately updated for the - * android version. + * Helper class for working with localized strings */ object LocaleManager { - /** - * Key used for saving the language selection to shared preferences. - */ - private const val LOCALE_PREF_KEY_STRING = "language-pref" - /** * Pattern to split a language string (to parse the language and region values). */ @@ -29,37 +19,17 @@ object LocaleManager { private const val MIN_LANGUAGE_CODE_LENGTH = 2 private const val MAX_LANGUAGE_CODE_LENGTH = 6 - /** - * Activate the locale associated with the provided context. - * - * @param context The current context. - */ - @JvmStatic - fun setLocale(context: Context): Context { - return updateResources(context, getLanguage(context)) - } - - /** - * Compare the language for the current context with another language. - * - * @param language The language to compare - * @return True if the languages are the same, else false - */ - fun isSameLanguage(language: String): Boolean { - val newLocale = languageLocale(language) - return Locale.getDefault().toString() == newLocale.toString() - } + @Suppress("ForbiddenComment") /** - * If the user has selected a language other than the device default, return that - * language code, else just return the device default language code. + * This is simply a wrapper for the per-app language code. + * TODO: Remove this and directly call PerAppLocaleManager * * @return The 2-letter language code (example "en") */ @JvmStatic - fun getLanguage(context: Context): String { - val prefs = PreferenceManager.getDefaultSharedPreferences(context) - return prefs.getString(LOCALE_PREF_KEY_STRING, LanguageUtils.getCurrentDeviceLanguageCode())!! + fun getLanguage(): String { + return PerAppLocaleManager.getCurrentLocaleLanguageCode() } /** @@ -87,27 +57,7 @@ object LocaleManager { return langID ?: deviceLanguageCode } - /** - * Update resources for the current session. - * - * @param context The current active context - * @param language The 2-letter language code (example "en") - * @return The modified context containing the updated localized resources - */ - @SuppressLint("AppBundleLocaleChanges") - private fun updateResources(context: Context, language: String): Context { - val locale = languageLocale(language) - Locale.setDefault(locale) - - val res = context.resources - val config = Configuration(res.configuration) - - // NOTE: Earlier versions of Android require both of these to be set, otherwise - // RTL may not be implemented properly. - config.setLocale(locale) - return context.createConfigurationContext(config) - } - + @Suppress("ForbiddenComment") /** * Method gets around a bug in the java.util.Formatter for API 7.x as detailed here * [https://bugs.openjdk.java.net/browse/JDK-8167567]. Any strings that contain @@ -116,9 +66,10 @@ object LocaleManager { * `String.format(LocaleManager.getSafeLocale(context), baseString, val)` * * An example of a string that contains locale-specific grouping separators: - * - * ` %,d likes ` - * + * ` + * %,d likes + `* + * TODO: This is a workaround for a bug in API 7, which we no longer support. Investigate removing this. */ @JvmStatic fun getSafeLocale(context: Context?): Locale { @@ -201,10 +152,7 @@ object LocaleManager { for (i in entryStrings.indices) { // now, we can split the sorted array to extract the display string and the language code - val split = entryStrings[i] - .split("__".toRegex()) - .dropLastWhile { it.isEmpty() } - .toTypedArray() + val split = entryStrings[i].split("__") sortedEntries[i] = split[0] sortedValues[i] = split[1] detailStrings[i] = @@ -244,7 +192,4 @@ object LocaleManager { displayLanguage } } - - @JvmStatic - fun getLocalePrefKeyString(): String = LOCALE_PREF_KEY_STRING } diff --git a/WordPress/src/main/java/org/wordpress/android/util/LocaleManagerWrapper.kt b/WordPress/src/main/java/org/wordpress/android/util/LocaleManagerWrapper.kt index 60e3c47b0f7d..0e24a3281caa 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/LocaleManagerWrapper.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/LocaleManagerWrapper.kt @@ -11,8 +11,5 @@ class LocaleManagerWrapper fun getLocale(): Locale = Locale.getDefault() fun getTimeZone(): TimeZone = TimeZone.getDefault() fun getCurrentCalendar(): Calendar = Calendar.getInstance(getLocale()) - fun getLanguage(): String = LocaleManager.getLanguage(context) - fun getLocalePrefKeyString(): String = LocaleManager.getLocalePrefKeyString() - fun isSameLanguage(language: String): Boolean = LocaleManager.isSameLanguage(language) - fun setLocale(context: Context): Context = LocaleManager.setLocale(context) + fun getLanguage(): String = LocaleManager.getLanguage() } diff --git a/WordPress/src/main/java/org/wordpress/android/util/PerAppLocaleManager.kt b/WordPress/src/main/java/org/wordpress/android/util/PerAppLocaleManager.kt index 0067c1fbfacb..dc51b00f2b7d 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/PerAppLocaleManager.kt +++ b/WordPress/src/main/java/org/wordpress/android/util/PerAppLocaleManager.kt @@ -31,86 +31,26 @@ class PerAppLocaleManager @Inject constructor( private val siteStore: SiteStore, private val accountStore: AccountStore, ) { - private fun getCurrentLocale(): Locale { - return if (isApplicationLocaleEmpty()) { - Locale.getDefault() - } else { - getApplicationLocaleList()[0] ?: Locale.getDefault() - } - } - - fun getCurrentLocaleDisplayName(): String = getCurrentLocale().displayName - - private fun getCurrentLocaleLanguageCode(): String = getCurrentLocale().language - - /** - * Important: this should only be called after Activity.onCreate() - * https://developer.android.com/reference/androidx/appcompat/app/AppCompatDelegate#getApplicationLocales() - */ - private fun getApplicationLocaleList() = AppCompatDelegate.getApplicationLocales() - - private fun isApplicationLocaleEmpty(): Boolean { - val locales = getApplicationLocaleList() - return (locales.isEmpty || locales == LocaleListCompat.getEmptyLocaleList()) - } - - /** - * We want to make sure the language pref for the in-app locale (old implementation) is set - * to the same locale as the AndroidX per-app locale. This way LocaleManager.getLanguage - - * which is used throughout the app - returns the correct language code. We can remove - * this once the per-app language pref is no longer experimental. - */ - fun checkAndUpdateOldLanguagePrefKey() { - val prefKey = LocaleManager.getLocalePrefKeyString() - val inAppLanguage = appPrefsWrapper.getPrefString(prefKey, "") - val perAppLanguage = getCurrentLocale().language - if (perAppLanguage.isNotEmpty() && inAppLanguage.equals(perAppLanguage).not()) { - appPrefsWrapper.setPrefString(prefKey, perAppLanguage) - appLogWrapper.d( - AppLog.T.SETTINGS, - "PerAppLocaleManager: changed inAppLanguage from $inAppLanguage to $perAppLanguage" - ) - } - } - - /** - * This routine can be helpful during development to reset the app locale - */ - @Suppress("unused") - fun resetApplicationLocale() { - AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList()) - } - private fun setCurrentLocaleByLanguageCode(languageCode: String) { // We shouldn't have to replace "_" with "-" but this is in order to work with our existing language picker // on pre-Android 13 devices val appLocale = LocaleListCompat.forLanguageTags(languageCode.replace("_", "-")) AppCompatDelegate.setApplicationLocales(appLocale) - checkAndUpdateOldLanguagePrefKey() } /** - * Previously the app locale was stored in SharedPreferences, so here we migrate to AndroidX per-app language prefs + * Previously the app locale was stored in SharedPreferences, so here we migrate to AndroidX per-app language prefs. + * This was added in our Jan 2025 release and can be removed after a few subsequent releases. */ fun performMigrationIfNecessary() { - if (isApplicationLocaleEmpty()) { - val prefKey = LocaleManager.getLocalePrefKeyString() - val previousLanguage = appPrefsWrapper.getPrefString(prefKey, "") - if (previousLanguage?.isNotEmpty() == true) { - appLogWrapper.d( - AppLog.T.SETTINGS, - "PerAppLocaleManager: performing migration to AndroidX per-app language prefs" - ) - setCurrentLocaleByLanguageCode(previousLanguage) - } else { - appLogWrapper.d( - AppLog.T.SETTINGS, - "PerAppLocaleManager: setting default locale" - ) - setCurrentLocaleByLanguageCode(Locale.getDefault().language) - } - } else { - checkAndUpdateOldLanguagePrefKey() + val previousLanguage = appPrefsWrapper.getPrefString(OLD_LOCALE_PREF_KEY_STRING, "") + if (previousLanguage?.isNotEmpty() == true) { + appLogWrapper.d( + AppLog.T.SETTINGS, + "PerAppLocaleManager: performing migration to AndroidX per-app language prefs" + ) + setCurrentLocaleByLanguageCode(previousLanguage) + appPrefsWrapper.removePref(OLD_LOCALE_PREF_KEY_STRING) } } @@ -131,7 +71,7 @@ class PerAppLocaleManager @Inject constructor( @Suppress("ForbiddenComment") /** * Called when the device language is changed from our in-app language picker - * TODO: Detect when language changed from app settings dialog + * TODO: Detect when language changed from system app settings dialog */ fun onLanguageChanged(languageCode: String) { if (languageCode.isEmpty()) { @@ -155,4 +95,30 @@ class PerAppLocaleManager @Inject constructor( // update Reader tags as they need be localized ReaderUpdateServiceStarter.startService(getContext(), EnumSet.of(UpdateTask.TAGS)) } + + /** + * This routine can be helpful during development to reset the app locale + */ + @Suppress("unused") + fun resetApplicationLocale() { + AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList()) + } + + companion object { + // Key previously used for saving the language selection to shared preferences + private const val OLD_LOCALE_PREF_KEY_STRING: String = "language-pref" + + private fun getCurrentLocale(): Locale { + val locales = AppCompatDelegate.getApplicationLocales() + return if (locales.isEmpty || locales == LocaleListCompat.getEmptyLocaleList()) { + Locale.getDefault() + } else { + locales[0] ?: Locale.getDefault() + } + } + + fun getCurrentLocaleDisplayName(): String = getCurrentLocale().displayName + + fun getCurrentLocaleLanguageCode(): String = getCurrentLocale().language + } } diff --git a/WordPress/src/main/java/org/wordpress/android/viewmodel/ContextProvider.kt b/WordPress/src/main/java/org/wordpress/android/viewmodel/ContextProvider.kt index 04c0af3557f4..001054380da9 100644 --- a/WordPress/src/main/java/org/wordpress/android/viewmodel/ContextProvider.kt +++ b/WordPress/src/main/java/org/wordpress/android/viewmodel/ContextProvider.kt @@ -1,16 +1,11 @@ package org.wordpress.android.viewmodel import android.content.Context -import org.wordpress.android.util.LocaleManager import javax.inject.Inject import javax.inject.Singleton @Singleton class ContextProvider @Inject constructor(private var context: Context) { - fun refreshContext() { - this.context = LocaleManager.setLocale(this.context) - } - fun getContext(): Context = context }