Changing language is now available.

This commit is contained in:
Alex Ning 2020-12-01 00:12:16 +08:00
parent 4acf8611e7
commit 924128f09a
8 changed files with 83 additions and 3 deletions

View File

@ -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 &&

View File

@ -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

View File

@ -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";

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFF"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2s0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2s0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2s-0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2s-0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2s0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2s0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2s-0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2s-0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>

View File

@ -295,4 +295,32 @@
<item>1</item>
</string-array>
<string-array name="settings_language">
<item>Auto</item>
<item>Hrvatski</item>
<item>English</item>
<item>Française</item>
<item>Deutsche</item>
<item>हिंदी</item>
<item>Italiana</item>
<item>日本語</item>
<item>Português (BR)</item>
<item>Español</item>
<item>Türkçe</item>
</string-array>
<string-array name="settings_language_values">
<item>auto</item>
<item>hr</item>
<item>en</item>
<item>fr</item>
<item>de</item>
<item>hi</item>
<item>it</item>
<item>ja</item>
<item>pt-rBR</item>
<item>es</item>
<item>tr-rTR</item>
</string-array>
</resources>

View File

@ -507,6 +507,7 @@
<string name="settings_swipe_action_swipe_left_title">Swipe Left</string>
<string name="settings_swipe_action_swipe_right_title">Swipe Right</string>
<string name="settings_swipe_action_info_summary">Not applicable to post feed with more than 1 column or post detail.</string>
<string name="settings_language_title">Language</string>
<string name="no_link_available">Cannot get the link</string>

View File

@ -25,4 +25,13 @@
app:title="@string/settings_respect_subreddit_recommended_comment_sort_type_title"
app:summary="@string/settings_respect_subreddit_recommended_comment_sort_type_summary" />
<ListPreference
app:defaultValue="auto"
app:entries="@array/settings_language"
app:entryValues="@array/settings_language_values"
app:key="language"
app:icon="@drawable/ic_language_24dp"
app:title="@string/settings_language_title"
app:useSimpleSummaryProvider="true" />
</PreferenceScreen>