A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Languages: a UI language must name itself in ui.lang (#1606) (#1736)
EllaSharakanskid9ca2fc1821e
diff --git a/changelog.d/1606-ui-lang-declared.md b/changelog.d/1606-ui-lang-declared.mdnew file mode 100644index 000000000..e6ae74fb8--- /dev/null+++ b/changelog.d/1606-ui-lang-declared.md@@ -0,0 +1,8 @@+### Added++- A language offered to the player must name itself: `Translations.CheckDeclaredLanguage` refuses a+ `translations/<lang>/ui.yaml` with no `ui.lang`, and one whose `ui.lang` names a different language from its+ directory (#1606). The second is the case worth the check — a `ui.yaml` copied from another language with its code+ left behind passes every shape check, and `ui.lang` is what the player reads to pick the Hebrew face and the+ language branches in `EventCardPresenter` and `SharePresenter`, so the failure is silently the wrong language+ rather than an error. Documented in `docs/translations.md`; no player code changes yet.diff --git a/docs/translations.md b/docs/translations.mdindex ce679db5b..2ad37001a 100644--- a/docs/translations.md+++ b/docs/translations.md@@ -82,6 +82,19 @@ non-empty string, `review` only `agent-checked`, and `sources` as a list of `{ utext itself (an event is all or none, placeholders as in the English, no stray `{` or `}`, Hebrew letters, no directionmarks, the UI's approved wording) are the content rules. They run on the text where it is used, as they do today.+## A UI language names itself++`ui.yaml` carries `ui.lang`, and its value is the language's own code — `ui.lang: "ro"` in `translations/ro/`. It is+not a label. The player reads this key to decide which language it is showing: the Hebrew face on the UI root, and the+branches in `EventCardPresenter` and `SharePresenter`. So a file that says the wrong thing here does not fail, it+shows one language's words laid out as another's.++A language offered to the player must therefore pass `Translations.CheckDeclaredLanguage`, which refuses a `ui.yaml`+with no `ui.lang` and one whose `ui.lang` names a different language from its directory. The second is the case worth+the check: a `ui.yaml` copied from another language with its code left behind passes every shape check in `Parse`.+`Parse` itself does not require the key, because it is also used to read a file's shape in isolation — the check is a+separate call, made where a language is adopted.+## The implementations and their tests- `sim/Statecraft.Sim/Translations.cs`: keys, parsing, writing, `Resolve` and `InlineHebrew`.diff --git a/sim/Statecraft.Sim.Tests/TranslationsTests.cs b/sim/Statecraft.Sim.Tests/TranslationsTests.csindex 4a7010b3c..3d329f478 100644--- a/sim/Statecraft.Sim.Tests/TranslationsTests.cs+++ b/sim/Statecraft.Sim.Tests/TranslationsTests.cs@@ -354,4 +354,61 @@ public class TranslationsTestsvar e = Assert.Throws<ContentException>(() => HebrewOverlay.Apply(c, Translations.Parse("he", null, events, content)));Assert.Contains(message, e.Message);}++ // ---- a UI language naming itself (#1606) ----++ [Fact]+ public void AUiFileThatDoesNotNameItsLanguageIsRefused()+ {+ // Parse checks each file's shape, and a copied file passes that; the player then reads ui.lang and trusts it.+ var set = Translations.Parse("fr", "hud.turn: \"Tour {0}\"\n", null, null);+ var e = Assert.Throws<ContentException>(() => Translations.CheckDeclaredLanguage(set));+ Assert.Contains(Translations.LangKey, e.Message);+ Assert.Contains("must name itself", e.Message);+ Assert.Equal($"translations/fr/{Translations.UiFile}", e.File);+ }++ [Fact]+ public void AUiFileCarryingAnotherLanguagesCodeIsRefused()+ {+ // The case worth the check: ui.yaml copied from another language and its code left behind. Nothing else+ // notices, and the player lays one language's words out as another's.+ var set = Translations.Parse("fr", Translations.LangKey + ": \"he\"\nhud.turn: \"Tour {0}\"\n", null, null);+ var e = Assert.Throws<ContentException>(() => Translations.CheckDeclaredLanguage(set));+ Assert.Contains("says 'he'", e.Message);+ Assert.Contains("fr", e.Message);+ }++ [Fact]+ public void AUiFileThatNamesItsOwnLanguageIsAccepted()+ {+ var set = Translations.Parse("fr", Translations.LangKey + ": \"fr\"\nhud.turn: \"Tour {0}\"\n", null, null);+ Translations.CheckDeclaredLanguage(set);+ Assert.Equal("fr", set.Ui.Single(kv => kv.Key == Translations.LangKey).Value);+ }++ [Theory]+ [InlineData("en")]+ [InlineData("he")]+ public void TheShippedUiFilesNameTheirOwnLanguage(string lang)+ {+ // The two files the player reads today (GameRoot's two TextAsset slots) must pass the check that a third+ // language will be held to.+ var set = Translations.Parse(lang, File.ReadAllText(Ui(lang)), null, null);+ Translations.CheckDeclaredLanguage(set);+ }++ [Fact]+ public void EveryLanguageUnderTranslationsNamesItsOwnLanguage()+ {+ var root = Path.Combine(Repo, "translations");+ var dirs = Directory.GetDirectories(root).OrderBy(d => d, StringComparer.Ordinal).ToList();+ Assert.NotEmpty(dirs); // a vacuous pass here would hide every miswritten file+ foreach (var dir in dirs)+ {+ var ui = Path.Combine(dir, Translations.UiFile);+ if (!File.Exists(ui)) continue;+ Translations.CheckDeclaredLanguage(Translations.Parse(Path.GetFileName(dir), File.ReadAllText(ui), null, null));+ }+ }}diff --git a/sim/Statecraft.Sim/Translations.cs b/sim/Statecraft.Sim/Translations.csindex 52279a763..08eed7808 100644--- a/sim/Statecraft.Sim/Translations.cs+++ b/sim/Statecraft.Sim/Translations.cs@@ -78,6 +78,13 @@ namespace Statecraft.Simpublic const string Hebrew = "he";public const string UiFile = "ui.yaml", EventsFile = "events.yaml", ContentFile = "content.yaml";+ /// <summary>The key by which a UI file says which language it is. Not a label: the player reads it to decide+ /// what it is showing — the Hebrew face on the UI root, and the branches in EventCardPresenter and+ /// SharePresenter — so a file that says the wrong thing here shows the wrong language's text direction and+ /// wording rather than failing. Every shipped UI file declares it (strings.en.yaml, strings.he.yaml,+ /// translations/ro/ui.yaml).</summary>+ public const string LangKey = "ui.lang";+static readonly Regex LangCode = new Regex(@"^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$", RegexOptions.CultureInvariant);static readonly Regex UiKey = new Regex(@"^[A-Za-z0-9_]+(\.[A-Za-z0-9_\-]+)*$", RegexOptions.CultureInvariant);static readonly Regex Id = new Regex(@"^[a-z][a-z0-9_]*$", RegexOptions.CultureInvariant);@@ -97,6 +104,22 @@ namespace Statecraft.Simpublic static bool IsLanguage(string code) => code != null && LangCode.IsMatch(code);+ /// <summary>Refuses a language whose UI file does not name itself in <see cref="LangKey"/>, or names another+ /// language (#1606). Call it before offering a language to the player: <see cref="Parse"/> checks each file's+ /// shape, which a copied-and-forgotten file passes, and the player then reads <see cref="LangKey"/> and trusts+ /// it. A file carrying another language's code is the case worth the check — nothing else notices, and the+ /// result is one language's words laid out as another's.</summary>+ public static void CheckDeclaredLanguage(TranslationSet set)+ {+ if (set == null) throw new ArgumentNullException(nameof(set));+ var where = $"translations/{set.Lang}/{UiFile}";+ var declared = set.Ui.Where(p => p.Key == LangKey).Select(p => p.Value).FirstOrDefault();+ if (declared == null)+ throw new ContentException(where, LangKey, $"a UI language must name itself: add {LangKey}: \"{set.Lang}\"");+ if (declared != set.Lang)+ throw new ContentException(where, LangKey, $"says '{declared}' but the file is {set.Lang}'s");+ }+// ---- parsing ----/// <summary>Reads one language's three files (each may be null: the language has none of that kind). Checks the