mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-03-20 16:36:36 +08:00
Merge pull request #520 from irihitech/locale
Add locale switch helper method
This commit is contained in:
@@ -60,7 +60,7 @@
|
||||
Foreground="{DynamicResource ButtonDefaultTertiaryForeground}"
|
||||
Command="{Binding ToggleThemeCommand}"
|
||||
OnContent="{StaticResource SemiIconMoon}"
|
||||
OffContent="{StaticResource SemiIconSun}" />
|
||||
OffContent="{StaticResource SemiIconSun}"/>
|
||||
|
||||
<Button>
|
||||
<PathIcon Data="{StaticResource SemiIconMenu}" />
|
||||
|
||||
@@ -60,6 +60,49 @@ public partial class MainViewModel : ObservableObject
|
||||
CommandParameter = SemiTheme.NightSky
|
||||
},
|
||||
]
|
||||
},
|
||||
new MenuItemViewModel()
|
||||
{
|
||||
Header = "Locale",
|
||||
Items=
|
||||
[
|
||||
new MenuItemViewModel
|
||||
{
|
||||
Header = "简体中文",
|
||||
Command = SelectLocaleCommand,
|
||||
CommandParameter = new System.Globalization.CultureInfo("zh-cn")
|
||||
},
|
||||
new MenuItemViewModel
|
||||
{
|
||||
Header = "English",
|
||||
Command = SelectLocaleCommand,
|
||||
CommandParameter = new System.Globalization.CultureInfo("en-us")
|
||||
},
|
||||
new MenuItemViewModel
|
||||
{
|
||||
Header = "日本語",
|
||||
Command = SelectLocaleCommand,
|
||||
CommandParameter = new System.Globalization.CultureInfo("ja-jp")
|
||||
},
|
||||
new MenuItemViewModel
|
||||
{
|
||||
Header = "Українська",
|
||||
Command = SelectLocaleCommand,
|
||||
CommandParameter = new System.Globalization.CultureInfo("uk-ua")
|
||||
},
|
||||
new MenuItemViewModel
|
||||
{
|
||||
Header = "Русский",
|
||||
Command = SelectLocaleCommand,
|
||||
CommandParameter = new System.Globalization.CultureInfo("ru-ru")
|
||||
},
|
||||
new MenuItemViewModel
|
||||
{
|
||||
Header = "繁體中文",
|
||||
Command = SelectLocaleCommand,
|
||||
CommandParameter = new System.Globalization.CultureInfo("zh-tw")
|
||||
},
|
||||
]
|
||||
}
|
||||
];
|
||||
}
|
||||
@@ -82,6 +125,16 @@ public partial class MainViewModel : ObservableObject
|
||||
app.RequestedThemeVariant = obj as ThemeVariant;
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void SelectLocale(object? obj)
|
||||
{
|
||||
var app = Application.Current;
|
||||
if (app is not null)
|
||||
{
|
||||
SemiTheme.OverrideLocaleResources(app, obj as System.Globalization.CultureInfo);
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private static async Task OpenUrl(string url)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Styling;
|
||||
@@ -60,4 +61,24 @@ public class SemiTheme : Styles
|
||||
}
|
||||
return _localeToResource[new CultureInfo("zh-cn")];
|
||||
}
|
||||
|
||||
public static void OverrideLocaleResources(Application application, CultureInfo? culture)
|
||||
{
|
||||
if (culture is null) return;
|
||||
if (!_localeToResource.TryGetValue(culture, out var resources)) return;
|
||||
foreach (var kv in resources)
|
||||
{
|
||||
application.Resources[kv.Key] = kv.Value;
|
||||
}
|
||||
}
|
||||
|
||||
public static void OverrideLocaleResources(StyledElement element, CultureInfo? culture)
|
||||
{
|
||||
if (culture is null) return;
|
||||
if (!_localeToResource.TryGetValue(culture, out var resources)) return;
|
||||
foreach (var kv in resources)
|
||||
{
|
||||
element.Resources[kv.Key] = kv.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user