-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathApp.cs
More file actions
186 lines (172 loc) · 5.71 KB
/
App.cs
File metadata and controls
186 lines (172 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using Microsoft.UI.Reactor.Localization;
using static Microsoft.UI.Reactor.Factories;
using Microsoft.UI.Xaml;
ReactorApp.Run<LocalizationApp>("Localization", width: 650, height: 700
#if DEBUG
, preview: true
#endif
);
// <snippet:resource-provider>
class DemoResourceProvider : IStringResourceProvider
{
private readonly Dictionary<string, Dictionary<string, string>> _strings = new()
{
["en-US"] = new() {
["App.title"] = "My Application",
["App.greeting"] = "Hello, {name}!",
},
["fr-FR"] = new() {
["App.title"] = "Mon Application",
["App.greeting"] = "Bonjour, {name} !",
},
["ar-SA"] = new() {
["App.title"] = "\u062a\u0637\u0628\u064a\u0642\u064a",
["App.greeting"] = "\u0645\u0631\u062d\u0628\u0627\u060c {name}!",
}
};
public string? GetString(string locale, string ns, string key)
{
var fullKey = $"{ns}.{key}";
return _strings.TryGetValue(locale, out var s)
&& s.TryGetValue(fullKey, out var v) ? v : null;
}
}
// </snippet:resource-provider>
// <snippet:locale-provider>
class LocaleSwitcher : Component
{
public override Element Render()
{
var (localeIndex, setLocaleIndex) = UseState(0);
var locales = new[] { "en-US", "fr-FR", "ar-SA" };
var locale = locales[localeIndex];
var provider = new DemoResourceProvider();
return VStack(16,
ComboBox(["English (US)", "Fran\u00e7ais", "\u0627\u0644\u0639\u0631\u0628\u064a\u0629"],
localeIndex, setLocaleIndex),
LocaleProvider(locale,
Component<LocalizedContent>(),
resourceProvider: provider,
defaultLocale: "en-US")
).Padding(24);
}
}
// </snippet:locale-provider>
// <snippet:useintl-messages>
class LocalizedContent : Component
{
public override Element Render()
{
var intl = UseIntl();
var title = intl.Message(new MessageKey("App", "title"));
var greeting = intl.Message(
new MessageKey("App", "greeting"),
("name", "Alice"));
return VStack(12,
TextBlock(title).FontSize(24).Bold(),
TextBlock(greeting).FontSize(16),
TextBlock($"Locale: {intl.Locale}").Opacity(0.6),
TextBlock($"Direction: {intl.Direction}").Opacity(0.6)
);
}
}
// </snippet:useintl-messages>
// <snippet:format-numbers-dates>
class FormattingDemo : Component
{
public override Element Render()
{
var intl = UseIntl();
var price = intl.FormatNumber(1234.56,
new NumberFormatOptions { Style = NumberStyle.Currency });
var percent = intl.FormatNumber(0.875,
new NumberFormatOptions { Style = NumberStyle.Percent });
var date = intl.FormatDate(DateTimeOffset.Now,
new DateFormatOptions { Style = DateStyle.Long });
var items = intl.FormatList(
new[] { "Apples", "Bananas", "Cherries" },
ListFormatType.Conjunction);
return VStack(8,
SubHeading("Formatting"),
TextBlock($"Price: {price}"),
TextBlock($"Rate: {percent}"),
TextBlock($"Date: {date}"),
TextBlock($"List: {items}")
).Padding(24);
}
}
// </snippet:format-numbers-dates>
// <snippet:rtl-detection>
class RtlDemo : Component
{
public override Element Render()
{
var intl = UseIntl();
var locales = new[] { "en-US", "fr-FR", "ar-SA", "he-IL", "ja-JP" };
return VStack(8,
SubHeading("RTL Detection"),
VStack(4,
locales.Select(loc =>
HStack(8,
TextBlock(loc).Width(60),
TextBlock(RtlHelper.IsRtlLocale(loc) ? "RTL" : "LTR")
.Bold()
.Foreground(RtlHelper.IsRtlLocale(loc)
? "#d13438" : "#107c10")
)
).ToArray()
),
When(intl.IsRtl, () =>
TextBlock("Current layout is right-to-left")
.Foreground("#d13438").SemiBold())
).Padding(24);
}
}
// </snippet:rtl-detection>
// <snippet:pseudo-localization>
class PseudoLocDemo : Component
{
public override Element Render()
{
var (pseudo, setPseudo) = UseState(false);
var provider = new DemoResourceProvider();
return VStack(12,
SubHeading("Pseudo-Localization"),
ToggleSwitch(pseudo, setPseudo,
header: "Enable pseudo-localization"),
LocaleProvider("en-US",
Func(ctx =>
{
var intl = ctx.UseIntl();
var title = intl.Message(new MessageKey("App", "title"));
var greeting = intl.Message(
new MessageKey("App", "greeting"),
("name", "World"));
return VStack(4,
TextBlock(title).FontSize(18).Bold(),
TextBlock(greeting));
}),
resourceProvider: provider,
pseudoLocalize: pseudo)
).Padding(24);
}
}
// </snippet:pseudo-localization>
// Main app
class LocalizationApp : Component
{
public override Element Render()
{
return ScrollView(
VStack(24,
Heading("Localization"),
Component<LocaleSwitcher>(),
Component<FormattingDemo>(),
Component<RtlDemo>(),
Component<PseudoLocDemo>()
).Padding(24)
);
}
}