From 0fb664c1410b957cae028143eb152d72e35ec9f7 Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sun, 5 Jun 2016 14:33:09 +0200 Subject: [PATCH] allow to switch languages at runtime --- .../gadgetbridge/activities/GBActivity.java | 22 ++++++++++++++ app/src/main/res/values/arrays.xml | 30 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 ++ app/src/main/res/xml/preferences.xml | 7 +++++ 4 files changed, 61 insertions(+) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java index 39c972ea1..dd9c2599a 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java @@ -1,14 +1,33 @@ package nodomain.freeyourgadget.gadgetbridge.activities; +import android.content.res.Configuration; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import java.util.Locale; + import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class GBActivity extends AppCompatActivity { + private void setLanguage(String language) { + Locale locale; + if (language.equals("default")) { + locale = Locale.getDefault(); + } else { + locale = new Locale(language); + } + Configuration config = new Configuration(); + config.locale = locale; + + // FIXME: I have no idea what I am doing + getApplicationContext().getResources().updateConfiguration(config, getApplicationContext().getResources().getDisplayMetrics()); + getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); + } + @Override protected void onCreate(Bundle savedInstanceState) { if (GBApplication.isDarkThemeEnabled()) { @@ -17,6 +36,9 @@ public class GBActivity extends AppCompatActivity { setTheme(R.style.GadgetbridgeTheme); } + Prefs prefs = GBApplication.getPrefs(); + String language = prefs.getString("language", "default"); + setLanguage(language); super.onCreate(savedInstanceState); } } diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 937c29f23..8cf1b2874 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -11,6 +11,36 @@ light dark + + System Default + Deutsch + English + Español + Français + Polski + Русский + Tiếng Việt + Türkçe + Українська + 한국어 + 日本語 + + + + default + de + en + es + fr + pl + ru + vi + tr + uk + ko + ja + + @string/always @string/when_screen_off diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 53a01068d..99275ef8c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,8 @@ Light Dark + Language + Notifications Repetitions Phone Calls diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 39aac5595..aa7843311 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -23,6 +23,13 @@ android:entryValues="@array/pref_theme_values" android:defaultValue="@string/pref_theme_value_light" android:summary="%s" /> +