feat: implement locale switch.

This commit is contained in:
Zhang Dian 2024-02-21 16:32:48 +08:00
parent 6459a8be1b
commit 82f284b5fa
7 changed files with 84 additions and 11 deletions

View File

@ -1,9 +1,10 @@
<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>
<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

@ -0,0 +1,7 @@
<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>
</ResourceDictionary>

View File

@ -0,0 +1,7 @@
<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>
</ResourceDictionary>

View File

@ -1,4 +1,4 @@
<Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<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>
@ -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,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")];
}
}