fix(YouTube - Settings): RVX language no longer changes the YouTube app language

This commit is contained in:
inotia00 2025-03-30 18:12:07 +09:00
parent 1dd7eda606
commit aac38dc8af
3 changed files with 59 additions and 42 deletions

View File

@ -60,6 +60,7 @@ public class Utils {
private static WeakReference<Activity> activityRef = new WeakReference<>(null); private static WeakReference<Activity> activityRef = new WeakReference<>(null);
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
private static volatile Context context; private static volatile Context context;
private static Locale contextLocale;
protected Utils() { protected Utils() {
} // utility class } // utility class
@ -308,34 +309,51 @@ public class Utils {
* @return Context with locale applied. * @return Context with locale applied.
*/ */
public static Context getLocalizedContext(Context mContext) { public static Context getLocalizedContext(Context mContext) {
Activity mActivity = activityRef.get(); try {
if (mActivity == null) { Activity mActivity = activityRef.get();
return mContext; if (mActivity != null && mContext != null) {
} AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get();
if (mContext == null) {
return null; // Locale of Application.
Locale applicationLocale = language == AppLanguage.DEFAULT
? mActivity.getResources().getConfiguration().locale
: language.getLocale();
// Locale of Context.
Locale contextLocale = mContext.getResources().getConfiguration().locale;
// If they are different, overrides the Locale of the Context and resource.
if (applicationLocale != contextLocale) {
Utils.contextLocale = contextLocale;
// If they are different, overrides the Locale of the Context and resource.
Locale.setDefault(applicationLocale);
Configuration configuration = new Configuration(mContext.getResources().getConfiguration());
configuration.setLocale(applicationLocale);
return mContext.createConfigurationContext(configuration);
}
}
} catch (Exception ex) {
Logger.printException(() -> "getLocalizedContext failed", ex);
} }
AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get(); return mContext;
}
// Locale of Application. public static void resetLocalizedContext() {
Locale applicationLocale = language == AppLanguage.DEFAULT try {
? mActivity.getResources().getConfiguration().locale if (contextLocale != null) {
: language.getLocale(); Locale.setDefault(contextLocale);
Context mContext = getContext();
// Locale of Context. if (mContext != null) {
Locale contextLocale = mContext.getResources().getConfiguration().locale; Configuration config = mContext.getResources().getConfiguration();
config.setLocale(contextLocale);
// If they are identical, no need to override them. setContext(mContext.createConfigurationContext(config));
if (applicationLocale == contextLocale) { }
return mContext; }
} catch (Exception ex) {
Logger.printException(() -> "resetLocalizedContext failed", ex);
} }
// If they are different, overrides the Locale of the Context and resource.
Locale.setDefault(applicationLocale);
Configuration configuration = new Configuration(mContext.getResources().getConfiguration());
configuration.setLocale(applicationLocale);
return mContext.createConfigurationContext(configuration);
} }
public static void setActivity(Activity mainActivity) { public static void setActivity(Activity mainActivity) {
@ -353,14 +371,6 @@ public class Utils {
// Must initially set context to check the app language. // Must initially set context to check the app language.
context = appContext; context = appContext;
Logger.initializationInfo(Utils.class, "Set context: " + appContext); Logger.initializationInfo(Utils.class, "Set context: " + appContext);
AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get();
if (language != AppLanguage.DEFAULT) {
// Create a new context with the desired language.
Configuration config = appContext.getResources().getConfiguration();
config.setLocale(language.getLocale());
context = appContext.createConfigurationContext(config);
}
} }
public static void setClipboard(@NonNull String text) { public static void setClipboard(@NonNull String text) {
@ -538,14 +548,6 @@ public class Utils {
return Build.VERSION.SDK_INT >= sdk; return Build.VERSION.SDK_INT >= sdk;
} }
public static int dpToPx(float dp) {
if (context == null) {
return (int) dp;
} else {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}
}
public static int dpToPx(int dp) { public static int dpToPx(int dp) {
if (context == null) { if (context == null) {
return dp; return dp;

View File

@ -377,6 +377,7 @@ public class ReVancedPreferenceFragment extends PreferenceFragment {
@Override @Override
public void onDestroy() { public void onDestroy() {
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener); mSharedPreferences.unregisterOnSharedPreferenceChangeListener(listener);
Utils.resetLocalizedContext();
super.onDestroy(); super.onDestroy();
} }

View File

@ -1,7 +1,9 @@
package com.google.android.apps.youtube.app.settings.videoquality; package com.google.android.apps.youtube.app.settings.videoquality;
import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle; import android.os.Bundle;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.View; import android.view.View;
@ -25,8 +27,8 @@ import app.revanced.extension.youtube.utils.ThemeUtils;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class VideoQualitySettingsActivity extends Activity { public class VideoQualitySettingsActivity extends Activity {
private static final String rvxSettingsLabel = ResourceUtils.getString("revanced_extended_settings_title"); private static String rvxSettingsLabel;
private static final String searchLabel = ResourceUtils.getString("revanced_extended_settings_search_title"); private static String searchLabel;
private static WeakReference<SearchView> searchViewRef = new WeakReference<>(null); private static WeakReference<SearchView> searchViewRef = new WeakReference<>(null);
private static WeakReference<ImageView> closeButtonRef = new WeakReference<>(null); private static WeakReference<ImageView> closeButtonRef = new WeakReference<>(null);
private ReVancedPreferenceFragment fragment; private ReVancedPreferenceFragment fragment;
@ -71,6 +73,10 @@ public class VideoQualitySettingsActivity extends Activity {
return; return;
} }
// Set label
rvxSettingsLabel = getString("revanced_extended_settings_title");
searchLabel = getString("revanced_extended_settings_search_title");
// Set toolbar // Set toolbar
setToolbar(); setToolbar();
@ -85,6 +91,14 @@ public class VideoQualitySettingsActivity extends Activity {
} }
} }
@SuppressLint("DiscouragedApi")
private String getString(String str) {
Context baseContext = getBaseContext();
Resources resources = baseContext.getResources();
int identifier = resources.getIdentifier(str, "string", baseContext.getPackageName());
return resources.getString(identifier);
}
private void filterPreferences(String query) { private void filterPreferences(String query) {
if (fragment == null) return; if (fragment == null) return;
fragment.filterPreferences(query); fragment.filterPreferences(query);