The pages built so far now live inside the shell of the mockups instead of the M0 placeholder, every interface text comes from Resources/it-IT, and each user keeps their own look and start page. - Shell: top bar, collapsible side menu with fly-out sub-menus, status bar and tabs that keep each page alive (page type and route values per tab, so a tab never shows another page); at most 12 tabs, least-used clean tab evicted. - One shortcut table with Alt variants for the combinations browsers keep (Ctrl+W, Ctrl+Tab, Ctrl+N); command palette for pages and commands. - ITexts over embedded JSON; tests forbid literal texts in .razor components and check that every key used exists. - User settings in sys_user_settings / user_settings; theme written on the server; profile page with live preview, start page, sessions and shortcuts. - Home, coming-soon pages for later milestones, localized sign-in and error pages; ADR 0020; wiki, CHANGELOG, STATUS and data model updated. Refs #25 #26 #31 #33 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
190 lines
8.2 KiB
C#
190 lines
8.2 KiB
C#
using System.Collections.Frozen;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Text.Json;
|
|
|
|
namespace Khaemhat.Application.Localization;
|
|
|
|
/// <summary>
|
|
/// The texts of the interface (SPEC §1.9, §10.8): keys such as <c>Shell.Menu.Home</c> resolved from
|
|
/// <c>Resources/<culture>/*.json</c>. Components never contain literal texts; they ask this service.
|
|
/// </summary>
|
|
public interface ITexts
|
|
{
|
|
/// <summary>The culture of the texts and of number and date formats.</summary>
|
|
CultureInfo Culture { get; }
|
|
|
|
/// <summary>The text of a key; a missing key gives <c>[key]</c>, visible in the page and caught by the tests.</summary>
|
|
/// <param name="key">The key, <c>Area.Entity.Text</c>.</param>
|
|
/// <returns>The text.</returns>
|
|
string this[string key] { get; }
|
|
|
|
/// <summary>The text of a key with <see cref="string.Format(IFormatProvider, string, object[])"/> placeholders filled in the culture.</summary>
|
|
/// <param name="key">The key.</param>
|
|
/// <param name="args">The values for <c>{0}</c>, <c>{1}</c>...</param>
|
|
/// <returns>The text.</returns>
|
|
string Format(string key, params object?[] args);
|
|
|
|
/// <summary>Whether a key exists.</summary>
|
|
/// <param name="key">The key.</param>
|
|
/// <returns><see langword="true"/> when it has a text.</returns>
|
|
bool Contains(string key);
|
|
|
|
/// <summary>Every key, for the tests and the documentation.</summary>
|
|
IReadOnlyCollection<string> Keys { get; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the texts from JSON files. Nested objects become dotted keys: <c>{"Shell": {"Menu": {"Home": "Home"}}}</c>
|
|
/// gives <c>Shell.Menu.Home</c>; the property <c>_</c> gives a text to an object that also has children. The file
|
|
/// name is not part of the key; two files defining the same key are an error.
|
|
/// </summary>
|
|
public sealed class JsonTextProvider : ITexts
|
|
{
|
|
/// <summary>The prefix of the embedded resources holding the texts: <c>texts/it-IT/Shell.json</c>.</summary>
|
|
public const string ResourcePrefix = "texts/";
|
|
|
|
private readonly FrozenDictionary<string, string> _texts;
|
|
|
|
/// <summary>Creates the provider from JSON documents.</summary>
|
|
/// <param name="culture">The culture.</param>
|
|
/// <param name="documents">The documents, by name (used in error messages).</param>
|
|
public JsonTextProvider(CultureInfo culture, IEnumerable<KeyValuePair<string, string>> documents)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(culture);
|
|
ArgumentNullException.ThrowIfNull(documents);
|
|
Culture = culture;
|
|
var texts = new Dictionary<string, string>(StringComparer.Ordinal);
|
|
foreach (var (name, json) in documents)
|
|
{
|
|
using var document = JsonDocument.Parse(json, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip, AllowTrailingCommas = true });
|
|
if (document.RootElement.ValueKind != JsonValueKind.Object)
|
|
{
|
|
throw new InvalidDataException($"The texts file {name} must contain an object.");
|
|
}
|
|
|
|
Flatten(document.RootElement, string.Empty, name, texts);
|
|
}
|
|
|
|
_texts = texts.ToFrozenDictionary(StringComparer.Ordinal);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public CultureInfo Culture { get; }
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyCollection<string> Keys => _texts.Keys;
|
|
|
|
/// <inheritdoc />
|
|
public string this[string key] => key is not null && _texts.TryGetValue(key, out var text) ? text : "[" + key + "]";
|
|
|
|
/// <summary>Loads the texts of a culture from the embedded resources of an assembly (<see cref="ResourcePrefix"/>).</summary>
|
|
/// <param name="assembly">The assembly, usually the web project.</param>
|
|
/// <param name="culture">The culture, for example <c>it-IT</c>.</param>
|
|
/// <returns>The provider.</returns>
|
|
public static JsonTextProvider FromAssembly(Assembly assembly, CultureInfo culture)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(assembly);
|
|
ArgumentNullException.ThrowIfNull(culture);
|
|
var prefix = ResourcePrefix + culture.Name + "/";
|
|
var documents = new List<KeyValuePair<string, string>>();
|
|
foreach (var resource in assembly.GetManifestResourceNames().Order(StringComparer.Ordinal))
|
|
{
|
|
var name = resource.Replace('\\', '/');
|
|
if (!name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) || !name.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
using var stream = assembly.GetManifestResourceStream(resource)!;
|
|
using var reader = new StreamReader(stream);
|
|
documents.Add(new(name, reader.ReadToEnd()));
|
|
}
|
|
|
|
if (documents.Count == 0)
|
|
{
|
|
throw new InvalidOperationException($"No texts for {culture.Name} in {assembly.GetName().Name}.");
|
|
}
|
|
|
|
return new JsonTextProvider(culture, documents);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public string Format(string key, params object?[] args) => string.Format(Culture, this[key], args);
|
|
|
|
/// <inheritdoc />
|
|
public bool Contains(string key) => key is not null && _texts.ContainsKey(key);
|
|
|
|
private static void Flatten(JsonElement element, string prefix, string file, Dictionary<string, string> texts)
|
|
{
|
|
foreach (var property in element.EnumerateObject())
|
|
{
|
|
// "_" gives a text to the object itself: {"Menu": {"Parties": {"_": "Anagrafiche", "Customers": "Clienti"}}}.
|
|
var key = property.Name == "_" && prefix.Length > 0 ? prefix : prefix.Length == 0 ? property.Name : prefix + "." + property.Name;
|
|
switch (property.Value.ValueKind)
|
|
{
|
|
case JsonValueKind.Object:
|
|
Flatten(property.Value, key, file, texts);
|
|
break;
|
|
case JsonValueKind.String:
|
|
if (!texts.TryAdd(key, property.Value.GetString()!))
|
|
{
|
|
throw new InvalidDataException($"The text {key} is defined twice ({file}).");
|
|
}
|
|
|
|
break;
|
|
default:
|
|
throw new InvalidDataException($"The text {key} in {file} must be a string or an object.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Number, money and date formats of the interface, in the culture of <see cref="ITexts"/> (SPEC §10.8).</summary>
|
|
public static class TextFormats
|
|
{
|
|
/// <summary>An amount in euro: <c>€ 1.234,50</c>.</summary>
|
|
/// <param name="texts">The texts.</param>
|
|
/// <param name="amount">The amount.</param>
|
|
/// <returns>The text.</returns>
|
|
public static string Money(this ITexts texts, decimal amount)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(texts);
|
|
return "€ " + amount.ToString("#,##0.00", texts.Culture);
|
|
}
|
|
|
|
/// <summary>A number with a fixed count of decimals: <c>1.234,5</c>.</summary>
|
|
/// <param name="texts">The texts.</param>
|
|
/// <param name="value">The value.</param>
|
|
/// <param name="decimals">The decimals.</param>
|
|
/// <returns>The text.</returns>
|
|
public static string Number(this ITexts texts, decimal value, int decimals = 0)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(texts);
|
|
return value.ToString("N" + decimals.ToString(CultureInfo.InvariantCulture), texts.Culture);
|
|
}
|
|
|
|
/// <summary>A date: <c>13/10/2026</c>.</summary>
|
|
/// <param name="texts">The texts.</param>
|
|
/// <param name="date">The date.</param>
|
|
/// <returns>The text.</returns>
|
|
public static string Date(this ITexts texts, DateOnly date)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(texts);
|
|
return date.ToString("dd/MM/yyyy", texts.Culture);
|
|
}
|
|
|
|
/// <summary>A date and time in the given time zone: <c>13/10/2026 14:05</c>.</summary>
|
|
/// <param name="texts">The texts.</param>
|
|
/// <param name="utc">The moment, in UTC.</param>
|
|
/// <param name="zone">The time zone of the user.</param>
|
|
/// <returns>The text.</returns>
|
|
public static string DateTime(this ITexts texts, DateTime utc, TimeZoneInfo zone)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(texts);
|
|
ArgumentNullException.ThrowIfNull(zone);
|
|
var local = TimeZoneInfo.ConvertTimeFromUtc(System.DateTime.SpecifyKind(utc, DateTimeKind.Utc), zone);
|
|
return local.ToString("dd/MM/yyyy HH:mm", texts.Culture);
|
|
}
|
|
}
|