|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Globalization; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +using Fluent.Net; |
| 7 | + |
| 8 | +namespace BizHawk.Client.DiscoHawk |
| 9 | +{ |
| 10 | + public static class Sample |
| 11 | + { |
| 12 | + private static MessageContext GetMessages(string lang) |
| 13 | + { |
| 14 | + //TODO use ReflectionCache |
| 15 | + var asm = typeof(TranslationService).Assembly; |
| 16 | + var stream = asm.GetManifestResourceStream(asm.GetManifestResourceNames().First(s => s.EndsWith($"{lang}.ftl"))); |
| 17 | + |
| 18 | + MessageContext mc = new(lang, new() { UseIsolating = false }); |
| 19 | + using StreamReader sr = new(stream); |
| 20 | + var errors = mc.AddMessages(sr); |
| 21 | + foreach (var error in errors) Console.WriteLine(error); |
| 22 | + return mc; |
| 23 | + } |
| 24 | + |
| 25 | + public static void RunAll() |
| 26 | + { |
| 27 | + RunTest("en"); |
| 28 | + RunTest("it"); |
| 29 | + RunTest("pl"); |
| 30 | + } |
| 31 | + |
| 32 | + private static void RunTest(string lang) |
| 33 | + { |
| 34 | + TranslationService translator = new([ GetMessages(lang) ]); |
| 35 | + Console.WriteLine($"\n{lang}:"); |
| 36 | + Console.WriteLine($"tabs-close-button = {translator.GetString("tabs-close-button")}"); |
| 37 | + Console.WriteLine( |
| 38 | + "tabs-close-tooltip ($tabCount = 1) = " |
| 39 | + + translator.GetString("tabs-close-tooltip", TranslationService.Args("tabCount", 1))); |
| 40 | + Console.WriteLine( |
| 41 | + "tabs-close-tooltip ($tabCount = 2) = " |
| 42 | + + translator.GetString("tabs-close-tooltip", TranslationService.Args("tabCount", 2))); |
| 43 | + Console.WriteLine( |
| 44 | + "tabs-close-warning ($tabCount = 1) = " |
| 45 | + + translator.GetString("tabs-close-warning", TranslationService.Args("tabCount", 1))); |
| 46 | + Console.WriteLine( |
| 47 | + "tabs-close-warning ($tabCount = 2) = " |
| 48 | + + translator.GetString("tabs-close-warning", TranslationService.Args("tabCount", 2))); |
| 49 | + Console.WriteLine($"sync-dialog-title = {translator.GetString("sync-dialog-title")}"); |
| 50 | + Console.WriteLine($"sync-headline-title = {translator.GetString("sync-headline-title")}"); |
| 51 | + Console.WriteLine($"sync-signedout-title = {translator.GetString("sync-signedout-title")}"); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public sealed class TranslationService(IReadOnlyList<MessageContext> Contexts) |
| 56 | + { |
| 57 | + public static Dictionary<string, object> Args(string name, object value, params object[] args) |
| 58 | + { |
| 59 | + if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(paramName: nameof(name)); |
| 60 | + if (value is null) throw new ArgumentNullException(paramName: nameof(value)); |
| 61 | + if (args.Length % 2 is not 0) |
| 62 | + { |
| 63 | + throw new ArgumentException( |
| 64 | + paramName: nameof(args), |
| 65 | + message: "Expected a comma separated list " + |
| 66 | + "of name, value arguments but the number of arguments is " + |
| 67 | + "not a multiple of two"); |
| 68 | + } |
| 69 | + |
| 70 | + Dictionary<string, object> argsDic = new() { [name] = value }; |
| 71 | + for (int i = 0; i < args.Length; i += 2) |
| 72 | + { |
| 73 | + if (args[i] is not string name1 || string.IsNullOrEmpty(name1)) |
| 74 | + { |
| 75 | + throw new ArgumentException( |
| 76 | + paramName: nameof(args), |
| 77 | + message: $"Expected the argument at index {i} to be a non-empty string"); |
| 78 | + } |
| 79 | + var value1 = args[i + 1]; |
| 80 | + if (value1 is null) |
| 81 | + { |
| 82 | + throw new ArgumentNullException( |
| 83 | + paramName: nameof(args), |
| 84 | + message: $"Expected the argument at index {i + 1} to be a non-null value"); |
| 85 | + } |
| 86 | + argsDic.Add(name1, value1); |
| 87 | + } |
| 88 | + return argsDic; |
| 89 | + } |
| 90 | + |
| 91 | + public string GetString( |
| 92 | + string id, |
| 93 | + IDictionary<string, object> args = null, |
| 94 | + ICollection<FluentError> errors = null) |
| 95 | + { |
| 96 | + foreach (var context in Contexts) |
| 97 | + { |
| 98 | + var msg = context.GetMessage(id); |
| 99 | + if (msg is not null) return context.Format(msg, args, errors); |
| 100 | + } |
| 101 | + return string.Empty; |
| 102 | + } |
| 103 | + |
| 104 | + public string PreferredLocale |
| 105 | + => Contexts[0].Locales.First(); |
| 106 | + |
| 107 | + private CultureInfo _culture; |
| 108 | + |
| 109 | + public CultureInfo Culture |
| 110 | + => _culture ??= new(PreferredLocale); |
| 111 | + } |
| 112 | +} |
0 commit comments