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

wquguru7250b1e67409
diff --git a/windows/src/Cicada.Cli/Cicada.Cli.csproj b/windows/src/Cicada.Cli/Cicada.Cli.csproj
index 3f2db54..4769523 100644
--- a/windows/src/Cicada.Cli/Cicada.Cli.csproj
+++ b/windows/src/Cicada.Cli/Cicada.Cli.csproj
@@ -9,7 +9,7 @@
</PropertyGroup>
<ItemGroup>
<!-- The Mac app's translations, used as they are (Chinese text is the key). -->
- <None Include="../../../Resources/**/Localizable.strings" LinkBase="Resources" CopyToOutputDirectory="PreserveNewest" />
+ <None Include="../../../Resources/**/Localizable.strings*" LinkBase="Resources" CopyToOutputDirectory="PreserveNewest" />
<ProjectReference Include="../Cicada.Core/Cicada.Core.csproj" />
</ItemGroup>
</Project>
diff --git a/windows/src/Cicada.Core/Strings.cs b/windows/src/Cicada.Core/Strings.cs
index bd46b84..6a4b448 100644
--- a/windows/src/Cicada.Core/Strings.cs
+++ b/windows/src/Cicada.Core/Strings.cs
@@ -5,11 +5,17 @@ namespace Cicada;
/// 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 next to the app at build time, plus
-/// `windows/src/Cicada/Strings/*.lproj/Windows.strings` for text only the Windows app has.
-/// Placeholders are the Mac's `%@`, filled in order. Never for prompts, logs or stored values.
+/// `windows/src/Cicada/Strings/*.lproj/Windows.strings` for text only the Windows 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.
public static class Strings
{
static Dictionary<string, string> table = [];
+ static Dictionary<string, Plural> plurals = [];
+
+ /// A stringsdict entry: the format with `%#@n@` placeholders, and each variable's forms by
+ /// plural category ("one", "other").
+ public sealed record Plural(string Format, Dictionary<string, Dictionary<string, string>> Forms);
public static string Language { get; private set; } = "zh-Hans";
/// Dates in the app's language, not Windows': English Cicada on Chinese Windows writes "Nov 4".
public static CultureInfo Culture { get; private set; } = CultureInfo.GetCultureInfo("zh-CN");
@@ -34,19 +40,40 @@ public static class Strings
if (read($"{Language}.lproj/{name}") is not { } text) continue;
foreach (var (k, v) in Parse(text)) table[k] = v;
}
+ plurals = read($"{Language}.lproj/Localizable.stringsdict") is { } dict ? ParsePlurals(dict) : [];
}
+ /// Only English has more than one form among the app's languages: "one" for exactly 1.
+ static string Category(object? n) =>
+ Language == "en" && n is IConvertible c && c.ToDouble(CultureInfo.InvariantCulture) == 1 ? "one" : "other";
+
public static string L(string key) => table.TryGetValue(key, out var v) && v.Length > 0 ? v : key;
- /// L() with the Mac's `%@` placeholders: LF("纪要停留在 %@。", clock).
+ /// L() with the Mac's `%@` placeholders: LF("纪要停留在 %@。", clock). A key in the stringsdict
+ /// takes its plural form for the number: `%#@n@` (or `%2$#@n@`) becomes variable n's "one" or
+ /// "other" text.
public static string LF(string key, params object[] args)
{
- var text = L(key);
+ var plural = plurals.GetValueOrDefault(key);
+ var text = plural?.Format ?? L(key);
var b = new StringBuilder();
int next = 0;
for (int i = 0; i < text.Length; i++)
{
- if (text[i] == '%' && i + 1 < text.Length && text[i + 1] == '@')
+ // %#@n@ or %2$#@n@: the variable's form for its number, with the number in it.
+ int j = i + 1;
+ int? position = null;
+ if (plural is not null && text[i] == '%' && j + 1 < text.Length && char.IsDigit(text[j]) && text[j + 1] == '$') { position = text[j] - '1'; j += 2; }
+ if (plural is not null && text[i] == '%' && j + 1 < text.Length && text[j] == '#' && text[j + 1] == '@' && text.IndexOf('@', j + 2) is var end && end > 0)
+ {
+ int at = position ?? next++;
+ var n = at >= 0 && at < args.Length ? args[at] : null;
+ var forms = plural.Forms.GetValueOrDefault(text[(j + 2)..end]) ?? [];
+ var form = forms.GetValueOrDefault(Category(n)) ?? forms.GetValueOrDefault("other") ?? "";
+ b.Append(form.Replace("%lld", $"{n}").Replace("%ld", $"{n}").Replace("%d", $"{n}"));
+ i = end;
+ }
+ else if (text[i] == '%' && i + 1 < text.Length && text[i + 1] == '@')
{
b.Append(next < args.Length ? args[next++] : "");
i++;
@@ -70,6 +97,56 @@ public static class Strings
return b.ToString();
}
+ /// The Mac's Localizable.stringsdict (an XML plist of dicts and strings) as key → Plural. A
+ /// small reader of its own: XmlReader refuses the plist's DOCTYPE unless told to skip DTDs.
+ public static Dictionary<string, Plural> ParsePlurals(string xml)
+ {
+ int i = 0;
+ string? Tag()
+ {
+ while (true)
+ {
+ int a = xml.IndexOf('<', i); if (a < 0) return null;
+ int e = xml.IndexOf('>', a); if (e < 0) return null;
+ i = e + 1;
+ var t = xml[(a + 1)..e].Trim();
+ if (!t.StartsWith('?') && !t.StartsWith('!')) return t;
+ }
+ }
+ string Text(string close)
+ {
+ int e = xml.IndexOf(close, i); if (e < 0) e = xml.Length;
+ var t = xml[i..e]; i = Math.Min(xml.Length, e + close.Length);
+ return t.Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "\"").Replace("&apos;", "'").Replace("&amp;", "&");
+ }
+ Dictionary<string, object> Dict()
+ {
+ var m = new Dictionary<string, object>();
+ while (Tag() is { } t && t != "/dict")
+ {
+ if (t != "key") continue;
+ var k = Text("</key>");
+ switch (Tag())
+ {
+ case "dict": m[k] = Dict(); break;
+ case "string": m[k] = Text("</string>"); break;
+ case "string/": m[k] = ""; break;
+ }
+ }
+ return m;
+ }
+ var result = new Dictionary<string, Plural>();
+ while (Tag() is { } t) if (t == "dict") break;
+ foreach (var (key, v) in Dict())
+ {
+ if (v is not Dictionary<string, object> entry || entry.GetValueOrDefault("NSStringLocalizedFormatKey") is not string format) continue;
+ var forms = entry.Where(e => e.Value is Dictionary<string, object>).ToDictionary(
+ e => e.Key, e => ((Dictionary<string, object>)e.Value).Where(f => f.Value is string).ToDictionary(f => f.Key, f => (string)f.Value));
+ result[key] = new Plural(format, forms);
+ }
+ return result;
+ }
+
/// `"key" = "value";` lines, with /* */ and // comments and \" \\ \n escapes.
public static Dictionary<string, string> Parse(string text)
{
diff --git a/windows/src/Cicada/Cicada.csproj b/windows/src/Cicada/Cicada.csproj
index 70f2191..1b5ad5b 100644
--- a/windows/src/Cicada/Cicada.csproj
+++ b/windows/src/Cicada/Cicada.csproj
@@ -27,7 +27,7 @@
<Using Include="System.IO" />
<!-- The UI text, inside the exe (Bundled.cs reads it): the installer is this one file, so it
must speak the user's language before anything is installed. -->
- <EmbeddedResource Include="../../../Resources/**/Localizable.strings" LogicalName="strings/%(RecursiveDir)%(Filename)%(Extension)" />
+ <EmbeddedResource Include="../../../Resources/**/Localizable.strings*" LogicalName="strings/%(RecursiveDir)%(Filename)%(Extension)" />
<EmbeddedResource Include="Strings/**/Windows.strings" LogicalName="strings/%(RecursiveDir)%(Filename)%(Extension)" />
<!-- Both made by scripts/build.sh from docs/icon.png and Resources/sample/sample.m4a; committed so CI on Windows needs no Mac tools. -->
<Resource Include="Assets/cicada.ico" Link="cicada.ico" />
diff --git a/windows/tests/Cicada.Tests/Cicada.Tests.csproj b/windows/tests/Cicada.Tests/Cicada.Tests.csproj
index 7c7b078..67770ae 100644
--- a/windows/tests/Cicada.Tests/Cicada.Tests.csproj
+++ b/windows/tests/Cicada.Tests/Cicada.Tests.csproj
@@ -6,7 +6,7 @@
<RootNamespace>Cicada.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
- <None Include="../../../Resources/**/Localizable.strings" LinkBase="Resources" CopyToOutputDirectory="PreserveNewest" />
+ <None Include="../../../Resources/**/Localizable.strings*" LinkBase="Resources" CopyToOutputDirectory="PreserveNewest" />
<None Include="../../src/Cicada/Strings/**/Windows.strings" LinkBase="Resources" CopyToOutputDirectory="PreserveNewest" />
<None Include="Fixtures/**" CopyToOutputDirectory="PreserveNewest" />
<ProjectReference Include="../../src/Cicada.Core/Cicada.Core.csproj" />
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index c6a3b99..b6d3b8b 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -959,6 +959,33 @@ static class Tests
Equal("%@ min", t["%@ 分钟"], "placeholders kept");
Equal("3 分钟", Strings.LF("%@ 分钟", 3), "LF fills %@");
Equal("正在下载 1.3.0:59%", Strings.LF("正在下载 %@:%lld%%", "1.3.0", 59), "%% is one percent sign");
+ Equal("1 句", Strings.LF("%lld 句", 1), "Chinese has no plural forms");
+ }
+
+ public static void PluralsFromTheMacsStringsdict()
+ {
+ var res = Path.Combine(AppContext.BaseDirectory, "Resources");
+ try
+ {
+ Strings.Load(res, "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 · 1 line. Ending stops listening and writes the full notes.",
+ Strings.LF("已记录 %@ · %lld 句。结束后停止收听,并整理一份完整版纪要。", "03:15", 1), "the end-meeting question");
+ Equal("1 point", Strings.LF("%lld 条要点", 1L), "a long counts too");
+ Equal("Live notes", Strings.L("实时纪要"), "plain strings still load");
+ Strings.Load(res, "ja");
+ Equal("1文", Strings.LF("%lld 句", 1), "no stringsdict: the .strings value");
+ var 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["x"]["other"], "forms");
+ }
+ finally { Strings.Load(res, "zh-Hans"); }
}
// MARK: Ling