Skip to content

Commit d27fa17

Browse files
authored
Merge pull request #21 from dotnet-campus/t/lvyi/set
Fix an fallback issue that IT NEVER WORKED
2 parents 145c41b + a2ab45c commit d27fa17

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
```

src/dotnetCampus.Localizations.Analyzer/Assets/Templates/ImmutableLocalization.g.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ private static ILocalizedStringProvider GetOrCreateLocalizedStringProvider(strin
118118
return provider;
119119
}
120120
var fallbackTag = global::dotnetCampus.Localizations.Helpers.LocalizationHelper.MatchWithFallback(languageTag, SupportedLanguageTags);
121+
provider = fallbackTag is null ? null : CreateLocalizedStringProviderCore(fallbackTag);
121122
if (provider is not null)
122123
{
123124
return provider;

src/dotnetCampus.Localizations.Analyzer/Assets/Templates/NotifiableLocalization.g.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ private static ILocalizedStringProvider CreateLocalizedStringProvider(string lan
9191
return provider;
9292
}
9393
var fallbackTag = global::dotnetCampus.Localizations.Helpers.LocalizationHelper.MatchWithFallback(languageTag, SupportedLanguageTags);
94+
provider = fallbackTag is null ? null : CreateLocalizedStringProviderCore(fallbackTag);
9495
if (provider is not null)
9596
{
9697
return provider;

0 commit comments

Comments
 (0)