From edc9d47da795efbc1606efae50788bdc81f4468b Mon Sep 17 00:00:00 2001 From: bopol Date: Tue, 28 Jan 2020 20:48:42 +0100 Subject: [PATCH] app language: refactoring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit renamed NewPipe's language into App language, and same for all the concerning thing (keys, comments…) we now call assureCorrectAppLanguage(CONTEXT) in activities needing it instead of changeAppLanguage(getAppLocale(CONTEXT), RESOURCES) changeAppLanguage becomes private. --- app/src/main/java/org/schabi/newpipe/MainActivity.java | 8 +++----- .../java/org/schabi/newpipe/about/AboutActivity.java | 6 ++---- .../org/schabi/newpipe/download/DownloadActivity.java | 6 ++---- .../org/schabi/newpipe/player/BackgroundPlayer.java | 6 ++---- .../org/schabi/newpipe/player/MainVideoPlayer.java | 9 ++++----- .../org/schabi/newpipe/player/PopupVideoPlayer.java | 7 +++---- .../schabi/newpipe/player/ServicePlayerActivity.java | 5 ++--- .../newpipe/player/helper/PlaybackParameterDialog.java | 7 +++---- .../java/org/schabi/newpipe/report/ErrorActivity.java | 5 ++--- .../org/schabi/newpipe/settings/SettingsActivity.java | 6 ++---- .../java/org/schabi/newpipe/util/Localization.java | 10 ++++++++-- app/src/main/res/values/settings_keys.xml | 8 ++++---- app/src/main/res/values/strings.xml | 4 ++-- app/src/main/res/xml/content_settings.xml | 8 ++++---- 14 files changed, 43 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MainActivity.java b/app/src/main/java/org/schabi/newpipe/MainActivity.java index 63bc8df15..7cd620faa 100644 --- a/app/src/main/java/org/schabi/newpipe/MainActivity.java +++ b/app/src/main/java/org/schabi/newpipe/MainActivity.java @@ -31,7 +31,6 @@ import android.preference.PreferenceManager; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; -import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; @@ -78,8 +77,7 @@ import org.schabi.newpipe.util.ThemeHelper; import java.util.ArrayList; import java.util.List; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @@ -118,7 +116,7 @@ public class MainActivity extends AppCompatActivity { } ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this)); - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); @@ -422,7 +420,7 @@ public class MainActivity extends AppCompatActivity { @Override protected void onResume() { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); Localization.init(getApplicationContext()); //change the date format to match the selected language on resume super.onResume(); diff --git a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java index 4d9cfc7e1..9e23d9d3d 100644 --- a/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java +++ b/app/src/main/java/org/schabi/newpipe/about/AboutActivity.java @@ -22,11 +22,9 @@ import android.widget.TextView; import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.R; -import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.ThemeHelper; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class AboutActivity extends AppCompatActivity { @@ -65,7 +63,7 @@ public class AboutActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); ThemeHelper.setTheme(this); this.setTitle(getString(R.string.title_activity_about)); diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java index 7aaaeeb17..00dd45ac9 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadActivity.java @@ -12,14 +12,12 @@ import android.view.MenuItem; import android.view.ViewTreeObserver; import org.schabi.newpipe.R; -import org.schabi.newpipe.settings.SettingsActivity; import org.schabi.newpipe.util.ThemeHelper; import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.ui.fragment.MissionsFragment; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class DownloadActivity extends AppCompatActivity { @@ -32,7 +30,7 @@ public class DownloadActivity extends AppCompatActivity { i.setClass(this, DownloadManagerService.class); startService(i); - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); ThemeHelper.setTheme(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_downloader); diff --git a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java index c74882161..9e23d9145 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BackgroundPlayer.java @@ -58,9 +58,7 @@ import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.ThemeHelper; import static org.schabi.newpipe.player.helper.PlayerHelper.getTimeString; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; - +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; /** * Base players joining the common properties @@ -117,7 +115,7 @@ public final class BackgroundPlayer extends Service { notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); lockManager = new LockManager(this); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); ThemeHelper.setTheme(this); basePlayerImpl = new BasePlayerImpl(this); basePlayerImpl.setup(); diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index 487629c12..31f7dd74a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -95,8 +95,7 @@ import static org.schabi.newpipe.util.AnimationUtils.Type.SCALE_AND_ALPHA; import static org.schabi.newpipe.util.AnimationUtils.Type.SLIDE_AND_ALPHA; import static org.schabi.newpipe.util.AnimationUtils.animateRotation; import static org.schabi.newpipe.util.AnimationUtils.animateView; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; import static org.schabi.newpipe.util.StateSaver.KEY_SAVED_STATE; /** @@ -127,7 +126,7 @@ public final class MainVideoPlayer extends AppCompatActivity @Override protected void onCreate(@Nullable Bundle savedInstanceState) { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]"); defaultPreferences = PreferenceManager.getDefaultSharedPreferences(this); @@ -195,7 +194,7 @@ public final class MainVideoPlayer extends AppCompatActivity @Override protected void onResume() { if (DEBUG) Log.d(TAG, "onResume() called"); - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onResume(); if (globalScreenOrientationLocked()) { @@ -226,7 +225,7 @@ public final class MainVideoPlayer extends AppCompatActivity @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); if (playerImpl.isSomePopupMenuVisible()) { playerImpl.getQualityPopupMenu().dismiss(); diff --git a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java index 96bc606cc..fc14e8d51 100644 --- a/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/PopupVideoPlayer.java @@ -80,8 +80,7 @@ import static org.schabi.newpipe.player.BasePlayer.STATE_PLAYING; import static org.schabi.newpipe.player.VideoPlayer.DEFAULT_CONTROLS_DURATION; import static org.schabi.newpipe.player.VideoPlayer.DEFAULT_CONTROLS_HIDE_TIME; import static org.schabi.newpipe.util.AnimationUtils.animateView; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; /** * Service Popup Player implementing VideoPlayer @@ -144,7 +143,7 @@ public final class PopupVideoPlayer extends Service { @Override public void onCreate() { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); @@ -172,7 +171,7 @@ public final class PopupVideoPlayer extends Service { @Override public void onConfigurationChanged(Configuration newConfig) { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); if (DEBUG) Log.d(TAG, "onConfigurationChanged() called with: newConfig = [" + newConfig + "]"); updateScreenSize(); updatePopupSize(popupLayoutParams.width, -1); diff --git a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java index 1c6d786cd..669d1c16c 100644 --- a/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java +++ b/app/src/main/java/org/schabi/newpipe/player/ServicePlayerActivity.java @@ -46,8 +46,7 @@ import java.util.List; import static org.schabi.newpipe.player.helper.PlayerHelper.formatPitch; import static org.schabi.newpipe.player.helper.PlayerHelper.formatSpeed; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public abstract class ServicePlayerActivity extends AppCompatActivity implements PlayerEventListener, SeekBar.OnSeekBarChangeListener, @@ -118,7 +117,7 @@ public abstract class ServicePlayerActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); ThemeHelper.setTheme(this); setContentView(R.layout.activity_player_queue_control); diff --git a/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java b/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java index 2aefa675e..3ada3a6be 100644 --- a/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java +++ b/app/src/main/java/org/schabi/newpipe/player/helper/PlaybackParameterDialog.java @@ -17,8 +17,7 @@ import org.schabi.newpipe.R; import org.schabi.newpipe.util.SliderStrategy; import static org.schabi.newpipe.player.BasePlayer.DEBUG; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; public class PlaybackParameterDialog extends DialogFragment { @NonNull private static final String TAG = "PlaybackParameterDialog"; @@ -110,7 +109,7 @@ public class PlaybackParameterDialog extends DialogFragment { @Override public void onCreate(@Nullable Bundle savedInstanceState) { - changeAppLanguage(getAppLocale(getContext()), getResources()); + assureCorrectAppLanguage(getContext()); super.onCreate(savedInstanceState); if (savedInstanceState != null) { initialTempo = savedInstanceState.getDouble(INITIAL_TEMPO_KEY, DEFAULT_TEMPO); @@ -140,7 +139,7 @@ public class PlaybackParameterDialog extends DialogFragment { @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { - changeAppLanguage(getAppLocale(getContext()), getResources()); + assureCorrectAppLanguage(getContext()); final View view = View.inflate(getContext(), R.layout.dialog_playback_parameter, null); setupControlViews(view); diff --git a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java index 05dfe33ca..4512ab3a6 100644 --- a/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java +++ b/app/src/main/java/org/schabi/newpipe/report/ErrorActivity.java @@ -46,8 +46,7 @@ import java.util.List; import java.util.TimeZone; import java.util.Vector; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; /* * Created by Christian Schabesberger on 24.10.15. @@ -174,7 +173,7 @@ public class ErrorActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onCreate(savedInstanceState); ThemeHelper.setTheme(this); setContentView(R.layout.activity_error); diff --git a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java index 6804f9831..53d60f86c 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/settings/SettingsActivity.java @@ -14,9 +14,7 @@ import android.view.MenuItem; import org.schabi.newpipe.R; import org.schabi.newpipe.util.ThemeHelper; -import static org.schabi.newpipe.util.Localization.changeAppLanguage; -import static org.schabi.newpipe.util.Localization.getAppLocale; - +import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage; /* * Created by Christian Schabesberger on 31.08.15. @@ -47,7 +45,7 @@ public class SettingsActivity extends AppCompatActivity implements BasePreferenc @Override protected void onCreate(Bundle savedInstanceBundle) { setTheme(ThemeHelper.getSettingsThemeStyle(this)); - changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); + assureCorrectAppLanguage(this); super.onCreate(savedInstanceBundle); setContentView(R.layout.settings_layout); diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index d2fbd3a65..6149118cc 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -218,7 +218,7 @@ public class Localization { return getPrettyTime().formatUnrounded(calendarTime); } - public static void changeAppLanguage(Locale loc, Resources res) { + private static void changeAppLanguage(Locale loc, Resources res) { DisplayMetrics dm = res.getDisplayMetrics(); Configuration conf = res.getConfiguration(); conf.setLocale(loc); @@ -227,11 +227,13 @@ public class Localization { public static Locale getAppLocale(Context context) { SharedPreferences prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(context); - String lang = prefs.getString("newpipes_language_key", "en"); + String lang = prefs.getString("app_language_key", "en"); Locale loc; if (lang.equals("system")) { loc = Locale.getDefault(); } else if (lang.matches(".*-.*")) { + //to differentiate different versions of the language + //for example, pt (portuguese in Portugal) and pt-br (portuguese in Brazil) String[] localisation = lang.split("-"); lang = localisation[0]; String country = localisation[1]; @@ -241,4 +243,8 @@ public class Localization { } return loc; } + + public static void assureCorrectAppLanguage(Context c) { + changeAppLanguage(getAppLocale(c), c.getResources()); + } } diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index ed3c454b8..1a7d61dae 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -176,7 +176,7 @@ enable_playback_resume enable_playback_state_lists - newpipes_language_key + app_language_key enable_lock_screen_video_thumbnail import_data @@ -926,8 +926,8 @@ ZW - - + + system ar az @@ -992,7 +992,7 @@ zh-hans zh-tw - + @string/systems_language العربية Azərbaycan dili diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c5a59abe7..29864963f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -589,6 +589,6 @@ Use SAF The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible Choose an instance - NewPipe\'s language - System\'s language + App language + System default diff --git a/app/src/main/res/xml/content_settings.xml b/app/src/main/res/xml/content_settings.xml index ab9896cde..197c14487 100644 --- a/app/src/main/res/xml/content_settings.xml +++ b/app/src/main/res/xml/content_settings.xml @@ -7,11 +7,11 @@ + android:title="@string/app_language_title"/>