A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Sim: refuse a hand-edited ui.yaml key or placeholder the English table lacks (Fixes #2598) (#2673)
Beri3e3c2df8cbd0
diff --git a/changelog.d/2598-ui-translation-keys.md b/changelog.d/2598-ui-translation-keys.mdnew file mode 100644index 000000000..89c542e42--- /dev/null+++ b/changelog.d/2598-ui-translation-keys.md@@ -0,0 +1,7 @@+### Fixed++- A hand-edited translation of the interface is now checked when the content loads in the editor, on the command line+ and before every player build, as the translation editor already checks a save (Fixes #2598). A string the English+ table does not have, such as a misspelt key, or a string whose placeholders differ from the English one, is refused+ with the file and the key, so a typo can no longer leave a line in English unnoticed or drop a number from it. Hebrew+ is unchanged.diff --git a/docs/translations.md b/docs/translations.mdindex 26bdc4110..919137d53 100644--- a/docs/translations.md+++ b/docs/translations.md@@ -150,7 +150,11 @@ separate call, made where a language is adopted.are not languages, and hidden names (`.DS_Store`) are ignored.- **The checks:** each language is parsed, must name itself (`CheckDeclaredLanguage`), may not translate the advisors orHow to play unless it is Hebrew, translates only events that exist (an event a ruleset leaves out counts) and every- option of each, and keys only fields that `Fields` offers. A read failure is reported with its file.+ option of each, and keys only fields that `Fields` offers. A read failure is reported with its file. Where the content+ is a checkout's (`unity/Assets/UI/strings.en.yaml` beside the content folder: the editor, the CLI, the tests and+ `ContentSync`'s load of the source before every player build), a `ui.yaml` other than Hebrew may key only strings that+ `strings.en.yaml` has, each with the English one's placeholders, the rule `tools/translate.py` applies to an editor+ save (#2598). A player has no English table beside its copy, so the build is where it is caught.- **A stale translation:** when an event or a record changes so that a translation no longer matches it, the load isrefused (owner, 1 November 2026: "Refuse + say how to fix (Recommended)"), so nothing stale ships. The error says how tounblock without a translator: update the entry, or delete it from the language's file, and that text shows in English.diff --git a/sim/Statecraft.Sim.Tests/TranslationLoaderTests.cs b/sim/Statecraft.Sim.Tests/TranslationLoaderTests.csindex 152fb4ecb..7b1812f34 100644--- a/sim/Statecraft.Sim.Tests/TranslationLoaderTests.cs+++ b/sim/Statecraft.Sim.Tests/TranslationLoaderTests.cs@@ -168,6 +168,33 @@ public class TranslationLoaderTestsRefused(new Dictionary<string, string> { ["ro/ui.yaml"] = "ui.lang: \"fr\"\n" }).Message);}+ // #2598: a hand-edited ui.yaml is held to the English table (unity/Assets/UI/strings.en.yaml) when the content is a+ // checkout's, as tools/translate.py holds an editor save: a misspelt key would leave the real one in English unnoticed.+ [Fact]+ public void AUiKeyTheEnglishLacksIsRefused()+ {+ var e = Refused(new Dictionary<string, string> { ["ro/ui.yaml"] = Ro + "hud.trun: \"Tura {0}\"\n" });+ Assert.Equal(("translations/ro/ui.yaml", "hud.trun"), (e.File, e.Field));+ Assert.Contains("strings.en.yaml has no such key", e.Message);+ Assert.Contains("delete it from translations/ro/ui.yaml", e.Message);+ }++ // The same rule as translate.py's _check: as many of each {...} token as the English (hud.attention: "Attention {0:0.0}+ // of {1:0.0}"), in any order, so a dropped {0} cannot lose its value and a renamed one cannot throw in string.Format.+ [Theory]+ [InlineData("Atenție {0:0.0}", false)] // one dropped+ [InlineData("Atenție {0} din {1:0.0}", false)] // one renamed (its format changed)+ [InlineData("Atenție {0:0.0} din {1:0.0} {1:0.0}", false)] // one twice+ [InlineData("{1:0.0} rămasă din {0:0.0}", true)] // reordered: the same tokens+ public void UiPlaceholdersMustMatchTheEnglish(string text, bool loads)+ {+ var files = new Dictionary<string, string> { ["ro/ui.yaml"] = Ro + $"hud.attention: \"{text}\"\n" };+ if (loads) { Assert.Single(LoadReal(files).Translations); return; }+ var e = Refused(files);+ Assert.Equal(("translations/ro/ui.yaml", "hud.attention"), (e.File, e.Field));+ Assert.Contains("placeholders differ from the English ({0:0.0}, {1:0.0})", e.Message);+ }+[Theory][InlineData("advisor.defeat_economy")][InlineData("how_to_play.title")]diff --git a/sim/Statecraft.Sim/ContentLoader.cs b/sim/Statecraft.Sim/ContentLoader.csindex 5a03bbcbc..1d0afb823 100644--- a/sim/Statecraft.Sim/ContentLoader.cs+++ b/sim/Statecraft.Sim/ContentLoader.cs@@ -617,6 +617,11 @@ namespace Statecraft.Simvar path = Path.Combine(translationsRoot!, rel); // only called for a file Collect found therereturn File.Exists(path) ? File.ReadAllText(path) : null;}, TranslationLoader.Collect(translationsRoot));+ // #2598: a checkout has the English UI table beside its content, so the editor, the CLI, the tests and ContentSync's+ // load of the source before every player build hold each ui.yaml to it. A player has none beside its copy.+ if (content.Translations.Any(t => t.Lang != Translations.Hebrew && t.Ui.Count > 0)+ && TranslationLoader.EnglishUi(contentRoot) is string english)+ TranslationLoader.CheckUiAgainstEnglish(content.Translations, english);// Only this overload can list the directory. The readFile overload (WebGL) cannot, so a misnamed file is caught// here and by RealContentTests before anything ships.var rulesDir = Path.Combine(contentRoot, "rules");diff --git a/sim/Statecraft.Sim/TranslationLoader.cs b/sim/Statecraft.Sim/TranslationLoader.csindex 187c2f3d3..271810108 100644--- a/sim/Statecraft.Sim/TranslationLoader.cs+++ b/sim/Statecraft.Sim/TranslationLoader.cs@@ -2,6 +2,7 @@ using System;using System.Collections.Generic;using System.IO;using System.Linq;+using System.Text.RegularExpressions;using YamlDotNet.Core;using YamlDotNet.RepresentationModel;@@ -133,6 +134,47 @@ namespace Statecraft.Sim"the advisors and How to play stay English or Hebrew only, for now (owner, 24 October 2026, #1604)");}+ // A placeholder as tools/translate.py's TOKEN reads one: braces around anything that is not a brace.+ static readonly Regex Token = new Regex(@"\{[^{}]+\}");++ /// <summary>The English UI table of a checkout (unity/Assets/UI/strings.en.yaml beside the folder holding+ /// <paramref name="contentRoot"/>), or null when there is none, as in a player's StreamingAssets or a test fixture.</summary>+ internal static string? EnglishUi(string contentRoot)+ {+ var parent = Path.GetDirectoryName(Path.GetFullPath(contentRoot).TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));+ var path = parent == null ? null : Path.Combine(parent, "unity", "Assets", "UI", "strings.en.yaml");+ return path != null && File.Exists(path) ? File.ReadAllText(path) : null;+ }++ /// <summary>#2598: every non-Hebrew ui.yaml keys only strings that <paramref name="englishUi"/> (strings.en.yaml) has,+ /// each with as many of each placeholder as the English one, the rule tools/translate.py's _check applies to an+ /// editor save. A hand-edited file is otherwise never compared: a misspelt key leaves the real one in English with no+ /// word, and a dropped or renamed placeholder loses its value or makes string.Format throw (UiText.Format). Hebrew is+ /// held to strings.he.yaml by its own tests.</summary>+ internal static void CheckUiAgainstEnglish(IEnumerable<TranslationSet> sets, string englishUi)+ {+ var english = new Dictionary<string, string>(StringComparer.Ordinal);+ var stream = new YamlStream();+ try { stream.Load(new StringReader(englishUi)); }+ catch (YamlException e) { throw new ContentException("strings.en.yaml", $"line {e.Start.Line}", e.InnerException?.Message ?? e.Message); }+ if (stream.Documents.Count > 0 && stream.Documents[0].RootNode is YamlMappingNode map)+ foreach (var kv in map.Children)+ if (kv.Key is YamlScalarNode k && kv.Value is YamlScalarNode v) english[k.Value ?? ""] = v.Value ?? "";+ foreach (var set in sets.Where(s => s.Lang != Translations.Hebrew))+ {+ var where = Where(set, Translations.UiFile);+ var unblock = Unblock(set, where, "it", "string");+ foreach (var kv in set.Ui)+ {+ if (!english.TryGetValue(kv.Key, out var en)) throw new ContentException(where, kv.Key, "strings.en.yaml has no such key" + unblock);+ var tokens = Token.Matches(en).Cast<Match>().Select(m => m.Value).ToList();+ var mine = Token.Matches(kv.Value).Cast<Match>().Select(m => m.Value);+ if (!tokens.OrderBy(t => t, StringComparer.Ordinal).SequenceEqual(mine.OrderBy(t => t, StringComparer.Ordinal), StringComparer.Ordinal))+ throw new ContentException(where, kv.Key, $"placeholders differ from the English ({(tokens.Count == 0 ? "none" : string.Join(", ", tokens))})" + unblock);+ }+ }+ }+/// <summary>How a writer unblocks a stale translation without a translator (owner, 1 November 2026: "Refuse + say how/// to fix (Recommended)"): delete the entry and the text shows in English. Not for Hebrew, whose files must equal the/// inline Hebrew, so a deleted entry is a missing one.</summary>