mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-13 14:51:51 -04:00
Fix languages going back to english.
This commit is contained in:
parent
30370377e6
commit
7349dd54eb
@ -11,14 +11,11 @@ public abstract class BaseActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
LocaleUtils.setLocale(this);
|
||||
Tools.setFullscreen(this);
|
||||
Tools.updateWindowSize(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void attachBaseContext(Context base) {
|
||||
super.attachBaseContext(LocaleUtils.setLocale(base));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void startActivity(Intent i) {
|
||||
|
@ -8,7 +8,7 @@ public class DisplayableLocale {
|
||||
private static Locale processStringLocale(String locale) {
|
||||
if (locale.contains("-")) {
|
||||
String[] split = locale.split("-");
|
||||
return new Locale(split[0], split[1]);
|
||||
return new Locale(split[0], split[1].toUpperCase());
|
||||
} else {
|
||||
return new Locale(locale);
|
||||
}
|
||||
|
@ -180,14 +180,7 @@ public class PojavLoginActivity extends BaseActivity {
|
||||
setContentView(R.layout.activity_pojav_login);
|
||||
|
||||
Spinner spinnerChgLang = findViewById(R.id.login_spinner_language);
|
||||
|
||||
String defaultLang = LocaleUtils.DEFAULT_LOCALE.getDisplayName();
|
||||
SpannableString defaultLangChar = new SpannableString(defaultLang);
|
||||
defaultLangChar.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, defaultLang.length(), 0);
|
||||
|
||||
final ArrayAdapter<DisplayableLocale> langAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
|
||||
langAdapter.add(new DisplayableLocale(LocaleUtils.DEFAULT_LOCALE, defaultLangChar));
|
||||
langAdapter.add(new DisplayableLocale(Locale.ENGLISH));
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("language_list.txt")));
|
||||
@ -205,17 +198,15 @@ public class PojavLoginActivity extends BaseActivity {
|
||||
}
|
||||
|
||||
langAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
|
||||
|
||||
int selectedLang = 0;
|
||||
for (int i = 0; i < langAdapter.getCount(); i++) {
|
||||
if (Locale.getDefault().toString().equalsIgnoreCase(langAdapter.getItem(i).mLocale.toString())) {
|
||||
selectedLang = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int selectedLang = getSelectorPosition(langAdapter, LocaleUtils.getLocale());
|
||||
if (selectedLang == -1) selectedLang = getSelectorPosition(langAdapter, null);
|
||||
|
||||
spinnerChgLang.setAdapter(langAdapter);
|
||||
spinnerChgLang.setSelection(selectedLang);
|
||||
if (selectedLang != -1){
|
||||
spinnerChgLang.setSelection(selectedLang);
|
||||
}
|
||||
|
||||
spinnerChgLang.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
|
||||
private boolean isInitCalled;
|
||||
@Override
|
||||
@ -225,15 +216,7 @@ public class PojavLoginActivity extends BaseActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
Locale locale;
|
||||
if (position == 0) {
|
||||
locale = LocaleUtils.DEFAULT_LOCALE;
|
||||
} else if (position == 1) {
|
||||
locale = Locale.ENGLISH;
|
||||
} else {
|
||||
locale = langAdapter.getItem(position).mLocale;
|
||||
}
|
||||
|
||||
Locale locale = langAdapter.getItem(position).mLocale;
|
||||
LauncherPreferences.PREF_LANGUAGE = locale.toString();
|
||||
LauncherPreferences.DEFAULT_PREF.edit().putString("language", LauncherPreferences.PREF_LANGUAGE).apply();
|
||||
|
||||
@ -256,6 +239,18 @@ public class PojavLoginActivity extends BaseActivity {
|
||||
});
|
||||
isSkipInit = true;
|
||||
}
|
||||
|
||||
/** @return The index in the array adapter for a given language, or english. -1 if not found */
|
||||
public int getSelectorPosition(@NonNull ArrayAdapter<DisplayableLocale> langAdapter, @Nullable Locale locale){
|
||||
String localeString = locale == null ? Locale.ENGLISH.toString() : locale.toString();
|
||||
for (int i = 0; i < langAdapter.getCount(); i++) {
|
||||
if (localeString.equalsIgnoreCase(langAdapter.getItem(i).mLocale.toString())) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
|
@ -1,37 +1,60 @@
|
||||
package net.kdt.pojavlaunch.utils;
|
||||
|
||||
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.PREF_LANGUAGE;
|
||||
|
||||
import android.content.*;
|
||||
import android.content.res.*;
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.preference.*;
|
||||
import java.util.*;
|
||||
import net.kdt.pojavlaunch.prefs.*;
|
||||
|
||||
public class LocaleUtils {
|
||||
public static final Locale DEFAULT_LOCALE;
|
||||
|
||||
static {
|
||||
DEFAULT_LOCALE = Locale.getDefault();
|
||||
|
||||
private static Locale CURRENT_LOCALE;
|
||||
|
||||
public static Locale getLocale(){
|
||||
return Locale.getDefault();
|
||||
}
|
||||
|
||||
public static Context setLocale(Context context) {
|
||||
if (LauncherPreferences.DEFAULT_PREF == null) {
|
||||
LauncherPreferences.DEFAULT_PREF = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
LauncherPreferences.loadPreferences(context);
|
||||
LauncherPreferences.loadPreferences(context);
|
||||
}
|
||||
|
||||
Locale locale;
|
||||
|
||||
|
||||
if (LauncherPreferences.PREF_LANGUAGE.equals("default")) {
|
||||
locale = DEFAULT_LOCALE;
|
||||
CURRENT_LOCALE = getLocale();
|
||||
} else {
|
||||
locale = new Locale(LauncherPreferences.PREF_LANGUAGE);
|
||||
if(CURRENT_LOCALE == null || !PREF_LANGUAGE.equalsIgnoreCase(CURRENT_LOCALE.toString())){
|
||||
String[] localeString;
|
||||
if(PREF_LANGUAGE.contains("_")){
|
||||
localeString = PREF_LANGUAGE.split("_");
|
||||
}else{
|
||||
localeString = new String[]{PREF_LANGUAGE, ""};
|
||||
}
|
||||
CURRENT_LOCALE = new Locale(localeString[0], localeString[1]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Locale.setDefault(locale);
|
||||
Locale.setDefault(CURRENT_LOCALE);
|
||||
|
||||
Resources res = context.getResources();
|
||||
Configuration config = new Configuration(res.getConfiguration());
|
||||
config.setLocale(locale);
|
||||
context = context.createConfigurationContext(config);
|
||||
Configuration config = res.getConfiguration();
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 24) {
|
||||
config.setLocale(CURRENT_LOCALE);
|
||||
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
|
||||
} else {
|
||||
config.locale = CURRENT_LOCALE;
|
||||
context.getApplicationContext().createConfigurationContext(config);
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user