[app] Add language selector (#1087)

This commit is contained in:
tehcneko 2021-09-09 15:00:08 +08:00 committed by GitHub
parent a48ab2aff3
commit 54652f7d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 139 additions and 14 deletions

View File

@ -26,6 +26,7 @@ import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Process; import android.os.Process;
import android.system.Os; import android.system.Os;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -57,6 +58,7 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor; import okhttp3.logging.HttpLoggingInterceptor;
import rikka.material.app.DayNightDelegate; import rikka.material.app.DayNightDelegate;
import rikka.material.app.LocaleDelegate;
public class App extends Application { public class App extends Application {
@ -130,6 +132,7 @@ public class App extends Application {
} }
DayNightDelegate.setApplicationContext(this); DayNightDelegate.setApplicationContext(this);
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme()); DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme());
LocaleDelegate.setDefaultLocale(getLocale());
loadRemoteVersion(); loadRemoteVersion();
RepoLoader.getInstance().loadRemoteData(); RepoLoader.getInstance().loadRemoteData();
@ -211,4 +214,12 @@ public class App extends Application {
} }
return buildTime.atOffset(ZoneOffset.UTC).plusDays(30).toInstant().isBefore(now); return buildTime.atOffset(ZoneOffset.UTC).plusDays(30).toInstant().isBefore(now);
} }
public static Locale getLocale() {
String tag = getPreferences().getString("language", null);
if (TextUtils.isEmpty(tag) || "SYSTEM".equals(tag)) {
return Locale.getDefault();
}
return Locale.forLanguageTag(tag);
}
} }

View File

@ -23,6 +23,7 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -33,6 +34,7 @@ import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.core.text.HtmlCompat;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.SwitchPreference; import androidx.preference.SwitchPreference;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -49,10 +51,13 @@ import org.lsposed.manager.util.BackupUtils;
import org.lsposed.manager.util.theme.ThemeUtil; import org.lsposed.manager.util.theme.ThemeUtil;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Locale; import java.util.Locale;
import rikka.core.util.ResourceUtils; import rikka.core.util.ResourceUtils;
import rikka.material.app.DayNightDelegate; import rikka.material.app.DayNightDelegate;
import rikka.material.app.LocaleDelegate;
import rikka.preference.SimpleMenuPreference;
import rikka.recyclerview.RecyclerViewKt; import rikka.recyclerview.RecyclerViewKt;
import rikka.widget.borderview.BorderRecyclerView; import rikka.widget.borderview.BorderRecyclerView;
@ -230,6 +235,40 @@ public class SettingsFragment extends BaseFragment {
return true; return true;
}); });
} }
SimpleMenuPreference language = findPreference("language");
if (language != null) {
language.setOnPreferenceChangeListener((preference, newValue) -> {
var locale = "SYSTEM".equals(newValue) ? LocaleDelegate.getSystemLocale() : Locale.forLanguageTag((String) newValue);
LocaleDelegate.setDefaultLocale(locale);
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.restart();
}
return true;
});
var tag = language.getValue();
var userLocale = App.getLocale();
var entries = new ArrayList<CharSequence>();
for (int i = 0; i < language.getEntries().length; i++) {
if (i > 0) {
var locale = Locale.forLanguageTag(language.getEntries()[i].toString());
entries.add(HtmlCompat.fromHtml(String.format("%s - %s",
!TextUtils.isEmpty(locale.getScript()) ? locale.getDisplayScript(locale) : locale.getDisplayName(locale),
!TextUtils.isEmpty(locale.getScript()) ? locale.getDisplayScript(userLocale) : locale.getDisplayName(userLocale)
), HtmlCompat.FROM_HTML_MODE_LEGACY));
} else {
entries.add(language.getEntries()[i]);
}
}
language.setEntries(entries.toArray(new CharSequence[0]));
if (TextUtils.isEmpty(tag) || "SYSTEM".equals(tag)) {
language.setSummary(getString(rikka.material.R.string.follow_system));
} else {
var locale = Locale.forLanguageTag(tag);
language.setSummary(!TextUtils.isEmpty(locale.getScript()) ? locale.getDisplayScript(userLocale) : locale.getDisplayName(userLocale));
}
}
} }
@Override @Override

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- <!--
~ This file is part of LSPosed. ~ This file is part of LSPosed.
~ ~
@ -15,17 +14,16 @@
~ You should have received a copy of the GNU General Public License ~ You should have received a copy of the GNU General Public License
~ along with LSPosed. If not, see <https://www.gnu.org/licenses/>. ~ along with LSPosed. If not, see <https://www.gnu.org/licenses/>.
~ ~
~ Copyright (C) 2020 EdXposed Contributors
~ Copyright (C) 2021 LSPosed Contributors ~ Copyright (C) 2021 LSPosed Contributors
--> -->
<resources>
<!-- MainActivity --> <vector xmlns:android="http://schemas.android.com/apk/res/android"
<!-- LogsActivity --> android:width="24dp"
<!-- Notification --> android:height="24dp"
<!-- ModulesActivity --> android:viewportWidth="24"
<!-- AppListActivity --> android:viewportHeight="24"
<!-- ModulesActivity and AppListActivity --> android:tint="?attr/colorControlNormal">
<!-- SettingsActivity --> <path
<!-- Module Repo --> android:fillColor="@android:color/white"
<!-- CrashReportActivity --> android:pathData="M12.87,15.07l-2.54,-2.51 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53L17,6L17,4h-7L10,2L8,2v2L1,4v1.99h11.17C11.5,7.92 10.44,9.75 9,11.35 8.07,10.32 7.3,9.19 6.69,8h-2c0.73,1.63 1.73,3.17 2.98,4.56l-5.09,5.02L4,19l5,-5 3.11,3.11 0.76,-2.04zM18.5,10h-2L12,22h2l1.12,-3h4.75L21,22h2l-4.5,-12zM15.88,17l1.62,-4.33L19.12,17h-3.24z"/>
</resources> </vector>

