Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(android): follow the system language as the Mac does when they speak yours

wqugurue3dcb1131c4a
diff --git a/android/core/src/main/kotlin/cicada/Strings.kt b/android/core/src/main/kotlin/cicada/Strings.kt
index d2f067b..7c3539c 100644
--- a/android/core/src/main/kotlin/cicada/Strings.kt
+++ b/android/core/src/main/kotlin/cicada/Strings.kt
@@ -29,7 +29,8 @@ object Strings {
@JvmStatic fun L(key: String): String = table[key]?.takeIf { it.isNotEmpty() } ?: key
- /// L() with the Mac's `%@` placeholders, filled in order (or `%1$@` by position).
+ /// L() with the Mac's placeholders filled in order: `%@` (or `%1$@` by position), and the
+ /// numbers `%lld` / `%ld` / `%d`.
@JvmStatic fun LF(key: String, vararg args: Any): String {
val text = L(key)
val b = StringBuilder()
@@ -39,6 +40,8 @@ object Strings {
if (text[i] == '%' && i + 1 < text.length && text[i + 1] == '@') {
b.append(args.getOrNull(next++) ?: ""); i += 2; continue
}
+ val number = listOf("%lld", "%ld", "%d").firstOrNull { text.startsWith(it, i) }
+ if (number != null) { b.append(args.getOrNull(next++) ?: ""); i += number.length; continue }
if (text[i] == '%' && i + 3 < text.length && text[i + 1].isDigit() && text[i + 2] == '$' && text[i + 3] == '@') {
b.append(args.getOrNull(text[i + 1] - '1') ?: ""); i += 4; continue
}
diff --git a/android/core/src/main/kotlin/cicada/Support.kt b/android/core/src/main/kotlin/cicada/Support.kt
index 7b4092c..c1ed567 100644
--- a/android/core/src/main/kotlin/cicada/Support.kt
+++ b/android/core/src/main/kotlin/cicada/Support.kt
@@ -61,7 +61,22 @@ object Languages {
fun fromSystem(tag: String): String {
val parts = tag.split('-', '_')
if (parts[0] != "zh") return parts[0]
- return if (parts.any { it in setOf("Hant", "TW", "HK", "MO") }) "zh-TW" else "zh-CN"
+ // As the Mac's translationCode: the script decides; a region only when there's none.
+ if ("Hant" in parts) return "zh-TW"
+ if ("Hans" in parts) return "zh-CN"
+ return if (parts.any { it in setOf("TW", "HK", "MO") }) "zh-TW" else "zh-CN"
+ }
+
+ /// The Mac's myLanguage: Chinese when the system list has it (the app speaks Chinese),
+ /// otherwise the first system language.
+ fun myLanguage(prefs: List<String>): String = fromSystem(prefs.firstOrNull { it.startsWith("zh") } ?: prefs.firstOrNull() ?: "zh-Hans")
+
+ /// The Mac's followSystemTarget: my language, unless that is what they speak; then the next
+ /// system language, or between Chinese and English when the system has only one.
+ fun followSystemTarget(source: String?, prefs: List<String>): String {
+ val mine = myLanguage(prefs)
+ if (source == null || source != mine) return mine
+ return prefs.map(::fromSystem).firstOrNull { it != mine } ?: if (mine.startsWith("zh")) "en" else "zh-CN"
}
/// Normalizes what Ming says the language is ("zh", "zh-Hans", "cmn") to a source code.