Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Android: pick the app's language on Android 13 and newer

Bee9d42c4023ee8
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index fdfe836..dded8f3 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -53,6 +53,12 @@ android {
}
}
+ // Lists the languages in res/ for Android 13's per-app language (App languages in
+ // the system's settings, and Settings › Appearance › Language in the app).
+ androidResources {
+ generateLocaleConfig = true
+ }
+
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/AppLanguage.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/AppLanguage.kt
new file mode 100644
index 0000000..d7c9952
--- /dev/null
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/AppLanguage.kt
@@ -0,0 +1,53 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.ui
+
+import android.app.LocaleManager
+import android.content.Context
+import android.os.Build
+import android.os.LocaleList
+import androidx.annotation.RequiresApi
+
+/**
+ * The app's own language, kept by Android (13 and newer) as the per-app language: it shows
+ * in the system's App languages screen too, and applies everywhere the app shows text,
+ * notifications and background services included. Older Android follows the phone.
+ */
+object AppLanguage {
+ /** The languages the app speaks, as BCP 47 tags, each named in its own language. */
+ val languages: List<Pair<String, String>> = listOf(
+ "en" to "English",
+ "zh-CN" to "简体中文",
+ )
+
+ /** Whether the phone lets the app pick its own language. */
+ val supported: Boolean
+ get() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
+
+ /** The picked language's tag, or "" to follow the phone. */
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
+ fun current(context: Context): String {
+ val locales = context.getSystemService(LocaleManager::class.java)?.applicationLocales ?: return ""
+ return if (locales.isEmpty) "" else normalize(locales[0].toLanguageTag())
+ }
+
+ /** Switches to [tag] ("" for the phone's). Android restarts the open screens in it. */
+ @RequiresApi(Build.VERSION_CODES.TIRAMISU)
+ fun set(context: Context, tag: String) {
+ val manager = context.getSystemService(LocaleManager::class.java) ?: return
+ manager.applicationLocales = if (tag.isEmpty()) LocaleList.getEmptyLocaleList() else LocaleList.forLanguageTags(tag)
+ }
+
+ /**
+ * One of [languages]' tags for [tag], or "" when the app doesn't speak it. Android may
+ * hand back a fuller tag ("zh-Hans-CN") than the one set.
+ */
+ internal fun normalize(tag: String): String {
+ languages.firstOrNull { it.first.equals(tag, ignoreCase = true) }?.let { return it.first }
+ val lower = tag.lowercase()
+ return when {
+ lower.startsWith("zh") && ("hans" in lower || "-cn" in lower || "-sg" in lower) -> "zh-CN"
+ lower == "en" || lower.startsWith("en-") -> "en"
+ else -> ""
+ }
+ }
+}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt
index aa5c822..4a7fea4 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/settings/SettingsScreen.kt
@@ -52,6 +52,9 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
+import app.nectarlink.android.ui.AppLanguage
+import androidx.compose.material3.RadioButton
+import androidx.compose.foundation.selection.selectable
import androidx.compose.ui.res.pluralStringResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
@@ -127,6 +130,7 @@ fun SettingsScreen(
) {
var confirmUnpair by remember { mutableStateOf<Device?>(null) }
var confirmClearEverything by remember { mutableStateOf(false) }
+ var pickLanguage by remember { mutableStateOf(false) }
var activeCategory by androidx.compose.runtime.saveable.rememberSaveable { mutableStateOf("all") }
var pulsingSectionKey by remember { mutableStateOf<String?>(null) }
val isGlass = LocalGlassTheme.current
@@ -251,6 +255,23 @@ fun SettingsScreen(
),
appearance.mode,
) { value -> onAppearance { it.copy(mode = value) } }
+ if (AppLanguage.supported) {
+ // System and English, plus the language picked from the list (or More).
+ val context = LocalContext.current
+ val language = remember { AppLanguage.current(context) }
+ val other = AppLanguage.languages.firstOrNull { it.first == language && it.first != "en" }
+ Label(stringResource(R.string.settings_language))
+ Choice(
+ listOf(
+ "" to stringResource(R.string.mode_system),
+ "en" to "English",
+ (other ?: (MORE_LANGUAGES to stringResource(R.string.settings_language_more))),
+ ),
+ language,
+ ) { value ->
+ if (value == "" || value == "en") AppLanguage.set(context, value) else pickLanguage = true
+ }
+ }
if (appearance.theme == "hive") {
SettingRowDivider()
val combTitle = stringResource(R.string.settings_comb_motion)
@@ -943,6 +964,39 @@ fun SettingsScreen(
)
}
+ if (pickLanguage && AppLanguage.supported) {
+ val context = LocalContext.current
+ val language = remember { AppLanguage.current(context) }
+ AlertDialog(
+ onDismissRequest = { pickLanguage = false },
+ containerColor = glassModalContainerColor(),
+ title = { Text(stringResource(R.string.settings_language)) },
+ text = {
+ Column {
+ AppLanguage.languages.forEach { (tag, name) ->
+ Row(
+ verticalAlignment = Alignment.CenterVertically,
+ modifier = Modifier
+ .fillMaxWidth()
+ .selectable(selected = tag == language, role = Role.RadioButton) {
+ pickLanguage = false
+ AppLanguage.set(context, tag)
+ }
+ .padding(vertical = 12.dp),
+ ) {
+ RadioButton(selected = tag == language, onClick = null)
+ Spacer(Modifier.width(12.dp))
+ Text(name, style = MaterialTheme.typography.bodyLarge)
+ }
+ }
+ }
+ },
+ confirmButton = {
+ TextButton(onClick = { pickLanguage = false }) { Text(stringResource(R.string.action_cancel)) }
+ },
+ )
+ }
+
if (confirmClearEverything) {
AlertDialog(
onDismissRequest = { confirmClearEverything = false },
@@ -1483,6 +1537,9 @@ private fun Label(text: String) {
Text(text, style = MaterialTheme.typography.titleSmall)
}
+/** The third language choice when it isn't a picked language: opens the list. */
+private const val MORE_LANGUAGES = "more"
+
@Composable
private fun Choice(options: List<Pair<String, String>>, selected: String, onSelect: (String) -> Unit) {
SingleChoiceSegmentedButtonRow(Modifier.fillMaxWidth()) {
diff --git a/android/app/src/main/res/resources.properties b/android/app/src/main/res/resources.properties
new file mode 100644
index 0000000..467b3ef
--- /dev/null
+++ b/android/app/src/main/res/resources.properties
@@ -0,0 +1 @@
+unqualifiedResLocale=en-US
diff --git a/android/app/src/main/res/values-zh-rCN/strings.xml b/android/app/src/main/res/values-zh-rCN/strings.xml
index f136e4c..fc3eb06 100644
--- a/android/app/src/main/res/values-zh-rCN/strings.xml
+++ b/android/app/src/main/res/values-zh-rCN/strings.xml
@@ -46,6 +46,8 @@
<string name="settings_appearance">外观</string>
<string name="settings_theme">主题</string>
<string name="settings_mode">模式</string>
+ <string name="settings_language">语言</string>
+ <string name="settings_language_more">更多</string>
<string name="settings_dynamic_color">壁纸取色</string>
<string name="settings_dynamic_color_hint">使用壁纸的颜色。</string>
<string name="settings_devices">电脑</string>
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index c9f49ac..3b7f48c 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -52,6 +52,8 @@
<string name="settings_appearance">Appearance</string>
<string name="settings_theme">Theme</string>
<string name="settings_mode">Mode</string>
+ <string name="settings_language">Language</string>
+ <string name="settings_language_more">More</string>
<string name="settings_dynamic_color">Colors from wallpaper</string>
<string name="settings_dynamic_color_hint">Use your wallpaper\'s colors.</string>
<string name="settings_devices">PCs</string>