View File

@ -6,6 +6,6 @@
android:tint="?attr/colorControlNormal" android:tint="?attr/colorControlNormal"
android:autoMirrored="true"> android:autoMirrored="true">
<path <path
android:fillColor="@android:color/white" android:fillColor="@color/material_blue_500"
android:pathData="M19,3h-4.18C14.4,1.84 13.3,1 12,1s-2.4,0.84 -2.82,2L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM12,3c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM13,17L8,17c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h5c0.55,0 1,0.45 1,1s-0.45,1 -1,1zM16,13L8,13c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h8c0.55,0 1,0.45 1,1s-0.45,1 -1,1zM16,9L8,9c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h8c0.55,0 1,0.45 1,1s-0.45,1 -1,1z"/> android:pathData="M19,3h-4.18C14.4,1.84 13.3,1 12,1s-2.4,0.84 -2.82,2L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM12,3c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM13,17L8,17c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h5c0.55,0 1,0.45 1,1s-0.45,1 -1,1zM16,13L8,13c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h8c0.55,0 1,0.45 1,1s-0.45,1 -1,1zM16,9L8,9c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1h8c0.55,0 1,0.45 1,1s-0.45,1 -1,1z"/>
</vector> </vector>

View File

@ -30,6 +30,7 @@
<string name="Logs">日志</string> <string name="Logs">日志</string>
<string name="Settings">设置</string> <string name="Settings">设置</string>
<string name="About">关于</string> <string name="About">关于</string>
<string name="report_issue">报告问题</string>
<string name="module_repo">仓库</string> <string name="module_repo">仓库</string>
<string name="module_repo_summary">模块仓库Beta</string> <string name="module_repo_summary">模块仓库Beta</string>
<string name="about_view_source_code"><![CDATA[在 %1$s 查看源码<br/>加入我们的 %2$s 频道]]></string> <string name="about_view_source_code"><![CDATA[在 %1$s 查看源码<br/>加入我们的 %2$s 频道]]></string>
@ -165,6 +166,7 @@
<string name="settings_show_hidden_icon_apps_enabled">强制应用在启动器中显示图标</string> <string name="settings_show_hidden_icon_apps_enabled">强制应用在启动器中显示图标</string>
<string name="settings_show_hidden_icon_apps_enabled_summary">在 Android 10 之后,应用不被允许隐藏启动器图标。关闭本选项可禁用此系统功能。</string> <string name="settings_show_hidden_icon_apps_enabled_summary">在 Android 10 之后,应用不被允许隐藏启动器图标。关闭本选项可禁用此系统功能。</string>
<string name="settings_group_system">系统</string> <string name="settings_group_system">系统</string>
<string name="settings_language">语言</string>
<!-- Module Repo --> <!-- Module Repo -->
<string name="module_readme">自述文件</string> <string name="module_readme">自述文件</string>

