Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

6.2 KB
Raw Blame
#nullable enable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using YamlDotNet.Serialization;

namespace Statecraft.Game.UI
{
    /// <summary>UI strings kept outside code (unity/Assets/UI/strings.en.yaml): key -> string.Format pattern.</summary>
    public sealed class UiText
    {
        /// <summary>U+200E LEFT-TO-RIGHT MARK: invisible, takes no room, and reads as a left-to-right letter to the bidi rules.</summary>
        public const char LeftToRightMark = '‎';

        // A format item ("{0}", "{1:N0}", "{2,-8}") or an escaped brace, as string.Format reads them.
        static readonly Regex FormatItem = new Regex(@"\{\{|\}\}|\{(\d+)((?:,\s*-?\d+)?(?::[^{}]*)?)\}", RegexOptions.CultureInvariant);

        static readonly char[] Braces = { '{', '}' };

        readonly Dictionary<string, string> map;
        readonly Dictionary<string, bool> hebrew = new Dictionary<string, bool>();
        readonly string file;
        UiText(Dictionary<string, string> map, string file) { this.map = map; this.file = file; }

        /// <summary>Parses the strings file. Throws InvalidOperationException naming the file when it is empty or not a map.</summary>
        public static UiText Parse(string yaml, string file = "strings.en.yaml")
        {
            Dictionary<string, string> m;
            try { m = new DeserializerBuilder().WithDuplicateKeyChecking().Build().Deserialize<Dictionary<string, string>>(yaml ?? ""); }
            catch (YamlDotNet.Core.YamlException e) { throw new InvalidOperationException($"{file}: {e.Message}", e); }
            if (m == null || m.Count == 0) throw new InvalidOperationException($"{file}: no strings");
            return new UiText(m, file);
        }

        /// <summary>These strings with <paramref name="lines"/> (a translations/&lt;lang&gt;/ui.yaml, #1606) over them: a key the
        /// translation has reads its text, every other key the text here, so a new language falls back to English per string.</summary>
        public UiText Over(IEnumerable<KeyValuePair<string, string>> lines, string file)
        {
            var m = new Dictionary<string, string>(map);
            foreach (var line in lines) m[line.Key] = line.Value;
            return new UiText(m, file);
        }

        /// <summary>True when the strings have <paramref name="key"/> (a lookup that must not throw, as GameStart's fallback).</summary>
        public bool Has(string key) => map.ContainsKey(key);

        /// <summary>The keys in this strings file. The collection is read-only to callers.</summary>
        public IEnumerable<string> Keys => map.Keys;

        public string this[string key] =>
            map.TryGetValue(key, out var v) ? v : throw new KeyNotFoundException($"{file} has no key '{key}'");

        /// <summary>string.Format over the key's pattern. In a Hebrew pattern (its own first letter, the format items left
        /// out, is Hebrew) each value with no Hebrew in it is put between two left-to-right marks (#543): in a right-to-left
        /// line the bidi rules would otherwise draw "-$4.2m" as "$4.2m-" and split "15 May 1948" into "May 1948 15". A value
        /// with Hebrew in it is left alone: a Hebrew word (a seat unit) between marks would make the space next to it left to
        /// right, and a value Format has already made from a Hebrew pattern has its own values marked. Every other pattern,
        /// so all of English, gives string.Format's text unchanged. A value written straight after a Hebrew letter (a
        /// one-letter prefix, "ל{2}") takes a maqaf when it starts with a digit or a Latin letter (HE-07): "לחיפה" but
        /// "ל־Haifa" where a name has no Hebrew form yet.</summary>
        public string Format(string key, params object[] args)
        {
            var pattern = this[key];
            if (!IsHebrew(key, pattern)) return string.Format(CultureInfo.InvariantCulture, pattern, args);
            // A brace left once the format items are taken out (a lone "}", "{x}") makes string.Format throw; so does this.
            if (FormatItem.Replace(pattern, "").IndexOfAny(Braces) >= 0)
                throw new FormatException($"{file}: '{key}' is not a valid format pattern: {pattern}");
            return FormatItem.Replace(pattern, m =>
            {
                if (!m.Groups[1].Success) return m.Value.Substring(1);              // "{{" or "}}"
                var value = string.Format(CultureInfo.InvariantCulture, "{0" + m.Groups[2].Value + "}", Arg(args, m.Groups[1].Value));
                var marked = value.Length == 0 || UiDirection.HasRightToLeft(value) ? value : LeftToRightMark + value + LeftToRightMark;
                return m.Index > 0 && IsHebrewLetter(pattern[m.Index - 1]) && StartsWithNumeralOrLatin(value) ? Maqaf + marked : marked;
            });
        }

        /// <summary>U+05BE HEBREW PUNCTUATION MAQAF.</summary>
        const char Maqaf = '\u05BE';

        static bool IsHebrewLetter(char c) => c >= '\u05D0' && c <= '\u05EA';

        /// <summary>Whether the value's first letter or digit, past any direction mark, is a digit or a non-Hebrew letter
        /// (HE-07: a prefix joins a Hebrew word directly, "לחיפה", and takes a maqaf before a numeral or Latin letters,
        /// "ל־Haifa", "ב־15 במאי 1948").</summary>
        static bool StartsWithNumeralOrLatin(string value)
        {
            foreach (var c in value)
            {
                if (c == LeftToRightMark || c == '\u200F' || c == '\u061C' || c >= '\u202A' && c <= '\u202E' || c >= '\u2066' && c <= '\u2069') continue;
                return char.IsLetterOrDigit(c) && !IsHebrewLetter(c);
            }
            return false;
        }

        static object? Arg(object[] args, string index)
        {
            int i = int.Parse(index, CultureInfo.InvariantCulture);
            if (args == null || i >= args.Length) throw new FormatException("Index (zero based) must be greater than or equal to zero and less than the size of the argument list.");
            return args[i];
        }

        bool IsHebrew(string key, string pattern)
        {
            if (!hebrew.TryGetValue(key, out var h)) hebrew[key] = h = UiDirection.RightToLeft(FormatItem.Replace(pattern, ""));
            return h;
        }
    }
}