mirror of
https://gitcode.com/gh_mirrors/se/Semi.Avalonia
synced 2026-05-02 14:01:28 +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}"
|
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}" />
|
||||||
|
|||||||
@@ -60,6 +60,49 @@ public partial class MainViewModel : ObservableObject
|
|||||||
CommandParameter = SemiTheme.NightSky
|
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")
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -83,6 +126,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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using Avalonia.Styling;
|
using Avalonia.Styling;
|
||||||
@@ -60,4 +61,24 @@ public class SemiTheme : Styles
|
|||||||
}
|
}
|
||||||
return _localeToResource[new CultureInfo("zh-cn")];
|
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