Add in-app language switcher

Closes #53
This commit is contained in:
Jakob Nixdorf 2017-11-15 11:48:26 +01:00
parent 6fb96e18a9
commit 07fc20bc0d
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
8 changed files with 87 additions and 3 deletions

View file

@ -366,7 +366,8 @@ public class MainActivity extends BaseActivity
key.equals(getString(R.string.settings_key_tap_to_reveal)) || key.equals(getString(R.string.settings_key_tap_to_reveal)) ||
key.equals(getString(R.string.settings_key_label_scroll))) { key.equals(getString(R.string.settings_key_label_scroll))) {
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
} else if (key.equals(getString(R.string.settings_key_theme))) { } else if (key.equals(getString(R.string.settings_key_theme)) ||
key.equals(getString(R.string.settings_key_lang))) {
recreate(); recreate();
} }
} }

View file

@ -85,7 +85,8 @@ public class SettingsActivity extends BaseActivity
} }
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key.equals(getString(R.string.settings_key_theme))) { if (key.equals(getString(R.string.settings_key_theme)) ||
key.equals(getString(R.string.settings_key_lang))) {
recreate(); recreate();
} }
} }

View file

@ -22,12 +22,15 @@
package org.shadowice.flocke.andotp.Activities; package org.shadowice.flocke.andotp.Activities;
import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import org.shadowice.flocke.andotp.R; import org.shadowice.flocke.andotp.R;
import org.shadowice.flocke.andotp.Utilities.Settings; import org.shadowice.flocke.andotp.Utilities.Settings;
import java.util.Locale;
public abstract class ThemedActivity extends AppCompatActivity { public abstract class ThemedActivity extends AppCompatActivity {
public Settings settings; public Settings settings;
@ -35,6 +38,20 @@ public abstract class ThemedActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
settings = new Settings(this); settings = new Settings(this);
setTheme();
setLocale();
super.onCreate(savedInstanceState);
}
@Override
public void onResume() {
setLocale();
super.onResume();
}
public void setTheme() {
String theme = settings.getTheme(); String theme = settings.getTheme();
if (theme.equals("light")) { if (theme.equals("light")) {
@ -44,7 +61,15 @@ public abstract class ThemedActivity extends AppCompatActivity {
} else if (theme.equals("black")) { } else if (theme.equals("black")) {
setTheme(R.style.AppTheme_Black_NoActionBar); setTheme(R.style.AppTheme_Black_NoActionBar);
} }
}
super.onCreate(savedInstanceState); public void setLocale() {
Locale locale = settings.getLang();
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
} }
} }

View file

@ -36,6 +36,7 @@ import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.KeyPair; import java.security.KeyPair;
import java.util.Collections; import java.util.Collections;
import java.util.Locale;
import java.util.Set; import java.util.Set;
import static org.shadowice.flocke.andotp.Preferences.PasswordEncryptedPreference.KEY_ALIAS; import static org.shadowice.flocke.andotp.Preferences.PasswordEncryptedPreference.KEY_ALIAS;
@ -202,6 +203,15 @@ public class Settings {
return settings.getStringSet(getResString(R.string.settings_key_panic), Collections.<String>emptySet()); return settings.getStringSet(getResString(R.string.settings_key_panic), Collections.<String>emptySet());
} }
public Locale getLang() {
String lang = getString(R.string.settings_key_lang, R.string.settings_default_lang);
if (lang.equals("system"))
return Tools.getSystemLocale();
else
return new Locale(lang);
}
public String getTheme() { public String getTheme() {
return getString(R.string.settings_key_theme, R.string.settings_default_theme); return getString(R.string.settings_key_theme, R.string.settings_default_theme);
} }

View file

@ -29,9 +29,11 @@ import android.graphics.ColorFilter;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter; import android.graphics.PorterDuffColorFilter;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Environment; import android.os.Environment;
import java.io.File; import java.io.File;
import java.util.Locale;
public class Tools { public class Tools {
/* Checks if external storage is available for read and write */ /* Checks if external storage is available for read and write */
@ -70,4 +72,12 @@ public class Tools {
File dir = new File(path); File dir = new File(path);
return dir.exists() || dir.mkdirs(); return dir.exists() || dir.mkdirs();
} }
public static Locale getSystemLocale() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
return Resources.getSystem().getConfiguration().locale;
}
}
} }

