Implements language changer

This commit is contained in:
khanhduytran0 2020-11-12 18:52:07 +07:00
parent 57325f4871
commit e8e7e88240
4 changed files with 69 additions and 12 deletions

View File

@ -0,0 +1,25 @@
package net.kdt.pojavlaunch;
import java.util.*;
public class DisplayableLocale {
public final Locale mLocale;
public final CharSequence mName;
public DisplayableLocale(Locale locale) {
this(locale, locale.getDisplayName(locale));
}
public DisplayableLocale(Locale locale, CharSequence name) {
mLocale = locale;
mName = name;
}
public Locale toLocale() {
return mLocale;
}
@Override
public String toString() {
return mName.toString();
}
}

View File

@ -19,11 +19,13 @@ import com.kdt.filerapi.*;
import com.kdt.mojangauth.*;
import java.io.*;
import java.util.*;
import java.util.zip.*;
import net.kdt.pojavlaunch.customcontrols.*;
import net.kdt.pojavlaunch.utils.*;
import org.apache.commons.compress.archivers.tar.*;
import org.apache.commons.compress.compressors.xz.*;
import org.apache.commons.io.*;
import net.kdt.pojavlaunch.prefs.*;
public class PojavLoginActivity extends AppCompatActivity
// MineActivity
@ -186,6 +188,10 @@ public class PojavLoginActivity extends AppCompatActivity
}
private void uiInit() {
if (!LauncherPreferences.PREF_LANGUAGE.equals("default")) {
setLocale(new Locale(LauncherPreferences.PREF_LANGUAGE));
}
setContentView(R.layout.launcher_login_v2);
loginLayout = findViewById(R.id.login_layout_linear);
@ -202,11 +208,26 @@ public class PojavLoginActivity extends AppCompatActivity
SpannableString defaultLangChar = new SpannableString(defaultLang);
defaultLangChar.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, defaultLang.length(),0);
ArrayAdapter<CharSequence> langAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
langAdapter.add(defaultLangChar);
for (Locale locale : Locale.getAvailableLocales()) {
langAdapter.add(locale.getDisplayLanguage());
ArrayAdapter<DisplayableLocale> langAdapter = new ArrayAdapter<DisplayableLocale>(this, android.R.layout.simple_spinner_item);
langAdapter.add(new DisplayableLocale(Locale.getDefault(), defaultLangChar));
langAdapter.add(new DisplayableLocale(Locale.ENGLISH));
// TODO better way to read language list
try {
ZipFile thisApk = new ZipFile(getApplicationInfo().publicSourceDir);
Enumeration<?> thisEntries = thisApk.entries();
while (thisEntries.hasMoreElements()) {
File currFile = new File("/" + ((ZipEntry) thisEntries.nextElement()).getName());
if (currFile.getAbsolutePath().startsWith("/res/values-") && currFile.getName().startsWith("values-")) {
// TODO use regex
Locale thisLocale = new Locale(currFile.getName().replace("values-", "").replace("-r", "-"));
langAdapter.add(new DisplayableLocale(thisLocale));
}
}
} catch (IOException e) {
Tools.showError(this, e);
}
langAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinnerChgLang.setAdapter(langAdapter);
@ -217,14 +238,12 @@ public class PojavLoginActivity extends AppCompatActivity
Locale locale;
if (position == 0) {
locale = Locale.getDefault();
} else if (position == 1) {
locale = Locale.ENGLISH;
} else {
locale = Locale.getAvailableLocales()[position - 1];
locale = Locale.getAvailableLocales()[position - 2];
}
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
// TODO replace deprecated
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
setLocale(locale);
}
@Override
@ -264,6 +283,16 @@ public class PojavLoginActivity extends AppCompatActivity
// Clear current profile
PojavProfile.setCurrentProfile(this, null);
}
private void setLocale(Locale locale) {
LauncherPreferences.PREF_LANGUAGE = locale.getLanguage();
LauncherPreferences.DEFAULT_PREF.edit().putString("language", LauncherPreferences.PREF_LANGUAGE).commit();
Locale.setDefault(locale);
Configuration config = new Configuration();
config.setLocale(locale);
// TODO replace deprecated
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}
private boolean isJavaRuntimeInstalled() {
return firstLaunchPrefs.getBoolean(PREF_IS_INSTALLED_JAVARUNTIME, false);

View File

@ -1,8 +1,8 @@
package net.kdt.pojavlaunch.prefs;
import android.content.*;
import java.util.*;
import net.kdt.pojavlaunch.*;
import android.os.*;
public class LauncherPreferences
{
@ -17,6 +17,7 @@ public class LauncherPreferences
public static String PREF_DEFAULTCTRL_PATH = Tools.CTRLDEF_FILE;
public static String PREF_CUSTOM_JAVA_ARGS;
public static String PREF_CUSTOM_OPENGL_LIBNAME = "libgl04es.so";
public static String PREF_LANGUAGE = "default";
public static void loadPreferences() {
PREF_BUTTONSIZE = DEFAULT_PREF.getFloat("controlSize", 1f);
@ -27,6 +28,8 @@ public class LauncherPreferences
PREF_VERTYPE_OLDBETA = DEFAULT_PREF.getBoolean("vertype_oldbeta", false);
PREF_LONGPRESS_TRIGGER = DEFAULT_PREF.getInt("timeLongPressTrigger", 500);
PREF_DEFAULTCTRL_PATH = DEFAULT_PREF.getString("defaultCtrl", Tools.CTRLDEF_FILE);
PREF_LANGUAGE = DEFAULT_PREF.getString("language", "default");
// Get double of max Android heap to set default heap size
int androidHeap = (int) (Runtime.getRuntime().maxMemory() / 1024l / 512l);
int doubleAndroidHeap = androidHeap * 2;

View File

@ -100,7 +100,7 @@
<string name="mcl_setting_title_longpresstrigger">How long will trigger after long press</string>
<string name="mcl_setting_subtitle_longpresstrigger">Change trigger time for long press in destroy block and drop item.</string>
<string name="mcl_setting_title_controlsize">Set control buttons size</string>
<string name="mcl_setting_title_javaargs">JRE Launch arguments</string>
<string name="mcl_setting_title_javaargs">JVM Launch arguments</string>
<string name="mcl_setting_subtitle_javaargs">Be careful, this may make game crash if modified without knowledge.</string>
<string name="mcl_setting_category_general">General settings</string>
<string name="mcl_setting_category_veroption">Version type will be in version list</string>