Add a language option to the preference menu

This commit is contained in:
Alexander Bakker 2019-06-22 09:58:35 +02:00
parent eb29be587f
commit b014d95005
6 changed files with 68 additions and 2 deletions

View file

@ -2,8 +2,12 @@ package com.beemdevelopment.aegis;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
public class Preferences {
private SharedPreferences _prefs;
@ -70,4 +74,18 @@ public class Preferences {
public int getTimeout() {
return _prefs.getInt("pref_timeout", -1);
}
public Locale getLocale() {
String lang = _prefs.getString("pref_lang", "system");
if (lang.equals("system")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
return Resources.getSystem().getConfiguration().locale;
}
}
return new Locale(lang);
}
}

View file

@ -2,6 +2,7 @@ package com.beemdevelopment.aegis.ui;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
@ -16,6 +17,8 @@ import com.beemdevelopment.aegis.Theme;
import androidx.annotation.CallSuper;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
public abstract class AegisActivity extends AppCompatActivity implements AegisApplication.LockListener {
private boolean _resumed;
private AegisApplication _app;
@ -24,8 +27,10 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp
protected void onCreate(Bundle savedInstanceState) {
_app = (AegisApplication) getApplication();
// set the theme and create the activity
setPreferredTheme(getPreferences().getCurrentTheme());
// set the theme and locale before creating the activity
Preferences prefs = getPreferences();
setPreferredTheme(prefs.getCurrentTheme());
setLocale(prefs.getLocale());
super.onCreate(savedInstanceState);
// if the app was killed, relaunch MainActivity and close everything else
@ -97,6 +102,15 @@ public abstract class AegisActivity extends AppCompatActivity implements AegisAp
}
}
protected void setLocale(Locale locale) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
/**
* Reports whether this Activity has been resumed. (i.e. onResume was called)
*/

View file

@ -117,6 +117,13 @@ public class PreferencesFragment extends PreferenceFragmentCompat {
}
});
Preference langPreference = findPreference("pref_lang");
langPreference.setOnPreferenceChangeListener((preference, newValue) -> {
_result.putExtra("needsRecreate", true);
getActivity().recreate();
return true;
});
int currentViewMode = app.getPreferences().getCurrentViewMode().ordinal();
Preference viewModePreference = findPreference("pref_view_mode");
viewModePreference.setSummary(String.format("%s: %s", getString(R.string.selected), getResources().getStringArray(R.array.view_mode_titles)[currentViewMode]));

View file

@ -29,4 +29,21 @@
<item>@string/compact_mode_title</item>
<item>@string/small_mode_title</item>
</string-array>
<string-array name="pref_lang_entries">
<item>System default</item>
<item>Dutch</item>
<item>English</item>
<item>French</item>
<item>Russian</item>
</string-array>
<string-array name="pref_lang_values" translatable="false">
<item>system</item>
<item>nl</item>
<item>en</item>
<item>fr</item>
<item>ru</item>
</string-array>
</resources>

View file

@ -15,6 +15,7 @@
<string name="pref_tools_group_title">Tools</string>
<string name="pref_select_theme_title">Theme</string>
<string name="pref_view_mode_title">View mode</string>
<string name="pref_lang_title">Language</string>
<string name="pref_account_name_title">Show the account name</string>
<string name="pref_account_name_summary">Enable this to show the account name next to the issuer</string>
<string name="pref_timeout_title">Timeout</string>

View file

@ -19,6 +19,15 @@
android:title="@string/pref_view_mode_title"
app:iconSpaceReserved="false"/>
<ListPreference
android:key="pref_lang"
android:title="@string/pref_lang_title"
android:summary="%s"
android:entries="@array/pref_lang_entries"
android:entryValues="@array/pref_lang_values"
android:defaultValue="system"
app:iconSpaceReserved="false"/>
<androidx.preference.SwitchPreferenceCompat
android:defaultValue="false"
android:key="pref_account_name"