View file

@ -11,6 +11,7 @@
<string name="settings_key_auth_pin_hash" translatable="false">pref_auth_pin_hash</string> <string name="settings_key_auth_pin_hash" translatable="false">pref_auth_pin_hash</string>
<string name="settings_key_panic" translatable="false">pref_panic</string> <string name="settings_key_panic" translatable="false">pref_panic</string>
<string name="settings_key_lang" translatable="false">pref_lang</string>
<string name="settings_key_theme" translatable="false">pref_theme</string> <string name="settings_key_theme" translatable="false">pref_theme</string>
<string name="settings_key_label_size" translatable="false">pref_label_size</string> <string name="settings_key_label_size" translatable="false">pref_label_size</string>
<string name="settings_key_label_scroll" translatable="false">pref_label_scroll</string> <string name="settings_key_label_scroll" translatable="false">pref_label_scroll</string>
@ -30,6 +31,7 @@
<!-- Default values --> <!-- Default values -->
<string name="settings_default_auth" translatable="false">none</string> <string name="settings_default_auth" translatable="false">none</string>
<string name="settings_default_lang" translatable="false">system</string>
<string name="settings_default_theme" translatable="false">light</string> <string name="settings_default_theme" translatable="false">light</string>
<integer name="settings_default_label_size">8</integer> <integer name="settings_default_label_size">8</integer>
@ -46,6 +48,19 @@
<item>settings</item> <item>settings</item>
</string-array> </string-array>
<string-array name="settings_values_lang" translatable="false">
<item>system</item>
<item>en</item>
<item>cs</item>
<item>de</item>
<item>es</item>
<item>fr</item>
<item>gl</item>
<item>nl</item>
<item>pl</item>
<item>ru</item>
</string-array>
<string-array name="settings_values_theme" translatable="false"> <string-array name="settings_values_theme" translatable="false">
<item>light</item> <item>light</item>
<item>dark</item> <item>dark</item>

View file

@ -14,6 +14,7 @@
<string name="settings_title_auth_pin">PIN</string> <string name="settings_title_auth_pin">PIN</string>
<string name="settings_title_panic">Panic Trigger</string> <string name="settings_title_panic">Panic Trigger</string>
<string name="settings_title_lang">Language</string>
<string name="settings_title_theme">Theme</string> <string name="settings_title_theme">Theme</string>
<string name="settings_title_label_size">Label font size</string> <string name="settings_title_label_size">Label font size</string>
<string name="settings_title_label_scroll">Scroll label</string> <string name="settings_title_label_scroll">Scroll label</string>
@ -64,6 +65,19 @@
<item>Reset app settings</item> <item>Reset app settings</item>
</string-array> </string-array>
<string-array name="settings_entries_lang">
<item>System default</item>
<item>English</item>
<item>Czech</item>
<item>German</item>
<item>Spanish</item>
<item>French</item>
<item>Galician</item>
<item>Dutch</item>
<item>Polish</item>
<item>Russian</item>
</string-array>
<string-array name="settings_entries_theme"> <string-array name="settings_entries_theme">
<item>Light theme</item> <item>Light theme</item>
<item>Dark theme</item> <item>Dark theme</item>

View file

@ -37,6 +37,14 @@
<PreferenceCategory <PreferenceCategory
android:title="@string/settings_category_title_ui"> android:title="@string/settings_category_title_ui">
<ListPreference
android:key="@string/settings_key_lang"
android:title="@string/settings_title_lang"
android:summary="%s"
android:entries="@array/settings_entries_lang"
android:entryValues="@array/settings_values_lang"
android:defaultValue="@string/settings_default_lang" />
<ListPreference <ListPreference
android:key="@string/settings_key_theme" android:key="@string/settings_key_theme"
android:title="@string/settings_title_theme" android:title="@string/settings_title_theme"