|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Globalization; |
| 5 | +using System.IO; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +using Fluent.Net; |
| 9 | + |
| 10 | +namespace BizHawk.Client.DiscoHawk |
| 11 | +{ |
| 12 | + public sealed class ArgsDict : Dictionary<string, object?> |
| 13 | + { |
| 14 | + public ArgsDict(int? tabCount = null) |
| 15 | + { |
| 16 | + Add(nameof(tabCount), tabCount); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public readonly struct MultiMessageContext |
| 21 | + { |
| 22 | + private static IReadOnlyList<MessageContext> ReadEmbeddedAndConcat(string lang, MessageContext[] overlays) |
| 23 | + { |
| 24 | + MessageContext mc = new(lang, new() { UseIsolating = false }); |
| 25 | + Stream embeddedStream; |
| 26 | + try |
| 27 | + { |
| 28 | + embeddedStream = ReflectionCache.EmbeddedResourceStream($"locale.{lang}.ftl"); |
| 29 | + } |
| 30 | + catch (ArgumentException e) |
| 31 | + { |
| 32 | + Console.WriteLine(e); |
| 33 | + return overlays; |
| 34 | + } |
| 35 | + using StreamReader sr = new(embeddedStream); |
| 36 | + var errors = mc.AddMessages(sr); |
| 37 | + foreach (var error in errors) Console.WriteLine(error); |
| 38 | + return [ ..overlays, mc ]; |
| 39 | + } |
| 40 | + |
| 41 | + private readonly IReadOnlyList<MessageContext> _contexts; |
| 42 | + |
| 43 | + public readonly CultureInfo Culture; |
| 44 | + |
| 45 | + public MultiMessageContext(IReadOnlyList<MessageContext> contexts) |
| 46 | + { |
| 47 | + _contexts = contexts; |
| 48 | + Culture = new(_contexts.FirstOrDefault()?.Locales?.First()); |
| 49 | + } |
| 50 | + |
| 51 | + public MultiMessageContext(string lang, params MessageContext[] overlays) |
| 52 | + : this(ReadEmbeddedAndConcat(lang, overlays)) {} |
| 53 | + |
| 54 | + public string? this[string id] |
| 55 | + => GetString(id); |
| 56 | + |
| 57 | + public string? GetString( |
| 58 | + string id, |
| 59 | + IDictionary<string, object?>? args = null, |
| 60 | + ICollection<FluentError>? errors = null) |
| 61 | + { |
| 62 | + foreach (var context in _contexts) |
| 63 | + { |
| 64 | + var msg = context.GetMessage(id); |
| 65 | + if (msg is not null) return context.Format(msg, args, errors); |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class Sample |
| 72 | + { |
| 73 | + public static void RunAll() |
| 74 | + { |
| 75 | + static void RunTest(string lang) |
| 76 | + { |
| 77 | + MultiMessageContext translator = new(lang); |
| 78 | + Console.WriteLine($"\n{lang}:"); |
| 79 | + Console.WriteLine($"tabs-close-button = {translator.GetString("tabs-close-button")}"); |
| 80 | + Console.WriteLine( |
| 81 | + "tabs-close-tooltip ($tabCount = 1) = " |
| 82 | + + translator.GetString("tabs-close-tooltip", new ArgsDict(tabCount: 1))); |
| 83 | + Console.WriteLine( |
| 84 | + "tabs-close-tooltip ($tabCount = 2) = " |
| 85 | + + translator.GetString("tabs-close-tooltip", new ArgsDict(tabCount: 2))); |
| 86 | + Console.WriteLine( |
| 87 | + "tabs-close-warning ($tabCount = 1) = " |
| 88 | + + translator.GetString("tabs-close-warning", new ArgsDict(tabCount: 1))); |
| 89 | + Console.WriteLine( |
| 90 | + "tabs-close-warning ($tabCount = 2) = " |
| 91 | + + translator.GetString("tabs-close-warning", new ArgsDict(tabCount: 2))); |
| 92 | + Console.WriteLine($"sync-dialog-title = {translator.GetString("sync-dialog-title")}"); |
| 93 | + Console.WriteLine($"sync-headline-title = {translator.GetString("sync-headline-title")}"); |
| 94 | + Console.WriteLine($"sync-signedout-title = {translator.GetString("sync-signedout-title")}"); |
| 95 | + } |
| 96 | + RunTest("en"); |
| 97 | + RunTest("it"); |
| 98 | + RunTest("pl"); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments