mirror of
https://github.com/inotia00/revanced-patches.git
synced 2025-06-12 21:27:43 +02:00
feat(YouTube - Settings): Add RVX language
setting https://github.com/inotia00/ReVanced_Extended/issues/2680
This commit is contained in:
@ -0,0 +1,114 @@
|
||||
package app.revanced.extension.shared.settings;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public enum AppLanguage {
|
||||
/**
|
||||
* The current app language.
|
||||
*/
|
||||
DEFAULT,
|
||||
|
||||
// Language codes found in locale_config.xml
|
||||
// All region specific variants have been removed.
|
||||
AF,
|
||||
AM,
|
||||
AR,
|
||||
AS,
|
||||
AZ,
|
||||
BE,
|
||||
BG,
|
||||
BN,
|
||||
BS,
|
||||
CA,
|
||||
CS,
|
||||
DA,
|
||||
DE,
|
||||
EL,
|
||||
EN,
|
||||
ES,
|
||||
ET,
|
||||
EU,
|
||||
FA,
|
||||
FI,
|
||||
FR,
|
||||
GL,
|
||||
GU,
|
||||
HI,
|
||||
HE, // App uses obsolete 'IW' and not the modern 'HE' ISO code.
|
||||
HR,
|
||||
HU,
|
||||
HY,
|
||||
ID,
|
||||
IS,
|
||||
IT,
|
||||
JA,
|
||||
KA,
|
||||
KK,
|
||||
KM,
|
||||
KN,
|
||||
KO,
|
||||
KY,
|
||||
LO,
|
||||
LT,
|
||||
LV,
|
||||
MK,
|
||||
ML,
|
||||
MN,
|
||||
MR,
|
||||
MS,
|
||||
MY,
|
||||
NE,
|
||||
NL,
|
||||
NB,
|
||||
OR,
|
||||
PA,
|
||||
PL,
|
||||
PT,
|
||||
RO,
|
||||
RU,
|
||||
SI,
|
||||
SK,
|
||||
SL,
|
||||
SQ,
|
||||
SR,
|
||||
SV,
|
||||
SW,
|
||||
TA,
|
||||
TE,
|
||||
TH,
|
||||
TL,
|
||||
TR,
|
||||
UK,
|
||||
UR,
|
||||
UZ,
|
||||
VI,
|
||||
ZH,
|
||||
ZU;
|
||||
|
||||
private final String language;
|
||||
|
||||
AppLanguage() {
|
||||
language = name().toLowerCase(Locale.US);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The 2 letter ISO 639_1 language code.
|
||||
*/
|
||||
public String getLanguage() {
|
||||
// Changing the app language does not force the app to completely restart,
|
||||
// so the default needs to be the current language and not a static field.
|
||||
if (this == DEFAULT) {
|
||||
return Locale.getDefault().getLanguage();
|
||||
}
|
||||
|
||||
return language;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
if (this == DEFAULT) {
|
||||
return Locale.getDefault();
|
||||
}
|
||||
|
||||
return Locale.forLanguageTag(language);
|
||||
}
|
||||
}
|
@ -22,6 +22,8 @@ public class BaseSettings {
|
||||
public static final BooleanSetting ENABLE_DEBUG_BUFFER_LOGGING = new BooleanSetting("revanced_enable_debug_buffer_logging", FALSE);
|
||||
public static final BooleanSetting SETTINGS_INITIALIZED = new BooleanSetting("revanced_settings_initialized", FALSE, false, false);
|
||||
|
||||
public static final EnumSetting<AppLanguage> REVANCED_LANGUAGE = new EnumSetting<>("revanced_language", AppLanguage.DEFAULT, true);
|
||||
|
||||
/**
|
||||
* These settings are used by YouTube and YouTube Music.
|
||||
*/
|
||||
|
@ -43,6 +43,8 @@ import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import app.revanced.extension.shared.settings.AppLanguage;
|
||||
import app.revanced.extension.shared.settings.BaseSettings;
|
||||
import app.revanced.extension.shared.settings.BooleanSetting;
|
||||
import kotlin.text.Regex;
|
||||
|
||||
@ -280,14 +282,13 @@ public class Utils {
|
||||
}
|
||||
|
||||
public static Resources getResources() {
|
||||
if (context != null) {
|
||||
return context.getResources();
|
||||
}
|
||||
Activity mActivity = activityRef.get();
|
||||
if (mActivity != null) {
|
||||
return mActivity.getResources();
|
||||
}
|
||||
Context mContext = getContext();
|
||||
if (mContext != null) {
|
||||
return mContext.getResources();
|
||||
}
|
||||
throw new IllegalStateException("Get resources failed");
|
||||
}
|
||||
|
||||
@ -301,7 +302,7 @@ public class Utils {
|
||||
* @param mContext Context to check locale.
|
||||
* @return Context with locale applied.
|
||||
*/
|
||||
public static Context getLocalizedContextAndSetResources(Context mContext) {
|
||||
public static Context getLocalizedContext(Context mContext) {
|
||||
Activity mActivity = activityRef.get();
|
||||
if (mActivity == null) {
|
||||
return mContext;
|
||||
@ -310,19 +311,15 @@ public class Utils {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Locale of MainActivity.
|
||||
Locale applicationLocale;
|
||||
AppLanguage language = BaseSettings.REVANCED_LANGUAGE.get();
|
||||
|
||||
// Locale of Application.
|
||||
Locale applicationLocale = language == AppLanguage.DEFAULT
|
||||
? mActivity.getResources().getConfiguration().locale
|
||||
: language.getLocale();
|
||||
|
||||
// Locale of Context.
|
||||
Locale contextLocale;
|
||||
|
||||
if (isSDKAbove(24)) {
|
||||
applicationLocale = mActivity.getResources().getConfiguration().getLocales().get(0);
|
||||
contextLocale = mContext.getResources().getConfiguration().getLocales().get(0);
|
||||
} else {
|
||||
applicationLocale = mActivity.getResources().getConfiguration().locale;
|
||||
contextLocale = mContext.getResources().getConfiguration().locale;
|
||||
}
|
||||
Locale contextLocale = mContext.getResources().getConfiguration().locale;
|
||||
|
||||
// If they are identical, no need to override them.
|
||||
if (applicationLocale == contextLocale) {
|
||||
@ -350,6 +347,14 @@ public class Utils {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// In some apps like TikTok, the Setting classes can load in weird orders due to cyclic class dependencies.
|
||||
// Calling the regular printDebug method here can cause a Settings context null pointer exception,
|
||||
// even though the context is already set before the call.
|
||||
|
@ -44,7 +44,7 @@ public class VideoQualitySettingsActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
super.attachBaseContext(Utils.getLocalizedContextAndSetResources(base));
|
||||
super.attachBaseContext(Utils.getLocalizedContext(base));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user