@@ -137,3 +137,56 @@ public sealed partial class MainPage : Page
137137 public ILocalizedValues Lang => global :: dotnetCampus .SampleUnoApp .Localizations .LocalizedText .Current ;
138138}
139139```
140+
141+ ## Advanced Usage
142+
143+ If you want to add real-time language switching support, you can modify the ` LocalizedText ` class as follows:
144+
145+ ``` csharp
146+ [LocalizedConfiguration (Default = " en-US" , SupportsNotification = true )]
147+ public static partial class LocalizedText
148+ {
149+ public static AppBuilder UseCompiledLang (this AppBuilder appBuilder )
150+ {
151+ if (OperatingSystem .IsWindows ())
152+ {
153+ SystemEvents .UserPreferenceChanged += SystemEvents_UserPreferenceChanged ;
154+ }
155+ return appBuilder ;
156+ }
157+
158+ [SupportedOSPlatform (" windows" )]
159+ private static void SystemEvents_UserPreferenceChanged (object sender , UserPreferenceChangedEventArgs e )
160+ {
161+ if (e .Category is UserPreferenceCategory .Locale )
162+ {
163+ // Retrieve the current language settings from the registry.
164+ //
165+ // Compared to CultureInfo.CurrentUICulture.Name or Win32 API's GetUserDefaultUILanguage, the registry can get updated standard language tags,
166+ // and supports user-defined language preferences without needing to log off.
167+ // Note: Even restarting the application will get the old settings; only logging off the system will get the new ones.
168+ var languageNames = RegistryKey .OpenBaseKey (RegistryHive .CurrentUser , RegistryView .Registry64 )
169+ .OpenSubKey (@" Control Panel\International\User Profile" , false )?
170+ .GetValue (" Languages" , null ) as IReadOnlyList <string >;
171+ if (languageNames ? [0 ] is { } name )
172+ {
173+ Dispatcher .UIThread .InvokeAsync (async () =>
174+ {
175+ await SetCurrent (name );
176+ }, DispatcherPriority .Background );
177+ }
178+ }
179+ }
180+ }
181+ ```
182+
183+ Then, you can use the ` UseCompiledLang ` method in your ` App.xaml.cs ` file:
184+
185+ ``` csharp
186+ public static AppBuilder BuildAvaloniaApp ()
187+ => AppBuilder .Configure <App >()
188+ .UsePlatformDetect ()
189+ .UseCompiledLang ()
190+ .XxxOthers ()
191+ ;
192+ ```
0 commit comments