View File

@ -31,4 +31,66 @@
<item>MODE_NIGHT_YES</item> <item>MODE_NIGHT_YES</item>
<item>MODE_NIGHT_FOLLOW_SYSTEM</item> <item>MODE_NIGHT_FOLLOW_SYSTEM</item>
</string-array> </string-array>
<string-array name="language" translatable="false">
<item>@string/follow_system</item>
<item>af-ZA</item>
<item>ar-SA</item>
<item>ca-ES</item>
<item>cs-CZ</item>
<item>da-DK</item>
<item>el-GR</item>
<item>es-ES</item>
<item>fi-FI</item>
<item>fr-FR</item>
<item>hu-HU</item>
<item>it-IT</item>
<item>iw-IL</item>
<item>ja-JP</item>
<item>ko-KR</item>
<item>nl-NL</item>
<item>no-NO</item>
<item>pl-PL</item>
<item>pt-BR</item>
<item>pt-PT</item>
<item>ro-RO</item>
<item>ru-RU</item>
<item>tr-TR</item>
<item>uk-UA</item>
<item>vi-VN</item>
<item>zh-CN</item>
<item>zh-HK</item>
<item>zh-TW</item>
</string-array>
<string-array name="language_value" translatable="false">
<item>SYSTEM</item>
<item>af-ZA</item>
<item>ar-SA</item>
<item>ca-ES</item>
<item>cs-CZ</item>
<item>da-DK</item>
<item>el-GR</item>
<item>es-ES</item>
<item>fi-FI</item>
<item>fr-FR</item>
<item>hu-HU</item>
<item>it-IT</item>
<item>iw-IL</item>
<item>ja-JP</item>
<item>ko-KR</item>
<item>nl-NL</item>
<item>no-NO</item>
<item>pl-PL</item>
<item>pt-BR</item>
<item>pt-PT</item>
<item>ro-RO</item>
<item>ru-RU</item>
<item>tr-TR</item>
<item>uk-UA</item>
<item>vi-VN</item>
<item>zh-CN</item>
<item>zh-HK</item>
<item>zh-TW</item>
</string-array>
</resources> </resources>

View File

@ -165,6 +165,7 @@
<string name="settings_show_hidden_icon_apps_enabled">Force apps to show launcher icons</string> <string name="settings_show_hidden_icon_apps_enabled">Force apps to show launcher icons</string>
<string name="settings_show_hidden_icon_apps_enabled_summary">After Android 10, apps are not allowed to hide their launcher icons. Turn off the toggle to disable this system feature.</string> <string name="settings_show_hidden_icon_apps_enabled_summary">After Android 10, apps are not allowed to hide their launcher icons. Turn off the toggle to disable this system feature.</string>
<string name="settings_group_system">System</string> <string name="settings_group_system">System</string>
<string name="settings_language">Language</string>
<!-- Module Repo --> <!-- Module Repo -->
<string name="module_readme">Readme</string> <string name="module_readme">Readme</string>

View File

@ -30,6 +30,18 @@
android:title="@string/dns_over_http" /> android:title="@string/dns_over_http" />
</com.takisoft.preferencex.PreferenceCategory> </com.takisoft.preferencex.PreferenceCategory>
<com.takisoft.preferencex.PreferenceCategory android:title="@string/settings_language">
<rikka.preference.SimpleMenuPreference
android:defaultValue="SYSTEM"
android:entries="@array/language"
android:entryValues="@array/language_value"
android:icon="@drawable/ic_baseline_translate_24"
android:summary="%s"
android:key="language"
android:title="@string/settings_language" />
</com.takisoft.preferencex.PreferenceCategory>
<com.takisoft.preferencex.PreferenceCategory android:title="@string/settings_group_theme"> <com.takisoft.preferencex.PreferenceCategory android:title="@string/settings_group_theme">
<SwitchPreference <SwitchPreference
android:defaultValue="true" android:defaultValue="true"