mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-04-30 13:13:24 +08:00
feat: add simple locale switch.
This commit is contained in:
@@ -47,6 +47,21 @@
|
|||||||
</Style>
|
</Style>
|
||||||
</StackPanel.Styles>
|
</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}">
|
<Button Command="{Binding OpenUrlCommand}" CommandParameter="{Binding DocumentationUrl}">
|
||||||
<PathIcon Data="{StaticResource SemiIconGlobe}" />
|
<PathIcon Data="{StaticResource SemiIconGlobe}" />
|
||||||
</Button>
|
</Button>
|
||||||
@@ -60,7 +75,7 @@
|
|||||||
Foreground="{DynamicResource ButtonDefaultTertiaryForeground}"
|
Foreground="{DynamicResource ButtonDefaultTertiaryForeground}"
|
||||||
Command="{Binding ToggleThemeCommand}"
|
Command="{Binding ToggleThemeCommand}"
|
||||||
OnContent="{StaticResource SemiIconMoon}"
|
OnContent="{StaticResource SemiIconMoon}"
|
||||||
OffContent="{StaticResource SemiIconSun}" />
|
OffContent="{StaticResource SemiIconSun}"/>
|
||||||
|
|
||||||
<Button>
|
<Button>
|
||||||
<PathIcon Data="{StaticResource SemiIconMenu}" />
|
<PathIcon Data="{StaticResource SemiIconMenu}" />
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ public partial class MainViewModel : ObservableObject
|
|||||||
public string RepoUrl => "https://github.com/irihitech/Semi.Avalonia";
|
public string RepoUrl => "https://github.com/irihitech/Semi.Avalonia";
|
||||||
public IReadOnlyList<MenuItemViewModel> MenuItems { get; }
|
public IReadOnlyList<MenuItemViewModel> MenuItems { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<MenuItemViewModel> LocaleItems { get; }
|
||||||
|
|
||||||
public MainViewModel()
|
public MainViewModel()
|
||||||
{
|
{
|
||||||
MenuItems =
|
MenuItems =
|
||||||
@@ -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]
|
[RelayCommand]
|
||||||
@@ -83,6 +131,16 @@ public partial class MainViewModel : ObservableObject
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[RelayCommand]
|
||||||
|
private void SelectLocale(object? obj)
|
||||||
|
{
|
||||||
|
var app = Application.Current;
|
||||||
|
if (app is not null)
|
||||||
|
{
|
||||||
|
SemiTheme.OverrideLocaleResources(app, obj as System.Globalization.CultureInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[RelayCommand]
|
[RelayCommand]
|
||||||
private static async Task OpenUrl(string url)
|
private static async Task OpenUrl(string url)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -62,8 +62,9 @@ public class SemiTheme : Styles
|
|||||||
return _localeToResource[new CultureInfo("zh-cn")];
|
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;
|
if (!_localeToResource.TryGetValue(culture, out var resources)) return;
|
||||||
foreach (var kv in resources)
|
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;
|
if (!_localeToResource.TryGetValue(culture, out var resources)) return;
|
||||||
foreach (var kv in resources)
|
foreach (var kv in resources)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user