chore: bootstrap repository foundation
Solution layout, build rules with zero-package guard, in-house test framework, localization runtime, brand mark rasteriser with ICO/PNG generation, Core contracts (imaging, video, domain model, TIFF reader), Titano.Tools (make-icon, i18n, xaml-lint, package-guard) and docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
109 files changed
+7523
No files matched your search
@@ -0,0 +1,39 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Titano.Core.Localization;
|
||||
|
||||
/// <summary>Languages shipped with Titano (BRIEF §16). English is the source.</summary>
|
||||
public static class Languages
|
||||
{
|
||||
public static readonly IReadOnlyList<string> Shipped =
|
||||
[
|
||||
"en", "it", "de", "fr", "es", "pt-BR", "pt-PT", "nl", "pl", "cs", "sk", "hu", "ro", "sv", "da", "nb",
|
||||
"fi", "ru", "uk", "tr", "el", "bg", "hr", "sl", "lt", "lv", "et", "ja", "zh-Hans", "zh-Hant", "ko",
|
||||
"ar", "he", "hi", "id", "ms", "vi", "th",
|
||||
];
|
||||
|
||||
/// <summary>Languages whose UI layout is mirrored.</summary>
|
||||
public static readonly IReadOnlySet<string> RightToLeft = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "ar", "he", "fa", "ur" };
|
||||
|
||||
/// <summary>The shipped language that best matches <paramref name="culture"/>, falling back to English.</summary>
|
||||
public static CultureInfo BestMatch(CultureInfo culture)
|
||||
{
|
||||
for (var c = culture; !Equals(c, CultureInfo.InvariantCulture); c = c.Parent)
|
||||
{
|
||||
var hit = Shipped.FirstOrDefault(s => s.Equals(c.Name, StringComparison.OrdinalIgnoreCase));
|
||||
if (hit is not null) return CultureInfo.GetCultureInfo(hit);
|
||||
}
|
||||
if (culture.TwoLetterISOLanguageName == "pt") return CultureInfo.GetCultureInfo("pt-PT");
|
||||
if (culture.TwoLetterISOLanguageName == "zh") return CultureInfo.GetCultureInfo("zh-Hans");
|
||||
if (culture.TwoLetterISOLanguageName is "no" or "nn") return CultureInfo.GetCultureInfo("nb");
|
||||
return CultureInfo.GetCultureInfo("en");
|
||||
}
|
||||
|
||||
/// <summary>Native display name ("Italiano", "日本語").</summary>
|
||||
public static string NativeName(string code)
|
||||
{
|
||||
var c = CultureInfo.GetCultureInfo(code);
|
||||
var name = c.NativeName;
|
||||
return name.Length > 0 ? char.ToUpper(name[0], c) + name[1..] : code;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Globalization;
|
||||
using System.Resources;
|
||||
|
||||
namespace Titano.Core.Localization;
|
||||
|
||||
/// <summary>
|
||||
/// Runtime string catalog. Keys are stable identifiers of the form <c>Area.Group.Name</c>; the first
|
||||
/// segment selects the resource file <c>i18n/<culture>/<Area>.resx</c>. English is the source.
|
||||
/// </summary>
|
||||
public static class Loc
|
||||
{
|
||||
private const string BaseNamePrefix = "Titano.Core.i18n.";
|
||||
private static readonly ConcurrentDictionary<string, ResourceManager> Managers = new(StringComparer.Ordinal);
|
||||
private static CultureInfo _culture = CultureInfo.CurrentUICulture;
|
||||
|
||||
/// <summary>Raised after <see cref="SetCulture"/> so bound UI can refresh without restart.</summary>
|
||||
public static event EventHandler? CultureChanged;
|
||||
|
||||
/// <summary>Raised once per missing key (used by tests and diagnostics).</summary>
|
||||
public static event Action<string>? MissingKey;
|
||||
|
||||
public static CultureInfo Culture => _culture;
|
||||
|
||||
/// <summary>Text direction of the current culture.</summary>
|
||||
public static bool IsRightToLeft => _culture.TextInfo.IsRightToLeft;
|
||||
|
||||
public static void SetCulture(CultureInfo culture)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(culture);
|
||||
if (Equals(culture, _culture)) return;
|
||||
_culture = culture;
|
||||
CultureChanged?.Invoke(null, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <summary>Returns the localized string for <paramref name="key"/> in the current culture.</summary>
|
||||
public static string L(string key) => Get(key, _culture) ?? Missing(key);
|
||||
|
||||
/// <summary>Returns the localized composite format for <paramref name="key"/> filled with <paramref name="args"/>.</summary>
|
||||
public static string L(string key, params object?[] args)
|
||||
{
|
||||
var format = Get(key, _culture);
|
||||
if (format is null) return Missing(key);
|
||||
return args.Length == 0 ? format : string.Format(_culture, format, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Plural-aware lookup: resolves <c>key.one</c>, <c>key.few</c>, <c>key.many</c> or <c>key.other</c>
|
||||
/// using CLDR rules of the current language; <c>{0}</c> receives the count.
|
||||
/// </summary>
|
||||
public static string Plural(string key, long count, params object?[] args)
|
||||
{
|
||||
var category = PluralRules.Select(_culture, count);
|
||||
var format = Get(key + "." + PluralRules.Suffix(category), _culture) ?? Get(key + ".other", _culture);
|
||||
if (format is null) return Missing(key);
|
||||
var all = new object?[args.Length + 1];
|
||||
all[0] = count;
|
||||
Array.Copy(args, 0, all, 1, args.Length);
|
||||
return string.Format(_culture, format, all);
|
||||
}
|
||||
|
||||
/// <summary>Looks a key up in a specific culture (falls back to parents and English); null when absent.</summary>
|
||||
public static string? Get(string key, CultureInfo culture)
|
||||
{
|
||||
var dot = key.IndexOf('.', StringComparison.Ordinal);
|
||||
if (dot <= 0) return null;
|
||||
var area = key[..dot];
|
||||
var manager = Managers.GetOrAdd(area, static a => new ResourceManager(BaseNamePrefix + a, typeof(Loc).Assembly));
|
||||
try
|
||||
{
|
||||
return manager.GetString(key, culture);
|
||||
}
|
||||
catch (MissingManifestResourceException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Every key and English value of an area, for tooling and the web admin export.</summary>
|
||||
public static IReadOnlyDictionary<string, string> GetArea(string area, CultureInfo culture)
|
||||
{
|
||||
var manager = Managers.GetOrAdd(area, static a => new ResourceManager(BaseNamePrefix + a, typeof(Loc).Assembly));
|
||||
var result = new Dictionary<string, string>(StringComparer.Ordinal);
|
||||
// Walk from invariant to specific so the most specific value wins.
|
||||
var chain = new List<CultureInfo>();
|
||||
for (var c = culture; !Equals(c, CultureInfo.InvariantCulture); c = c.Parent) chain.Add(c);
|
||||
chain.Add(CultureInfo.InvariantCulture);
|
||||
chain.Reverse();
|
||||
foreach (var c in chain)
|
||||
{
|
||||
ResourceSet? set;
|
||||
try
|
||||
{
|
||||
set = manager.GetResourceSet(c, createIfNotExists: true, tryParents: false);
|
||||
}
|
||||
catch (MissingManifestResourceException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (set is null) continue;
|
||||
foreach (System.Collections.DictionaryEntry entry in set)
|
||||
{
|
||||
if (entry.Key is string k && entry.Value is string v) result[k] = v;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string Missing(string key)
|
||||
{
|
||||
MissingKey?.Invoke(key);
|
||||
return "⟦" + key + "⟧";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Titano.Core.Localization;
|
||||
|
||||
public enum PluralCategory
|
||||
{
|
||||
One,
|
||||
Few,
|
||||
Many,
|
||||
Other,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Minimal CLDR cardinal plural rules for the shipped languages (integers only, which is all the UI needs).
|
||||
/// </summary>
|
||||
public static class PluralRules
|
||||
{
|
||||
public static string Suffix(PluralCategory c) => c switch
|
||||
{
|
||||
PluralCategory.One => "one",
|
||||
PluralCategory.Few => "few",
|
||||
PluralCategory.Many => "many",
|
||||
_ => "other",
|
||||
};
|
||||
|
||||
public static PluralCategory Select(CultureInfo culture, long count)
|
||||
{
|
||||
var n = Math.Abs(count);
|
||||
var lang = culture.TwoLetterISOLanguageName;
|
||||
long mod10 = n % 10, mod100 = n % 100;
|
||||
switch (lang)
|
||||
{
|
||||
// No plural distinction.
|
||||
case "ja" or "zh" or "ko" or "vi" or "th" or "id" or "ms":
|
||||
return PluralCategory.Other;
|
||||
|
||||
// one: 0 and 1.
|
||||
case "fr" or "hi":
|
||||
return n is 0 or 1 ? PluralCategory.One : PluralCategory.Other;
|
||||
|
||||
case "pt" when culture.Name.Equals("pt-BR", StringComparison.OrdinalIgnoreCase):
|
||||
return n is 0 or 1 ? PluralCategory.One : PluralCategory.Other;
|
||||
|
||||
// East Slavic.
|
||||
case "ru" or "uk":
|
||||
if (mod10 == 1 && mod100 != 11) return PluralCategory.One;
|
||||
if (mod10 is >= 2 and <= 4 && mod100 is < 12 or > 14) return PluralCategory.Few;
|
||||
return PluralCategory.Many;
|
||||
|
||||
case "pl":
|
||||
if (n == 1) return PluralCategory.One;
|
||||
if (mod10 is >= 2 and <= 4 && mod100 is < 12 or > 14) return PluralCategory.Few;
|
||||
return PluralCategory.Many;
|
||||
|
||||
case "cs" or "sk":
|
||||
if (n == 1) return PluralCategory.One;
|
||||
if (n is >= 2 and <= 4) return PluralCategory.Few;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "hr" or "sr" or "bs":
|
||||
if (mod10 == 1 && mod100 != 11) return PluralCategory.One;
|
||||
if (mod10 is >= 2 and <= 4 && mod100 is < 12 or > 14) return PluralCategory.Few;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "sl":
|
||||
if (mod100 == 1) return PluralCategory.One;
|
||||
if (mod100 == 2) return PluralCategory.Few; // CLDR "two", folded into few
|
||||
if (mod100 is 3 or 4) return PluralCategory.Few;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "lt":
|
||||
if (mod10 == 1 && mod100 is < 11 or > 19) return PluralCategory.One;
|
||||
if (mod10 is >= 2 and <= 9 && mod100 is < 11 or > 19) return PluralCategory.Few;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "lv":
|
||||
if (mod10 == 0 || mod100 is >= 11 and <= 19) return PluralCategory.Many; // CLDR "zero"
|
||||
if (mod10 == 1 && mod100 != 11) return PluralCategory.One;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "ro":
|
||||
if (n == 1) return PluralCategory.One;
|
||||
if (n == 0 || mod100 is >= 2 and <= 19) return PluralCategory.Few;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "ar":
|
||||
if (n == 1) return PluralCategory.One;
|
||||
if (n == 2) return PluralCategory.Few; // CLDR "two", folded
|
||||
if (mod100 is >= 3 and <= 10) return PluralCategory.Few;
|
||||
if (mod100 is >= 11 and <= 99) return PluralCategory.Many;
|
||||
return PluralCategory.Other;
|
||||
|
||||
case "he":
|
||||
if (n == 1) return PluralCategory.One;
|
||||
if (n == 2) return PluralCategory.Few; // CLDR "two", folded
|
||||
return PluralCategory.Other;
|
||||
|
||||
// Default Germanic/Romance rule: one for exactly 1.
|
||||
default:
|
||||
return n == 1 ? PluralCategory.One : PluralCategory.Other;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user