Skip to content

Commit 205466c

Browse files
committed
Move call to setApplicationLocales
1 parent 7f10312 commit 205466c

3 files changed

Lines changed: 33 additions & 33 deletions

File tree

app/src/main/java/org/schabi/newpipe/MainActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ protected void onCreate(final Bundle savedInstanceState) {
190190
&& ReleaseVersionUtil.INSTANCE.isReleaseApk()) {
191191
UpdateSettingsFragment.askForConsentToUpdateChecks(this);
192192
}
193+
194+
Localization.migrateAppLanguageSettingIfNecessary(getApplicationContext());
193195
}
194196

195197
@Override

app/src/main/java/org/schabi/newpipe/settings/NewPipeSettings.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@
66
import android.content.SharedPreferences;
77
import android.os.Build;
88
import android.os.Environment;
9-
import android.util.Log;
109

1110
import androidx.annotation.NonNull;
1211
import androidx.annotation.StringRes;
13-
import androidx.appcompat.app.AppCompatDelegate;
14-
import androidx.core.os.LocaleListCompat;
1512
import androidx.preference.PreferenceManager;
1613

1714
import org.schabi.newpipe.App;
@@ -45,7 +42,6 @@
4542
* Helper class for global settings.
4643
*/
4744
public final class NewPipeSettings {
48-
private static final String TAG = NewPipeSettings.class.toString();
4945
private NewPipeSettings() { }
5046

5147
public static void initSettings(final Context context) {
@@ -68,7 +64,6 @@ public static void initSettings(final Context context) {
6864
saveDefaultAudioDownloadDirectory(context);
6965

7066
disableMediaTunnelingIfNecessary(context);
71-
migrateAppLanguageSettingIfNecessary(context);
7267
}
7368

7469
static void saveDefaultVideoDownloadDirectory(final Context context) {
@@ -189,32 +184,4 @@ public static void setMediaTunneling(@NonNull final Context context) {
189184
DeviceUtils.MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION).apply();
190185
}
191186
}
192-
193-
private static void migrateAppLanguageSettingIfNecessary(@NonNull final Context context) {
194-
// Starting with pull request #12093, NewPipe on Android 13+ exclusively uses Android's
195-
// public per-app language APIs to read and set the UI language for NewPipe.
196-
// If running on Android 13+, the following migration will move any existing custom
197-
// app language in SharedPreferences to use the public per-app language APIs instead.
198-
if (Build.VERSION.SDK_INT >= 33) {
199-
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
200-
final String appLanguageKey = context.getString(R.string.app_language_key);
201-
final String appLanguageValue = sp.getString(appLanguageKey, null);
202-
if (appLanguageValue != null) {
203-
sp.edit().remove(appLanguageKey).apply();
204-
final String appLanguageDefaultValue =
205-
context.getString(R.string.default_localization_key);
206-
if (!appLanguageValue.equals(appLanguageDefaultValue)) {
207-
try {
208-
AppCompatDelegate.setApplicationLocales(
209-
LocaleListCompat.forLanguageTags(appLanguageValue)
210-
);
211-
} catch (final RuntimeException e) {
212-
Log.e(TAG, "Failed to migrate previous custom app language "
213-
+ "setting to public per-app language APIs"
214-
);
215-
}
216-
}
217-
}
218-
}
219-
}
220187
}

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
import android.text.TextUtils;
1313
import android.text.format.DateUtils;
1414
import android.util.DisplayMetrics;
15+
import android.util.Log;
1516

1617
import androidx.annotation.NonNull;
1718
import androidx.annotation.Nullable;
1819
import androidx.annotation.PluralsRes;
1920
import androidx.annotation.StringRes;
2021
import androidx.appcompat.app.AppCompatDelegate;
2122
import androidx.core.math.MathUtils;
23+
import androidx.core.os.LocaleListCompat;
2224
import androidx.preference.PreferenceManager;
2325

2426
import org.ocpsoft.prettytime.PrettyTime;
@@ -65,6 +67,7 @@
6567
*/
6668

6769
public final class Localization {
70+
private static final String TAG = Localization.class.toString();
6871
public static final String DOT_SEPARATOR = " • ";
6972
private static PrettyTime prettyTime;
7073

@@ -433,4 +436,32 @@ private static String getQuantity(@NonNull final Context context,
433436
final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE);
434437
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
435438
}
439+
440+
public static void migrateAppLanguageSettingIfNecessary(@NonNull final Context context) {
441+
// Starting with pull request #12093, NewPipe on Android 13+ exclusively uses Android's
442+
// public per-app language APIs to read and set the UI language for NewPipe.
443+
// If running on Android 13+, the following code will migrate any existing custom
444+
// app language in SharedPreferences to use the public per-app language APIs instead.
445+
if (Build.VERSION.SDK_INT >= 33) {
446+
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
447+
final String appLanguageKey = context.getString(R.string.app_language_key);
448+
final String appLanguageValue = sp.getString(appLanguageKey, null);
449+
if (appLanguageValue != null) {
450+
sp.edit().remove(appLanguageKey).apply();
451+
final String appLanguageDefaultValue =
452+
context.getString(R.string.default_localization_key);
453+
if (!appLanguageValue.equals(appLanguageDefaultValue)) {
454+
try {
455+
AppCompatDelegate.setApplicationLocales(
456+
LocaleListCompat.forLanguageTags(appLanguageValue)
457+
);
458+
} catch (final RuntimeException e) {
459+
Log.e(TAG, "Failed to migrate previous custom app language "
460+
+ "setting to public per-app language APIs"
461+
);
462+
}
463+
}
464+
}
465+
}
466+
}
436467
}

0 commit comments

Comments
 (0)