Findings, advisor reasons and a few UI texts put a count before a plural noun without plural forms, so English and Italian read "1 frames are black". Each such key is now a plural stem (.one/.other) whose count is its first argument; findings and reasons, which travel as data, resolve through the new Loc.Text, which picks the form from that argument (a fraction takes "other"), so stored deep reports and project reasons keep their keys and arguments. Texts with two or more counts (job log lines, "frames with a capture time: 12 of 300") read as labels, which agree with no number. A single exposure change gets its own finding instead of "1 exposure changes, typically 0 frames apart". The converted and reworded keys leave the 36 other catalogs until they are translated again; the dead plain Shell.JobBar.Ready goes too. `i18n check` now fails on an English text that counts a noun without plural forms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
176 lines
9.4 KiB
C#
176 lines
9.4 KiB
C#
using System.Collections;
|
|
using System.Globalization;
|
|
using System.Reflection;
|
|
using System.Resources;
|
|
using System.Text.RegularExpressions;
|
|
using Titano.Core.Advice;
|
|
using Titano.Core.Analysis;
|
|
using Titano.Core.Analysis.Deep;
|
|
using Titano.Core.Diagnostics.Synthetic;
|
|
using Titano.Core.Localization;
|
|
using Titano.Core.Project;
|
|
using Titano.Core.Tests.Analysis.Deep;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Advice;
|
|
|
|
|
|
public sealed partial class AdvisorLocalizationTests(TestContext context)
|
|
{
|
|
private static readonly CultureInfo English = CultureInfo.GetCultureInfo("en");
|
|
private static readonly CultureInfo Italian = CultureInfo.GetCultureInfo("it");
|
|
private const string LogPrefix = "Advisor.Log.";
|
|
|
|
[Test]
|
|
public void EveryAdvisorKeyHasEnglishAndItalianTextWithTheSamePlaceholders()
|
|
{
|
|
var manager = new ResourceManager("Titano.Core.i18n.Advisor", typeof(Loc).Assembly);
|
|
var english = Entries(manager.GetResourceSet(CultureInfo.InvariantCulture, createIfNotExists: true, tryParents: false)!);
|
|
var italian = Entries(manager.GetResourceSet(Italian, createIfNotExists: true, tryParents: false)!);
|
|
// Job log lines (Advisor.Log.*) are written by the analysis job handlers in Titano.Data, not by the advisor itself.
|
|
var keys = Keys(typeof(AdvisorKeys)).Concat(Keys(typeof(FindingKeys))).Concat(Keys(typeof(DeepFindingKeys)))
|
|
.Concat(english.Keys.Where(k => k.StartsWith(LogPrefix, StringComparison.Ordinal))).ToList();
|
|
Assert.Greater(keys.Count, 90);
|
|
var forms = 0;
|
|
foreach (var key in keys)
|
|
{
|
|
// A key that counts something is a plural stem (BRIEF §16) whose count is its first argument.
|
|
string[] names = english.ContainsKey(key) ? [key] : [key + ".one", key + ".other"];
|
|
if (names.Length == 2) Assert.Contains("0", Placeholders(english.GetValueOrDefault(key + ".other") ?? string.Empty), $"{key}.other counts with {{0}}");
|
|
foreach (var name in names)
|
|
{
|
|
Assert.True(english.TryGetValue(name, out var en), $"English text missing for {name}");
|
|
Assert.True(italian.TryGetValue(name, out var it), $"Italian text missing for {name}");
|
|
Assert.SequenceEqual(Placeholders(en!), Placeholders(it!), name);
|
|
Assert.LessOrEqual(en!.Length, 200);
|
|
forms++;
|
|
}
|
|
}
|
|
Assert.Equal(english.Count, forms);
|
|
}
|
|
|
|
[Test]
|
|
public void ACountOfOneReadsInTheSingularInBothLanguages()
|
|
{
|
|
Assert.Equal("1 frame is black (lens cap or missed exposure).", Loc.Text(DeepFindingKeys.BlackFrames, [1.0], English));
|
|
Assert.Equal("3 frames are black (lens cap or missed exposure).", Loc.Text(DeepFindingKeys.BlackFrames, [3.0], English));
|
|
Assert.Equal("1 fotogramma è nero (tappo sull'obiettivo o scatto mancato).", Loc.Text(DeepFindingKeys.BlackFrames, [1.0], Italian));
|
|
Assert.Equal("The lens changes once.", Loc.Text(FindingKeys.LensChanged, [1], English));
|
|
Assert.Equal("L'obiettivo cambia 2 volte.", Loc.Text(FindingKeys.LensChanged, [2], Italian));
|
|
Assert.Equal("2 exposure changes reached the pixels: each is spread over a smooth transition.", Loc.Text(AdvisorKeys.HolyGrailReceived, [2], English));
|
|
// Advisor windows are measured in tenths of a frame: a fraction takes the plural.
|
|
Assert.Equal("Flicker stays correlated over 1.5 frames: a 7-frame window spans four times that.", Loc.Text(AdvisorKeys.WindowCorrelation, [1.5, 7], English));
|
|
Assert.Equal("Flicker stays correlated over 1 frame: a 5-frame window spans four times that.", Loc.Text(AdvisorKeys.WindowCorrelation, [1.0, 5], English));
|
|
// Keys without a count read as before.
|
|
Assert.Equal("Clouds cover about 42% of the sky on average.", Loc.Text(DeepFindingKeys.Clouds, [42.0], English));
|
|
}
|
|
|
|
[Test]
|
|
public void ASingleExposureChangeHasNoSpacing()
|
|
{
|
|
var frames = Enumerable.Range(0, 20).Select(i => new LightFrame
|
|
{
|
|
Index = i,
|
|
CapturedAt = new DateTime(2026, 6, 1, 20, 0, 0, DateTimeKind.Utc).AddSeconds(10 * i),
|
|
ExposureTime = i < 12 ? 1.0 / 250 : 1.0 / 125,
|
|
FNumber = 8,
|
|
Iso = 100,
|
|
}).ToList();
|
|
var single = LightAnalyzer.Analyze(frames).Findings.Single(f => f.Key == FindingKeys.ExposureStep);
|
|
Assert.SequenceEqual([12], single.Frames);
|
|
Assert.Equal("One exposure change of -1.0 EV.", Loc.Text(single.Key, single.Arguments, English));
|
|
Assert.Equal("Un solo cambio di esposizione di -1,0 EV.", Loc.Text(single.Key, single.Arguments, Italian));
|
|
Assert.DoesNotContain(FindingKeys.ExposureSteps, LightAnalyzer.Analyze(frames).Findings.Select(f => f.Key));
|
|
}
|
|
|
|
[Test]
|
|
public async Task EveryReasonAndFindingOfTheGoldenScenesFormatsInBothLanguages()
|
|
{
|
|
var scenes = new List<(SyntheticFrameSource Source, DeepAnalysisReport? Deep)>
|
|
{
|
|
(new SyntheticFrameSource(DeepScenes.NightSky()), await DeepReports.Night),
|
|
(new SyntheticFrameSource(DeepScenes.NightSky(jitter: 2), 2), await DeepReports.NightJitter),
|
|
(new SyntheticFrameSource(DeepScenes.HolyGrail()), await DeepReports.HolyGrail),
|
|
(new SyntheticFrameSource(DeepScenes.HolyGrail()) { StripExposure = true }, null),
|
|
};
|
|
var rendered = 0;
|
|
foreach (var (source, deep) in scenes)
|
|
{
|
|
var frames = Enumerable.Range(0, source.Count).Select(source.Metadata).ToList();
|
|
var light = LightAnalyzer.Analyze(frames);
|
|
var sequence = new SequenceContext { FrameCount = frames.Count, SourceWidth = 6000, SourceHeight = 4000, MedianExposureSeconds = 1.0 / 250 };
|
|
var result = Advisor.Recommend(light, deep, sequence);
|
|
foreach (var r in result.Recommendations) rendered += Render(r.ReasonKey, [.. r.ReasonArgs]);
|
|
foreach (var f in result.Findings) rendered += Render(f.Key, f.Arguments);
|
|
foreach (var p in SequencePresets.Generate(light, deep, sequence))
|
|
{
|
|
rendered += Render(p.NameKey, [.. p.NameArgs]);
|
|
rendered += Render(p.ReasonKey!, [.. p.ReasonArgs]);
|
|
}
|
|
// Stored reasons (culture-neutral strings) render with the current culture's numbers.
|
|
var applied = Advisor.Apply(new ProjectSettings(), result);
|
|
Assert.DoesNotContain('⟦', Advisor.Explain(applied.Exposure.HolyGrail.ReasonKey!, applied.Exposure.HolyGrail.ReasonArgs));
|
|
}
|
|
context.Log($"{rendered} texts rendered");
|
|
Assert.Greater(rendered, 80);
|
|
}
|
|
|
|
[Test]
|
|
public void StoredNumericArgumentsAreLocalisedWhenExplained()
|
|
{
|
|
var text = Format(Loc.Get(AdvisorKeys.DeflickerOn, Italian)!, Italian, 0.125);
|
|
Assert.Contains("0,125", text);
|
|
Assert.Contains("0.125", Format(Loc.Get(AdvisorKeys.DeflickerOn, English)!, English, 0.125));
|
|
}
|
|
|
|
[Test]
|
|
public async Task PresetsRoundTripAsJsonAndSaveAsUserPresets()
|
|
{
|
|
var source = new SyntheticFrameSource(DeepScenes.NightSky(jitter: 2), 2);
|
|
var frames = Enumerable.Range(0, source.Count).Select(source.Metadata).ToList();
|
|
var presets = SequencePresets.Generate(LightAnalyzer.Analyze(frames), await DeepReports.NightJitter, new SequenceContext { SourceWidth = 6000, SourceHeight = 4000 });
|
|
foreach (var preset in presets)
|
|
{
|
|
var json = preset.ToJson();
|
|
Assert.Equal(json, SequencePreset.FromJson(json).ToJson());
|
|
}
|
|
var user = presets[0].AsUserPreset(" My night ");
|
|
Assert.True(user.IsUser);
|
|
Assert.Equal("My night", user.Name);
|
|
Assert.NotEqual(presets[0].Id, user.Id);
|
|
var restored = SequencePreset.FromJson(user.ToJson());
|
|
Assert.Equal("My night", restored.DisplayName());
|
|
Assert.Equal(presets[0].Settings.ToJson(), restored.Settings.ToJson());
|
|
Assert.Equal(presets[0].ExportPresetId, restored.ExportPresetId);
|
|
}
|
|
|
|
private static int Render(string key, object?[] args)
|
|
{
|
|
foreach (var culture in new[] { English, Italian })
|
|
{
|
|
var text = Loc.Text(key, args, culture);
|
|
Assert.False(text.Contains('⟦'), $"{key} has no {culture.Name} text");
|
|
Assert.False(text.Contains('{'), $"{key} left a placeholder: {text}");
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
private static string Format(string format, CultureInfo culture, params object?[] args) => string.Format(culture, format, args);
|
|
|
|
private static Dictionary<string, string> Entries(ResourceSet set)
|
|
{
|
|
var entries = new Dictionary<string, string>(StringComparer.Ordinal);
|
|
foreach (DictionaryEntry entry in set) entries[(string)entry.Key] = (string)entry.Value!;
|
|
return entries;
|
|
}
|
|
|
|
private static IEnumerable<string> Keys(Type type) =>
|
|
type.GetFields(BindingFlags.Public | BindingFlags.Static).Where(f => f.IsLiteral && f.FieldType == typeof(string) && f.Name != nameof(AdvisorKeys.MissingPrefix)).Select(f => (string)f.GetRawConstantValue()!)
|
|
.Concat(type.GetProperties(BindingFlags.Public | BindingFlags.Static).Where(p => p.PropertyType == typeof(string)).Select(p => (string)p.GetValue(null)!));
|
|
|
|
private static IEnumerable<string> Placeholders(string text) => PlaceholderRegex().Matches(text).Select(m => m.Groups[1].Value).Order();
|
|
|
|
[GeneratedRegex(@"\{(\d+)(?:[,:][^}]*)?\}")]
|
|
private static partial Regex PlaceholderRegex();
|
|
}
|