Skip to content

Commit ac27453

Browse files
committed
Add Fluent.Net sample to DiscoHawk
1 parent 0dc08f7 commit ac27453

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PackageVersion Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
44
<PackageVersion Include="Cyotek.Drawing.BitmapFont" Version="2.0.4" />
55
<PackageVersion Include="DotNetAnalyzers.DocumentationAnalyzers" Version="1.0.0-beta.59" />
6+
<PackageVersion Include="Fluent.Net" Version="1.0.63" />
67
<PackageVersion Include="Google.FlatBuffers" Version="23.5.26" /> <!-- last version with .NET Standard 2.0 support -->
78
<PackageVersion Include="ImGui.NET" Version="1.90.6.1" />
89
<PackageVersion Include="JunitXml.TestLogger" Version="3.1.12" />

src/BizHawk.Client.DiscoHawk/BizHawk.Client.DiscoHawk.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
<UseWindowsForms>true</UseWindowsForms>
1111
</PropertyGroup>
1212
<ItemGroup>
13+
<PackageReference Include="Fluent.Net" PrivateAssets="all" />
1314
<ProjectReference Include="$(ProjectDir)../BizHawk.Emulation.DiscSystem/BizHawk.Emulation.DiscSystem.csproj" />
1415
<Content Include="discohawk.ico" />
1516
<EmbeddedResource Include="discohawk.ico" />
17+
<EmbeddedResource Include="locale/*.ftl" />
1618
</ItemGroup>
1719
<ItemGroup>
1820
<Compile Update="About.cs" SubType="Form" />

src/BizHawk.Client.DiscoHawk/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ private static void Main(string[] args)
114114
}
115115
}
116116

117+
Sample.RunAll(); // testing Fluent.Net
118+
117119
// Do something for visuals, I guess
118120
Application.EnableVisualStyles();
119121
Application.SetCompatibleTextRenderingDefault(false);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Closing tabs
2+
3+
tabs-close-button = Close
4+
tabs-close-tooltip = {$tabCount ->
5+
[one] Close {$tabCount} tab
6+
*[other] Close {$tabCount} tabs
7+
}
8+
tabs-close-warning =
9+
You are about to close {$tabCount} tabs.
10+
Are you sure you want to continue?
11+
12+
## Syncing
13+
14+
-sync-brand-name = Firefox Account
15+
16+
sync-dialog-title = {-sync-brand-name}
17+
sync-headline-title =
18+
{-sync-brand-name}: The best way to bring
19+
your data always with you
20+
sync-signedout-title =
21+
Connect with your {-sync-brand-name}
22+
23+
## Datetime
24+
date-is = The date is 'DATETIME($dt, weekday: "short", month: "short", year: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric")'.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Closing tabs
2+
3+
tabs-close-button = Chiudi
4+
tabs-close-tooltip = {$tabCount ->
5+
[one] Chiudi {$tabCount} scheda
6+
*[other] Chiudi {$tabCount} schede
7+
}
8+
tabs-close-warning =
9+
Verranno chiuse {$tabCount} schede. Proseguire?
10+
11+
## Syncing
12+
13+
-sync-brand-name = {$first ->
14+
*[uppercase] Account Firefox
15+
[lowercase] account Firefox
16+
}
17+
18+
sync-dialog-title = {-sync-brand-name}
19+
sync-headline-title =
20+
{-sync-brand-name}: il modo migliore
21+
per avere i tuoi dati sempre con te
22+
sync-signedout-title =
23+
Connetti il tuo {-sync-brand-name[lowercase]}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Closing tabs
2+
3+
tabs-close-button = Zamknij
4+
tabs-close-tooltip = {$tabCount ->
5+
[one] Zamknij kartę
6+
[few] Zamknij {$tabCount} karty
7+
*[many] Zamknij { $tabCount } kart
8+
}
9+
tabs-close-warning = {$tabCount ->
10+
[few] Zostaną zamknięte {$tabCount} karty.
11+
Czy chcesz kontynuować?
12+
*[many] Zostanie zamkniętych {$tabCount} kart.
13+
Czy chcesz kontynuować?
14+
}
15+
16+
## Syncing
17+
18+
-sync-brand-name = {$case ->
19+
*[nominative] Konto Firefox
20+
[genitive] Konta Firefox
21+
[accusative] Kontem Firefox
22+
}
23+
24+
sync-dialog-title = {-sync-brand-name}
25+
sync-headline-title =
26+
{-sync-brand-name}: Najlepszy sposób na to,
27+
aby mieć swoje dane zawsze przy sobie
28+
sync-signedout-title =
29+
Zaloguj do {-sync-brand-name[genitive]}

0 commit comments

Comments
 (0)