From 924128f09aa3c514bcbe74af65ad4feb05c774dc Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Tue, 1 Dec 2020 00:12:16 +0800 Subject: Changing language is now available. --- .../infinityforreddit/activities/BaseActivity.java | 15 ++++++++++++++- .../settings/MiscellaneousPreferenceFragment.java | 13 +++++++++++-- .../infinityforreddit/utils/SharedPreferencesUtils.java | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java index e01c0430..ccc00968 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java @@ -29,9 +29,11 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; import java.lang.reflect.Field; +import java.util.Locale; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.AppBarStateChangeListener; +import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.font.ContentFontFamily; import ml.docilealligator.infinityforreddit.font.ContentFontStyle; @@ -39,7 +41,6 @@ import ml.docilealligator.infinityforreddit.font.FontFamily; import ml.docilealligator.infinityforreddit.font.FontStyle; import ml.docilealligator.infinityforreddit.font.TitleFontFamily; import ml.docilealligator.infinityforreddit.font.TitleFontStyle; -import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.utils.CustomThemeSharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; @@ -64,6 +65,18 @@ public abstract class BaseActivity extends AppCompatActivity { customThemeWrapper = getCustomThemeWrapper(); SharedPreferences mSharedPreferences = getDefaultSharedPreferences(); + + String language = mSharedPreferences.getString(SharedPreferencesUtils.LANGUAGE, SharedPreferencesUtils.LANGUAGE_DEFAULT_VALUE); + if (language.equals(SharedPreferencesUtils.LANGUAGE_DEFAULT_VALUE)) { + language = Resources.getSystem().getConfiguration().locale.getLanguage(); + } + Locale locale = new Locale(language); + Locale.setDefault(locale); + Resources resources = getResources(); + Configuration config = resources.getConfiguration(); + config.setLocale(locale); + resources.updateConfiguration(config, resources.getDisplayMetrics()); + boolean systemDefault = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; int systemThemeType = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.THEME_KEY, "2")); immersiveInterface = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MiscellaneousPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MiscellaneousPreferenceFragment.java index c27e7eec..f33dd57f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MiscellaneousPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MiscellaneousPreferenceFragment.java @@ -7,6 +7,7 @@ import android.os.Bundle; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; +import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreference; @@ -15,10 +16,10 @@ import org.greenrobot.eventbus.EventBus; import javax.inject.Inject; import javax.inject.Named; -import ml.docilealligator.infinityforreddit.events.ChangeSavePostFeedScrolledPositionEvent; -import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.events.ChangeSavePostFeedScrolledPositionEvent; +import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; public class MiscellaneousPreferenceFragment extends PreferenceFragmentCompat { @@ -40,6 +41,7 @@ public class MiscellaneousPreferenceFragment extends PreferenceFragmentCompat { SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT); SwitchPreference savePostFeedScrolledPositionSwitch = findPreference(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION); + ListPreference languageListPreference = findPreference(SharedPreferencesUtils.LANGUAGE); if (confirmToExitSwitch != null) { confirmToExitSwitch.setOnPreferenceChangeListener((preference, newValue) -> { @@ -57,6 +59,13 @@ public class MiscellaneousPreferenceFragment extends PreferenceFragmentCompat { return true; }); } + + if (languageListPreference != null) { + languageListPreference.setOnPreferenceChangeListener((preference, newValue) -> { + EventBus.getDefault().post(new RecreateActivityEvent()); + return true; + }); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index ebfa20fe..f6c3d5bd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -144,6 +144,8 @@ public class SharedPreferencesUtils { public static final String SWIPE_RIGHT_ACTION = "swipe_right_action"; public static final int SWIPE_ACITON_UPVOTE = 0; public static final int SWIPE_ACITON_DOWNVOTE = 1; + public static final String LANGUAGE = "language"; + public static final String LANGUAGE_DEFAULT_VALUE = "auto"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_COUNT = "_main_page_tab_count"; -- cgit v1.2.3