From 69a6d9ebfb82f0b3826f593ab89c794372faad69 Mon Sep 17 00:00:00 2001 From: kosharskiy Date: Thu, 14 Jan 2021 18:42:27 +0200 Subject: [PATCH] fixed default app language on first start; the same fix made for video filter --- .../peertube/activity/CommonActivity.java | 37 ++++++++++++------- .../peertube/activity/VideoListActivity.java | 11 +++++- app/src/main/res/xml/root_preferences.xml | 2 - 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/net/schueller/peertube/activity/CommonActivity.java b/app/src/main/java/net/schueller/peertube/activity/CommonActivity.java index b9c1ee7..d68ffe4 100644 --- a/app/src/main/java/net/schueller/peertube/activity/CommonActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/CommonActivity.java @@ -19,16 +19,17 @@ package net.schueller.peertube.activity; import android.content.SharedPreferences; import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceManager; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.app.AppCompatDelegate; - import net.schueller.peertube.R; import java.util.Locale; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.app.AppCompatDelegate; + public class CommonActivity extends AppCompatActivity { @Override @@ -51,21 +52,31 @@ public class CommonActivity extends AppCompatActivity { ); // Set language - String countryCode = sharedPref.getString(getString(R.string.pref_language_app_key), "en"); - assert countryCode != null; - Locale locale = new Locale(countryCode); + String countryCode = sharedPref.getString(getString(R.string.pref_language_app_key), null); + + if (countryCode == null) { + return; + } + + setLocale(countryCode); + } + + + public void setLocale(String languageCode) { + + Locale locale = new Locale(languageCode); //Neither Chinese language choice was working, found this fix on stack overflow - if (countryCode.equals("zh-rCN")) + if (languageCode.equals("zh-rCN")) locale = Locale.SIMPLIFIED_CHINESE; - if (countryCode.equals("zh-rTW")) + if (languageCode.equals("zh-rTW")) locale = Locale.TRADITIONAL_CHINESE; Locale.setDefault(locale); - Configuration config = getBaseContext().getResources().getConfiguration(); - config.locale = locale; - getBaseContext().getResources().updateConfiguration(config, - getBaseContext().getResources().getDisplayMetrics()); - } + Resources resources = getResources(); + Configuration config = resources.getConfiguration(); + config.setLocale(locale); + resources.updateConfiguration(config, resources.getDisplayMetrics()); + } } diff --git a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java index fbd0b6d..a760c4a 100644 --- a/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java +++ b/app/src/main/java/net/schueller/peertube/activity/VideoListActivity.java @@ -70,6 +70,8 @@ import net.schueller.peertube.service.VideoPlayerService; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Locale; import java.util.Set; import retrofit2.Call; @@ -321,7 +323,14 @@ public class VideoListActivity extends CommonActivity { SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); String nsfw = sharedPref.getBoolean(getString(R.string.pref_show_nsfw_key), false) ? "both" : "false"; - Set languages = sharedPref.getStringSet(getString(R.string.pref_video_language_key), null); + + Locale locale = getResources().getConfiguration().locale; + String country = locale.getLanguage(); + + HashSet countries = new HashSet<>(1); + countries.add(country); + + Set languages = sharedPref.getStringSet(getString(R.string.pref_video_language_key), countries); String apiBaseURL = APIUrlHelper.getUrlWithVersion(this); GetVideoDataService service = RetrofitInstance.getRetrofitInstance(apiBaseURL, APIUrlHelper.useInsecureConnection(this)).create(GetVideoDataService.class); diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index e96fdd9..96736b3 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -4,7 +4,6 @@