mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-03-16 06:26:36 +08:00
feat: add simple locale switch.
This commit is contained in:
@@ -46,6 +46,21 @@
|
||||
<Setter Property="Theme" Value="{DynamicResource InnerPathIcon}" />
|
||||
</Style>
|
||||
</StackPanel.Styles>
|
||||
|
||||
<Button>
|
||||
<PathIcon Data="{StaticResource SemiIconLanguage}" />
|
||||
<Button.Flyout>
|
||||
<MenuFlyout Placement="Bottom" ItemsSource="{Binding LocaleItems}" />
|
||||
</Button.Flyout>
|
||||
<Button.Styles>
|
||||
<Style Selector="MenuItem" x:DataType="views:MenuItemViewModel">
|
||||
<Setter Property="Header" Value="{Binding Header}" />
|
||||
<Setter Property="ItemsSource" Value="{Binding Items}" />
|
||||
<Setter Property="Command" Value="{Binding Command}" />
|
||||
<Setter Property="CommandParameter" Value="{Binding CommandParameter}" />
|
||||
</Style>
|
||||
</Button.Styles>
|
||||
</Button>
|
||||
|
||||
<Button Command="{Binding OpenUrlCommand}" CommandParameter="{Binding DocumentationUrl}">
|
||||
<PathIcon Data="{StaticResource SemiIconGlobe}" />
|
||||
@@ -60,7 +75,7 @@
|
||||
Foreground="{DynamicResource ButtonDefaultTertiaryForeground}"
|
||||
Command="{Binding ToggleThemeCommand}"
|
||||
OnContent="{StaticResource SemiIconMoon}"
|
||||
OffContent="{StaticResource SemiIconSun}" />
|
||||
OffContent="{StaticResource SemiIconSun}"/>
|
||||
|
||||
<Button>
|
||||
<PathIcon Data="{StaticResource SemiIconMenu}" />
|
||||
|
||||
@@ -25,6 +25,8 @@ public partial class MainViewModel : ObservableObject
|
||||
public string DocumentationUrl => "https://docs.irihi.tech/semi";
|
||||
public string RepoUrl => "https://github.com/irihitech/Semi.Avalonia";
|
||||
public IReadOnlyList<MenuItemViewModel> MenuItems { get; }
|
||||
|
||||
public IReadOnlyList<MenuItemViewModel> LocaleItems { get; }
|
||||
|
||||
public MainViewModel()
|
||||
{
|
||||
@@ -62,6 +64,52 @@ public partial class MainViewModel : ObservableObject
|
||||
]
|
||||
}
|
||||
];
|
||||
LocaleItems =
|
||||
[
|
||||
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")
|
||||
},
|
||||
]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
@@ -82,6 +130,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)
|
||||
|
||||
@@ -62,8 +62,9 @@ public class SemiTheme : Styles
|
||||
return _localeToResource[new CultureInfo("zh-cn")];
|
||||
}
|
||||
|
||||
public static void OverrideLocaleResources(Application application, CultureInfo culture)
|
||||
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)
|
||||
{
|
||||
@@ -71,8 +72,9 @@ public class SemiTheme : Styles
|
||||
}
|
||||
}
|
||||
|
||||
public static void OverrideLocaleResources(StyledElement element, CultureInfo culture)
|
||||
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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user