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): in English, a meeting with one line says 1 line, not 1 lines

wqugurufefcc60377dd
diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts
index 3ab13e0..a5c0ad2 100644
--- a/android/app/build.gradle.kts
+++ b/android/app/build.gradle.kts
@@ -60,12 +60,12 @@ kotlin { jvmToolchain(21) }
dependencies { implementation(project(":core")) }
-// The UI text: the Mac's Localizable.strings and the Android-only Android.strings, both keyed by
+// The UI text: the Mac's Localizable.strings (and .stringsdict, its plurals) and the Android-only Android.strings, both keyed by
// the Chinese, into assets/Resources/<lang>.lproj/.
val copyStrings by tasks.registering(Sync::class) {
into(layout.buildDirectory.dir("generated/strings/Resources"))
for (lang in listOf("zh-Hans", "en", "ja", "ko")) {
- from(rootProject.file("../Resources/$lang.lproj")) { include("Localizable.strings"); into("$lang.lproj") }
+ from(rootProject.file("../Resources/$lang.lproj")) { include("Localizable.strings", "Localizable.stringsdict"); into("$lang.lproj") }
from(file("src/main/strings/$lang.lproj")) { include("Android.strings"); into("$lang.lproj") }
}
}
diff --git a/android/app/src/main/kotlin/com/wquguru/cicada/ImportActivity.kt b/android/app/src/main/kotlin/com/wquguru/cicada/ImportActivity.kt
index 128e18a..16c6ea7 100644
--- a/android/app/src/main/kotlin/com/wquguru/cicada/ImportActivity.kt
+++ b/android/app/src/main/kotlin/com/wquguru/cicada/ImportActivity.kt
@@ -10,6 +10,7 @@ import android.widget.FrameLayout
import android.widget.LinearLayout
import cicada.Languages
import cicada.Strings.L
+import cicada.Strings.LF
/// 导入录音或视频 (the Mac's FileImport): pick a file, or share one to 知了 from another app, and it
/// becomes a meeting record: transcribed, translated and summarised like one that was heard. It
@@ -80,7 +81,7 @@ class ImportActivity : Activity() {
done != null -> {
column.addView(ui.card(fileRow(done.sourceFile ?: "", done.durationSeconds), gap = 16).apply {
addView(ui.text(L("转写完成"), 17f, Palette.ink, bold = true), ui.lp(top = 18))
- addView(ui.helper("${done.lines.size} ${L("句")} · ${L("纪要在会议记录里")}"), ui.lp(top = 4))
+ addView(ui.helper("${LF("%lld 句", done.lines.size)} · ${L("纪要在会议记录里")}"), ui.lp(top = 4))
addView(ui.button(L("查看会议记录"), "primary") {
startActivity(Intent(this@ImportActivity, RecordActivity::class.java).putExtra("id", done.id)); finish()
}, ui.lp(top = 18))
diff --git a/android/app/src/main/kotlin/com/wquguru/cicada/MainActivity.kt b/android/app/src/main/kotlin/com/wquguru/cicada/MainActivity.kt
index 1656e42..dae1de3 100644
--- a/android/app/src/main/kotlin/com/wquguru/cicada/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/wquguru/cicada/MainActivity.kt
@@ -265,7 +265,7 @@ class MainActivity : Activity() {
// Under a day's heading: the time, how long, how many lines, and where it came from.
val from = s.sourceFile?.let { L("导入") }
val meta = if (live) "${time(s.startedAt)} · ${Languages.clock(s.durationSeconds)}"
- else listOfNotNull("${time(s.startedAt)}", Languages.clock(s.durationSeconds), "${s.lines.size} ${L("句")}", from).joinToString(" · ")
+ else listOfNotNull("${time(s.startedAt)}", Languages.clock(s.durationSeconds), LF("%lld 句", s.lines.size), from).joinToString(" · ")
val todos = s.notes?.todos?.size ?: 0
val trailing = if (todos > 0) ui.badge(LF("%@ 个待办", todos), Palette.body, Palette.ground) else null
return ui.listRow(title, meta, trailing = trailing, titleView = titleView) {
diff --git a/android/app/src/main/kotlin/com/wquguru/cicada/RecordsActivity.kt b/android/app/src/main/kotlin/com/wquguru/cicada/RecordsActivity.kt
index aac9a97..bcc8863 100644
--- a/android/app/src/main/kotlin/com/wquguru/cicada/RecordsActivity.kt
+++ b/android/app/src/main/kotlin/com/wquguru/cicada/RecordsActivity.kt
@@ -245,7 +245,7 @@ class RecordActivity : Activity() {
top.addView(ui.text(meta, 13f, Palette.secondary), ui.lp(top = 6))
recordingNote(s, live, file, playable)?.let { top.addView(it, ui.lp(top = 6)) }
speakerChips(s)?.let { top.addView(it, ui.lp(top = 14)) }
- val tabLabels = listOf(L("纪要"), "${L("文字记录")} · ${s.lines.size} ${L("句")}")
+ val tabLabels = listOf(L("纪要"), "${L("文字记录")} · ${LF("%lld 句", s.lines.size)}")
val tabs = ui.tabs(tabLabels, tab) { tab = it; render() }
top.addView(tabs, ui.lp(top = 18))
headTabs = tabs
diff --git a/android/app/src/main/strings/en.lproj/Android.strings b/android/app/src/main/strings/en.lproj/Android.strings
index 7601eb7..ce7f400 100644
--- a/android/app/src/main/strings/en.lproj/Android.strings
+++ b/android/app/src/main/strings/en.lproj/Android.strings
@@ -21,7 +21,6 @@
"全部" = "See all";
"还没有会议记录" = "No meetings yet";
"M月d日" = "MMM d";
-"句" = "lines";
"%@ 个待办" = "To-dos: %@";
"%@ 分钟前更新" = "Updated %@ min ago";
"20 秒没听到声音。通话和会议 app 的声音系统不让录" = "No sound for 20s. Call & meeting apps block audio capture.";
diff --git a/android/app/src/main/strings/ja.lproj/Android.strings b/android/app/src/main/strings/ja.lproj/Android.strings
index dc717e3..35124db 100644
--- a/android/app/src/main/strings/ja.lproj/Android.strings
+++ b/android/app/src/main/strings/ja.lproj/Android.strings
@@ -21,7 +21,6 @@
"全部" = "すべて";
"还没有会议记录" = "会議の記録はまだありません";
"M月d日" = "M月d日";
-"句" = "文";
"%@ 个待办" = "ToDo %@件";
"%@ 分钟前更新" = "%@分前に更新";
"20 秒没听到声音。通话和会议 app 的声音系统不让录" = "20秒間音声がありません。通話・会議アプリは録音できません";
diff --git a/android/app/src/main/strings/ko.lproj/Android.strings b/android/app/src/main/strings/ko.lproj/Android.strings
index 214a6e9..3133328 100644
--- a/android/app/src/main/strings/ko.lproj/Android.strings
+++ b/android/app/src/main/strings/ko.lproj/Android.strings
@@ -21,7 +21,6 @@
"全部" = "전체";
"还没有会议记录" = "아직 회의 기록이 없어요";
"M月d日" = "M월 d일";
-"句" = "문장";
"%@ 个待办" = "할 일 %@개";
"%@ 分钟前更新" = "%@분 전 업데이트";
"20 秒没听到声音。通话和会议 app 的声音系统不让录" = "20초 동안 소리가 안 들려요. 통화 및 회의 앱은 녹음할 수 없어요";
diff --git a/android/cli/src/main/kotlin/cicada/cli/Tests.kt b/android/cli/src/main/kotlin/cicada/cli/Tests.kt
index e6a95d6..7270582 100644
--- a/android/cli/src/main/kotlin/cicada/cli/Tests.kt
+++ b/android/cli/src/main/kotlin/cicada/cli/Tests.kt
@@ -70,6 +70,7 @@ object Tests {
"longMeetingsAreNotedInPartsByTime" to ::longMeetingsAreNotedInPartsByTime,
"topicStartsWhenItsLinesWereSaid" to ::topicStartsWhenItsLinesWereSaid,
"loadsTheMacsTranslations" to ::loadsTheMacsTranslations,
+ "pluralsFromTheMacsStringsdict" to ::pluralsFromTheMacsStringsdict,
"keyFileIsPrivate" to ::keyFileIsPrivate,
"onboardingKeepsAWorkingSavedKey" to ::onboardingKeepsAWorkingSavedKey,
"lingRequestShape" to ::lingRequestShape,
@@ -696,6 +697,28 @@ object Tests {
equal("x\ny", t["a \"b\""], "escapes")
equal("%@ min", t["%@ 分钟"], "placeholders kept")
equal("3 分钟", Strings.LF("%@ 分钟", 3), "LF fills %@")
+ equal("1 句", Strings.LF("%lld 句", 1), "Chinese has no plural forms")
+ }
+
+ fun pluralsFromTheMacsStringsdict() {
+ loadStrings("en")
+ equal("1 line", Strings.LF("%lld 句", 1), "one")
+ equal("2 lines", Strings.LF("%lld 句", 2), "other")
+ equal("0 lines", Strings.LF("%lld 句", 0), "zero is other in English")
+ equal("Heard 1 line", Strings.LF("已听到 %lld 句", 1), "text around the plural")
+ equal("12:00 · 1 line", Strings.LF("%@ · %lld 句", "12:00", 1), "positional %2$#@n@ after %1$@")
+ equal("Recorded 03:15 · 7 lines. Ending stops listening and writes the full notes.",
+ Strings.LF("已记录 %@ · %lld 句。结束后停止收听,并整理一份完整版纪要。", "03:15", 7), "positional, other")
+ equal("1 point", Strings.LF("%lld 条要点", 1L), "a Long counts too")
+ equal("Live notes", Strings.L("实时纪要"), "plain strings still load")
+ loadStrings("ja")
+ equal("1文", Strings.LF("%lld 句", 1), "no stringsdict: the .strings value")
+ loadStrings("zh-Hans") // the other tests read the Chinese
+ val p = Strings.parsePlurals("<?xml version=\"1.0\"?>\n<!DOCTYPE plist>\n<plist version=\"1.0\"><dict><key>a &amp; b</key><dict>" +
+ "<key>NSStringLocalizedFormatKey</key><string>%#@x@ &lt;</string><key>x</key><dict><key>one</key><string>%lld y</string>" +
+ "<key>other</key><string>%lld ys</string></dict></dict></dict></plist>")
+ equal("%#@x@ <", p["a & b"]?.format, "entities")
+ equal("%lld ys", p["a & b"]?.forms?.get("x")?.get("other"), "forms")
}
fun onboardingKeepsAWorkingSavedKey() {
diff --git a/android/core/src/main/kotlin/cicada/Strings.kt b/android/core/src/main/kotlin/cicada/Strings.kt
index 7c3539c..b5bcc02 100644
--- a/android/core/src/main/kotlin/cicada/Strings.kt
+++ b/android/core/src/main/kotlin/cicada/Strings.kt
@@ -4,10 +4,16 @@ import java.util.Locale
/// UI text. The Chinese text is the key, as on the Mac, and the translations are the Mac's own
/// `Resources/{en,ja,ko}.lproj/Localizable.strings` (copied into the APK's assets at build time),
-/// plus `Android.strings` for text only the Android app has. Placeholders are the Mac's `%@`,
+/// plus `Android.strings` for text only the Android app has, and the Mac's
+/// `Localizable.stringsdict` for plurals ("1 line", "2 lines"). Placeholders are the Mac's `%@`,
/// filled in order. Never for prompts, logs or stored values.
object Strings {
@Volatile private var table: Map<String, String> = emptyMap()
+ @Volatile private var plurals: Map<String, Plural> = emptyMap()
+
+ /// A stringsdict entry: the format with `%#@n@` placeholders, and each variable's forms by
+ /// plural category ("one", "other").
+ class Plural(val format: String, val forms: Map<String, Map<String, String>>)
@Volatile var language = "zh-Hans"; private set
val locale: Locale get() = when (language) {
@@ -25,31 +31,89 @@ object Strings {
read("$language.lproj/$name")?.let { t.putAll(parse(it)) }
}
table = t
+ plurals = read("$language.lproj/Localizable.stringsdict")?.let { parsePlurals(it) } ?: emptyMap()
}
+ /// Only English has more than one form among the app's languages: "one" for exactly 1.
+ private fun category(n: Any?): String =
+ if (language == "en" && (n as? Number)?.toDouble() == 1.0) "one" else "other"
+
@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), and the
- /// numbers `%lld` / `%ld` / `%d`.
+ /// numbers `%lld` / `%ld` / `%d`. A key in the stringsdict takes its plural form for the
+ /// number: `%#@n@` (or `%2$#@n@`) becomes variable n's "one" or "other" text.
@JvmStatic fun LF(key: String, vararg args: Any): String {
- val text = L(key)
+ val plural = plurals[key]
+ val text = plural?.format ?: L(key)
val b = StringBuilder()
var next = 0
var i = 0
while (i < text.length) {
- 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
+ if (text[i] != '%' || i + 1 >= text.length) { b.append(text[i]); i++; continue }
+ // An optional position: %2$…
+ var j = i + 1
+ var position: Int? = null
+ if (text[j].isDigit() && j + 1 < text.length && text[j + 1] == '$') { position = text[j] - '1'; j += 2 }
+ fun arg() = args.getOrNull(position ?: next++)
+ if (text.startsWith("#@", j) && plural != null) {
+ val end = text.indexOf('@', j + 2)
+ if (end > 0) {
+ val n = arg()
+ val forms = plural.forms[text.substring(j + 2, end)] ?: emptyMap()
+ val form = forms[category(n)] ?: forms["other"] ?: ""
+ b.append(form.replace("%lld", "$n").replace("%ld", "$n").replace("%d", "$n"))
+ i = end + 1; continue
+ }
}
+ val spec = listOf("@", "lld", "ld", "d").firstOrNull { text.startsWith(it, j) }
+ if (spec != null) { b.append(arg() ?: ""); i = j + spec.length; continue }
b.append(text[i]); i++
}
return b.toString()
}
+ /// The Mac's Localizable.stringsdict (an XML plist of dicts and strings) as key → Plural. A
+ /// small reader of its own: the XML parsers want to fetch the plist DTD from apple.com.
+ fun parsePlurals(xml: String): Map<String, Plural> {
+ var i = 0
+ fun tag(): String? {
+ while (true) {
+ val a = xml.indexOf('<', i); if (a < 0) return null
+ val e = xml.indexOf('>', a); if (e < 0) return null
+ i = e + 1
+ val t = xml.substring(a + 1, e).trim()
+ if (!t.startsWith("?") && !t.startsWith("!")) return t
+ }
+ }
+ fun text(close: String): String {
+ val e = xml.indexOf(close, i).let { if (it < 0) xml.length else it }
+ val t = xml.substring(i, e); i = minOf(xml.length, e + close.length)
+ return t.replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&apos;", "'").replace("&amp;", "&")
+ }
+ fun dict(): Map<String, Any> {
+ val m = LinkedHashMap<String, Any>()
+ while (true) {
+ val t = tag() ?: return m
+ if (t == "/dict") return m
+ if (t != "key") continue
+ val k = text("</key>")
+ when (tag()) { "dict" -> m[k] = dict(); "string" -> m[k] = text("</string>"); "string/" -> m[k] = "" }
+ }
+ }
+ while (true) { val t = tag() ?: return emptyMap(); if (t == "dict") break }
+ val result = HashMap<String, Plural>()
+ for ((key, v) in dict()) {
+ val entry = v as? Map<*, *> ?: continue
+ val format = entry["NSStringLocalizedFormatKey"] as? String ?: continue
+ val forms = entry.mapNotNull { (name, d) ->
+ (d as? Map<*, *>)?.let { name as String to it.entries.filter { e -> e.value is String }.associate { e -> e.key as String to e.value as String } }
+ }.toMap()
+ result[key] = Plural(format, forms)
+ }
+ return result
+ }
+
/// `"key" = "value";` lines, with /* */ and // comments and \" \\ \n escapes.
fun parse(text: String): Map<String, String> {
val result = HashMap<String, String>()