app language: refactoring

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.
This commit is contained in:
bopol 2020-01-28 20:48:42 +01:00
parent 156a2eb4ff
commit edc9d47da7
14 changed files with 43 additions and 52 deletions

View File

@ -31,7 +31,6 @@ import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
@ -78,8 +77,7 @@ import org.schabi.newpipe.util.ThemeHelper;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity"; private static final String TAG = "MainActivity";
@ -118,7 +116,7 @@ public class MainActivity extends AppCompatActivity {
} }
ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this)); ThemeHelper.setTheme(this, ServiceHelper.getSelectedServiceId(this));
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
@ -422,7 +420,7 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
Localization.init(getApplicationContext()); //change the date format to match the selected language on resume Localization.init(getApplicationContext()); //change the date format to match the selected language on resume
super.onResume(); super.onResume();

View File

@ -22,11 +22,9 @@ import android.widget.TextView;
import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.BuildConfig;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
public class AboutActivity extends AppCompatActivity { public class AboutActivity extends AppCompatActivity {
@ -65,7 +63,7 @@ public class AboutActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ThemeHelper.setTheme(this); ThemeHelper.setTheme(this);
this.setTitle(getString(R.string.title_activity_about)); this.setTitle(getString(R.string.title_activity_about));

View File

@ -12,14 +12,12 @@ import android.view.MenuItem;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.settings.SettingsActivity;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import us.shandian.giga.service.DownloadManagerService; import us.shandian.giga.service.DownloadManagerService;
import us.shandian.giga.ui.fragment.MissionsFragment; import us.shandian.giga.ui.fragment.MissionsFragment;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
public class DownloadActivity extends AppCompatActivity { public class DownloadActivity extends AppCompatActivity {
@ -32,7 +30,7 @@ public class DownloadActivity extends AppCompatActivity {
i.setClass(this, DownloadManagerService.class); i.setClass(this, DownloadManagerService.class);
startService(i); startService(i);
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
ThemeHelper.setTheme(this); ThemeHelper.setTheme(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_downloader); setContentView(R.layout.activity_downloader);

View File

@ -58,9 +58,7 @@ import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import static org.schabi.newpipe.player.helper.PlayerHelper.getTimeString; import static org.schabi.newpipe.player.helper.PlayerHelper.getTimeString;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
/** /**
* Base players joining the common properties * Base players joining the common properties
@ -117,7 +115,7 @@ public final class BackgroundPlayer extends Service {
notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
lockManager = new LockManager(this); lockManager = new LockManager(this);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
ThemeHelper.setTheme(this); ThemeHelper.setTheme(this);
basePlayerImpl = new BasePlayerImpl(this); basePlayerImpl = new BasePlayerImpl(this);
basePlayerImpl.setup(); basePlayerImpl.setup();

View File

@ -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.Type.SLIDE_AND_ALPHA;
import static org.schabi.newpipe.util.AnimationUtils.animateRotation; import static org.schabi.newpipe.util.AnimationUtils.animateRotation;
import static org.schabi.newpipe.util.AnimationUtils.animateView; import static org.schabi.newpipe.util.AnimationUtils.animateView;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
import static org.schabi.newpipe.util.StateSaver.KEY_SAVED_STATE; import static org.schabi.newpipe.util.StateSaver.KEY_SAVED_STATE;
/** /**
@ -127,7 +126,7 @@ public final class MainVideoPlayer extends AppCompatActivity
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]"); if (DEBUG) Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
defaultPreferences = PreferenceManager.getDefaultSharedPreferences(this); defaultPreferences = PreferenceManager.getDefaultSharedPreferences(this);
@ -195,7 +194,7 @@ public final class MainVideoPlayer extends AppCompatActivity
@Override @Override
protected void onResume() { protected void onResume() {
if (DEBUG) Log.d(TAG, "onResume() called"); if (DEBUG) Log.d(TAG, "onResume() called");
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onResume(); super.onResume();
if (globalScreenOrientationLocked()) { if (globalScreenOrientationLocked()) {
@ -226,7 +225,7 @@ public final class MainVideoPlayer extends AppCompatActivity
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
if (playerImpl.isSomePopupMenuVisible()) { if (playerImpl.isSomePopupMenuVisible()) {
playerImpl.getQualityPopupMenu().dismiss(); playerImpl.getQualityPopupMenu().dismiss();

View File

@ -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_DURATION;
import static org.schabi.newpipe.player.VideoPlayer.DEFAULT_CONTROLS_HIDE_TIME; 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.AnimationUtils.animateView;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
/** /**
* Service Popup Player implementing VideoPlayer * Service Popup Player implementing VideoPlayer
@ -144,7 +143,7 @@ public final class PopupVideoPlayer extends Service {
@Override @Override
public void onCreate() { public void onCreate() {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)); notificationManager = ((NotificationManager) getSystemService(NOTIFICATION_SERVICE));
@ -172,7 +171,7 @@ public final class PopupVideoPlayer extends Service {
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
if (DEBUG) Log.d(TAG, "onConfigurationChanged() called with: newConfig = [" + newConfig + "]"); if (DEBUG) Log.d(TAG, "onConfigurationChanged() called with: newConfig = [" + newConfig + "]");
updateScreenSize(); updateScreenSize();
updatePopupSize(popupLayoutParams.width, -1); updatePopupSize(popupLayoutParams.width, -1);

View File

@ -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.formatPitch;
import static org.schabi.newpipe.player.helper.PlayerHelper.formatSpeed; import static org.schabi.newpipe.player.helper.PlayerHelper.formatSpeed;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
public abstract class ServicePlayerActivity extends AppCompatActivity public abstract class ServicePlayerActivity extends AppCompatActivity
implements PlayerEventListener, SeekBar.OnSeekBarChangeListener, implements PlayerEventListener, SeekBar.OnSeekBarChangeListener,
@ -118,7 +117,7 @@ public abstract class ServicePlayerActivity extends AppCompatActivity
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ThemeHelper.setTheme(this); ThemeHelper.setTheme(this);
setContentView(R.layout.activity_player_queue_control); setContentView(R.layout.activity_player_queue_control);

View File

@ -17,8 +17,7 @@ import org.schabi.newpipe.R;
import org.schabi.newpipe.util.SliderStrategy; import org.schabi.newpipe.util.SliderStrategy;
import static org.schabi.newpipe.player.BasePlayer.DEBUG; import static org.schabi.newpipe.player.BasePlayer.DEBUG;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
public class PlaybackParameterDialog extends DialogFragment { public class PlaybackParameterDialog extends DialogFragment {
@NonNull private static final String TAG = "PlaybackParameterDialog"; @NonNull private static final String TAG = "PlaybackParameterDialog";
@ -110,7 +109,7 @@ public class PlaybackParameterDialog extends DialogFragment {
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
changeAppLanguage(getAppLocale(getContext()), getResources()); assureCorrectAppLanguage(getContext());
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (savedInstanceState != null) { if (savedInstanceState != null) {
initialTempo = savedInstanceState.getDouble(INITIAL_TEMPO_KEY, DEFAULT_TEMPO); initialTempo = savedInstanceState.getDouble(INITIAL_TEMPO_KEY, DEFAULT_TEMPO);
@ -140,7 +139,7 @@ public class PlaybackParameterDialog extends DialogFragment {
@NonNull @NonNull
@Override @Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
changeAppLanguage(getAppLocale(getContext()), getResources()); assureCorrectAppLanguage(getContext());
final View view = View.inflate(getContext(), R.layout.dialog_playback_parameter, null); final View view = View.inflate(getContext(), R.layout.dialog_playback_parameter, null);
setupControlViews(view); setupControlViews(view);

View File

@ -46,8 +46,7 @@ import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.Vector; import java.util.Vector;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
/* /*
* Created by Christian Schabesberger on 24.10.15. * Created by Christian Schabesberger on 24.10.15.
@ -174,7 +173,7 @@ public class ErrorActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ThemeHelper.setTheme(this); ThemeHelper.setTheme(this);
setContentView(R.layout.activity_error); setContentView(R.layout.activity_error);

View File

@ -14,9 +14,7 @@ import android.view.MenuItem;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
import static org.schabi.newpipe.util.Localization.changeAppLanguage; import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
import static org.schabi.newpipe.util.Localization.getAppLocale;
/* /*
* Created by Christian Schabesberger on 31.08.15. * Created by Christian Schabesberger on 31.08.15.
@ -47,7 +45,7 @@ public class SettingsActivity extends AppCompatActivity implements BasePreferenc
@Override @Override
protected void onCreate(Bundle savedInstanceBundle) { protected void onCreate(Bundle savedInstanceBundle) {
setTheme(ThemeHelper.getSettingsThemeStyle(this)); setTheme(ThemeHelper.getSettingsThemeStyle(this));
changeAppLanguage(getAppLocale(getApplicationContext()), getResources()); assureCorrectAppLanguage(this);
super.onCreate(savedInstanceBundle); super.onCreate(savedInstanceBundle);
setContentView(R.layout.settings_layout); setContentView(R.layout.settings_layout);

View File

@ -218,7 +218,7 @@ public class Localization {
return getPrettyTime().formatUnrounded(calendarTime); return getPrettyTime().formatUnrounded(calendarTime);
} }
public static void changeAppLanguage(Locale loc, Resources res) { private static void changeAppLanguage(Locale loc, Resources res) {
DisplayMetrics dm = res.getDisplayMetrics(); DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration(); Configuration conf = res.getConfiguration();
conf.setLocale(loc); conf.setLocale(loc);
@ -227,11 +227,13 @@ public class Localization {
public static Locale getAppLocale(Context context) { public static Locale getAppLocale(Context context) {
SharedPreferences prefs = androidx.preference.PreferenceManager.getDefaultSharedPreferences(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; Locale loc;
if (lang.equals("system")) { if (lang.equals("system")) {
loc = Locale.getDefault(); loc = Locale.getDefault();
} else if (lang.matches(".*-.*")) { } 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("-"); String[] localisation = lang.split("-");
lang = localisation[0]; lang = localisation[0];
String country = localisation[1]; String country = localisation[1];
@ -241,4 +243,8 @@ public class Localization {
} }
return loc; return loc;
} }
public static void assureCorrectAppLanguage(Context c) {
changeAppLanguage(getAppLocale(c), c.getResources());
}
} }

View File

@ -176,7 +176,7 @@
<string name="enable_playback_resume_key" translatable="false">enable_playback_resume</string> <string name="enable_playback_resume_key" translatable="false">enable_playback_resume</string>
<string name="enable_playback_state_lists_key" translatable="false">enable_playback_state_lists</string> <string name="enable_playback_state_lists_key" translatable="false">enable_playback_state_lists</string>
<string name="newpipes_language_key" translatable="false">newpipes_language_key</string> <string name="app_language_key" translatable="false">app_language_key</string>
<string name="enable_lock_screen_video_thumbnail_key" translatable="false">enable_lock_screen_video_thumbnail</string> <string name="enable_lock_screen_video_thumbnail_key" translatable="false">enable_lock_screen_video_thumbnail</string>
<string name="import_data" translatable="false">import_data</string> <string name="import_data" translatable="false">import_data</string>
@ -926,8 +926,8 @@
<item>ZW</item> <item>ZW</item>
</string-array> </string-array>
<!--The next two arrays are set to change the NewPipe language--> <!--The next two arrays are set to change the app language-->
<string-array name="language_code_np" translatable="false"> <string-array name="app_language_code" translatable="false">
<item>system</item> <item>system</item>
<item>ar</item> <item>ar</item>
<item>az</item> <item>az</item>
@ -992,7 +992,7 @@
<item>zh-hans</item> <item>zh-hans</item>
<item>zh-tw</item> <item>zh-tw</item>
</string-array> </string-array>
<string-array name="language_name_np" translatable="false"> <string-array name="app_language_name" translatable="false">
<item>@string/systems_language</item> <item>@string/systems_language</item>
<item>العربية</item> <item>العربية</item>
<item>Azərbaycan dili</item> <item>Azərbaycan dili</item>

View File

@ -589,6 +589,6 @@
<string name="downloads_storage_use_saf_title">Use SAF</string> <string name="downloads_storage_use_saf_title">Use SAF</string>
<string name="downloads_storage_use_saf_summary">The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible</string> <string name="downloads_storage_use_saf_summary">The Storage Access Framework allows downloads to an external SD card.\nNote: some devices are not compatible</string>
<string name="choose_instance_prompt">Choose an instance</string> <string name="choose_instance_prompt">Choose an instance</string>
<string name="newpipes_language_title">NewPipe\'s language</string> <string name="app_language_title">App language</string>
<string name="systems_language">System\'s language</string> <string name="systems_language">System default</string>
</resources> </resources>

View File

@ -7,11 +7,11 @@
<ListPreference <ListPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"
android:defaultValue="system" android:defaultValue="system"
android:entries="@array/language_name_np" android:entries="@array/app_language_name"
android:entryValues="@array/language_code_np" android:entryValues="@array/app_language_code"
android:key="@string/newpipes_language_key" android:key="@string/app_language_key"
android:summary="%s" android:summary="%s"
android:title="@string/newpipes_language_title"/> android:title="@string/app_language_title"/>
<ListPreference <ListPreference
app:iconSpaceReserved="false" app:iconSpaceReserved="false"