Merge pull request #301 from irihitech/296-index

Implement Locale Switch
This commit is contained in:
Dong Bin 2024-02-23 19:40:24 +08:00 committed by GitHub
commit a920dbc873
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 114 additions and 13 deletions

View File

@ -1,9 +1,13 @@
<Application
x:Class="Semi.Avalonia.Demo.App"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:semi="https://irihi.tech/semi">
<Application.Styles>
<!-- You can still reference in old way.
<StyleInclude Source="avares://Semi.Avalonia/Themes/Index.axaml" />
-->
<semi:SemiTheme Locale="zh-CN"/>
<StyleInclude Source="avares://Semi.Avalonia.DataGrid/Index.axaml" />
<StyleInclude Source="avares://Semi.Avalonia.ColorPicker/Index.axaml" />
</Application.Styles>

View File

@ -0,0 +1,4 @@
using Avalonia.Metadata;
[assembly:XmlnsPrefix("https://irihi.tech/semi", "semi")]
[assembly:XmlnsDefinition("https://irihi.tech/semi", "Semi.Avalonia")]

View File

@ -80,11 +80,11 @@
<TextBox
IsVisible="{Binding !SelectingFolder}"
Text="{Binding FileName}"
Watermark="File name" />
Watermark="{DynamicResource STRING_CHOOSER_FILE_NAME}" />
</DockPanel>
<CheckBox
VerticalAlignment="Center"
Content="Show hidden files"
Content="{DynamicResource STRING_CHOOSER_SHOW_HIDDEN_FILES}"
DockPanel.Dock="Left"
IsChecked="{Binding ShowHiddenFiles}" />
<UniformGrid
@ -94,15 +94,13 @@
<Button
Margin="8,0,0,0"
Classes="Primary"
Command="{Binding Ok}">
OK
</Button>
Command="{Binding Ok}"
Content="{DynamicResource STRING_CHOOSER_DIALOG_OK}" />
<Button
Margin="8,0,0,0"
Classes="Danger"
Command="{Binding Cancel}">
Cancel
</Button>
Command="{Binding Cancel}"
Content="{DynamicResource STRING_CHOOSER_DIALOG_CANCEL}" />
</UniformGrid>
</DockPanel>
<Border

View File

@ -7,7 +7,7 @@
<MenuItem
x:Name="SelectableTextBlockContextFlyoutCopyItem"
Command="{Binding $parent[SelectableTextBlock].Copy}"
Header="Copy"
Header="{DynamicResource STRING_MENU_COPY}"
InputGesture="{x:Static TextBox.CopyGesture}"
IsEnabled="{Binding $parent[SelectableTextBlock].CanCopy}" />
</MenuFlyout>

View File

@ -6,19 +6,19 @@
<MenuItem
x:Name="TextBoxContextFlyoutCutItem"
Command="{Binding $parent[TextBox].Cut}"
Header="Cut"
Header="{DynamicResource STRING_MENU_CUT}"
InputGesture="{x:Static TextBox.CutGesture}"
IsEnabled="{Binding $parent[TextBox].CanCut}" />
<MenuItem
x:Name="TextBoxContextFlyoutCopyItem"
Command="{Binding $parent[TextBox].Copy}"
Header="Copy"
Header="{DynamicResource STRING_MENU_COPY}"
InputGesture="{x:Static TextBox.CopyGesture}"
IsEnabled="{Binding $parent[TextBox].CanCopy}" />
<MenuItem
x:Name="TextBoxContextFlyoutPasteItem"
Command="{Binding $parent[TextBox].Paste}"
Header="Paste"
Header="{DynamicResource STRING_MENU_PASTE}"
InputGesture="{x:Static TextBox.PasteGesture}"
IsEnabled="{Binding $parent[TextBox].CanPaste}" />
</MenuFlyout>

View File

@ -0,0 +1,10 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:String x:Key="STRING_CHOOSER_FILE_NAME">File name</x:String>
<x:String x:Key="STRING_CHOOSER_SHOW_HIDDEN_FILES">Show hidden flies</x:String>
<x:String x:Key="STRING_CHOOSER_DIALOG_OK">OK</x:String>
<x:String x:Key="STRING_CHOOSER_DIALOG_CANCEL">Cancel</x:String>
<x:String x:Key="STRING_MENU_CUT">Cut</x:String>
<x:String x:Key="STRING_MENU_COPY">Copy</x:String>
<x:String x:Key="STRING_MENU_PASTE">Paste</x:String>
</ResourceDictionary>

View File

@ -0,0 +1,10 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:String x:Key="STRING_CHOOSER_FILE_NAME">文件名</x:String>
<x:String x:Key="STRING_CHOOSER_SHOW_HIDDEN_FILES">显示隐藏文件</x:String>
<x:String x:Key="STRING_CHOOSER_DIALOG_OK">确认</x:String>
<x:String x:Key="STRING_CHOOSER_DIALOG_CANCEL">取消</x:String>
<x:String x:Key="STRING_MENU_CUT">剪切</x:String>
<x:String x:Key="STRING_MENU_COPY">复制</x:String>
<x:String x:Key="STRING_MENU_PASTE">粘贴</x:String>
</ResourceDictionary>

View File

@ -9,6 +9,7 @@
<ResourceInclude Source="avares://Semi.Avalonia/Controls/_index.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Base.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/_index.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Locale/zh-cn.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Styles.Resources>

View File

@ -0,0 +1,19 @@
<Styles x:Class="Semi.Avalonia.SemiTheme" xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Styles.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<MergeResourceInclude x:Key="Default" Source="avares://Semi.Avalonia/Themes/Light/_index.axaml" />
<MergeResourceInclude x:Key="Dark" Source="avares://Semi.Avalonia/Themes/Dark/_index.axaml" />
</ResourceDictionary.ThemeDictionaries>
<ResourceDictionary.MergedDictionaries>
<ResourceInclude Source="avares://Semi.Avalonia/Controls/_index.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Base.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Themes/Shared/_index.axaml" />
<ResourceInclude Source="avares://Semi.Avalonia/Locale/zh-cn.axaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Styles.Resources>
<Style Selector=":is(UserControl)">
<Setter Property="Theme" Value="{DynamicResource {x:Type UserControl}}" />
</Style>
</Styles>

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
namespace Semi.Avalonia;
public class SemiTheme: Styles
{
private static readonly Dictionary<CultureInfo, string> _localeToResource = new()
{
{ new CultureInfo("zh-CN"), "avares://Semi.Avalonia/Locale/zh-CN.axaml" },
{ new CultureInfo("en-US"), "avares://Semi.Avalonia/Locale/en-US.axaml" },
};
private readonly IServiceProvider? sp;
public SemiTheme(IServiceProvider? provider = null)
{
sp = provider;
AvaloniaXamlLoader.Load(provider, this);
}
private CultureInfo? _locale;
public CultureInfo? Locale
{
get => _locale;
set
{
_locale = value;
var resource = TryGetLocaleResource(value);
var d = AvaloniaXamlLoader.Load(sp, new Uri(resource)) as ResourceDictionary;
if (d is null) return;
foreach (var kv in d)
{
this.Resources.Add(kv);
}
}
}
private static string TryGetLocaleResource(CultureInfo? locale)
{
if (locale is null)
{
return _localeToResource[new CultureInfo("zh-CN")];
}
if (_localeToResource.TryGetValue(locale, out var resource))
{
return resource;
}
return _localeToResource[new CultureInfo("zh-CN")];
}
}