Skip to content

Commit

Permalink
支持修改应用语言
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy committed Jun 28, 2024
1 parent 442f890 commit 94ddedb
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Desktop/RodelAgent.UI.Models/Constants/SettingNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public enum SettingNames
IsMigrating,
LastSelectedGroup,
ChatGroupPanelType,
AppLanguage,
}
6 changes: 6 additions & 0 deletions src/Desktop/RodelAgent.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using RodelAgent.UI.Models.Constants;
using RodelAgent.UI.Toolkits;
using Windows.ApplicationModel.Activation;
using Windows.Globalization;
using Windows.Storage;

namespace RodelAgent.UI;
Expand All @@ -29,6 +30,11 @@ public partial class App : Application
public App()
{
InitializeComponent();
var language = SettingsToolkit.ReadLocalSetting(SettingNames.AppLanguage, "default");
ApplicationLanguages.PrimaryLanguageOverride = language != "default"
? language
: string.Empty;

FluentIcons.WinUI.Extensions.UseSegoeMetrics(this);
var mainAppInstance = AppInstance.FindOrRegisterForKey(Id);
mainAppInstance.Activated += OnAppInstanceActivated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private void OnUnloaded(object sender, RoutedEventArgs e)
private void Clean()
{
_document.RichTextBlock.Blocks.Clear();
_renderer.Clear();
_renderer?.Clear();
GC.Collect();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:SettingSectionBase
x:Class="RodelAgent.UI.Controls.Settings.LanguageSettingSection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:community="using:CommunityToolkit.WinUI.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ext="using:RodelAgent.UI.Extensions"
xmlns:ic="using:FluentIcons.WinUI"
xmlns:local="using:RodelAgent.UI.Controls.Settings"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<community:SettingsCard Description="{ext:Locale Name=LanguageSettingDescription}" Header="{ext:Locale Name=LanguageSetting}">
<community:SettingsCard.HeaderIcon>
<ic:SymbolIcon Symbol="LocalLanguage" />
</community:SettingsCard.HeaderIcon>

<ComboBox
x:Name="LanguageComboBox"
MinWidth="120"
AutomationProperties.Name="{ext:Locale Name=LanguageSetting}"
SelectionChanged="OnLanguageChangedAsync"
SelectionChangedTrigger="Committed">
<ComboBoxItem Content="{ext:Locale Name=SystemDefault}" Tag="default" />
<ComboBoxItem Content="简体中文" Tag="zh-Hans-CN" />
<ComboBoxItem Content="English" Tag="en-US" />
</ComboBox>
</community:SettingsCard>
</local:SettingSectionBase>
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Rodel. All rights reserved.

using System.ComponentModel;
using Microsoft.Windows.AppLifecycle;
using RodelAgent.UI.Models.Constants;
using RodelAgent.UI.Toolkits;

namespace RodelAgent.UI.Controls.Settings;

/// <summary>
/// 应用语言设置.
/// </summary>
public sealed partial class LanguageSettingSection : SettingSectionBase
{
/// <summary>
/// Initializes a new instance of the <see cref="LanguageSettingSection"/> class.
/// </summary>
public LanguageSettingSection()
{
InitializeComponent();
Loaded += OnLoaded;
Unloaded += OnUnloaded;
}

private void OnUnloaded(object sender, RoutedEventArgs e)
=> ViewModel.PropertyChanged -= OnViewModelPropertyChanged;

private void OnLoaded(object sender, RoutedEventArgs e)
{
ViewModel.PropertyChanged += OnViewModelPropertyChanged;
CheckLanguage();
}

private void OnViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(ViewModel.AppLanguage))
{
CheckLanguage();
}
}

private void CheckLanguage()
{
if (string.IsNullOrEmpty(ViewModel.AppLanguage))
{
return;
}

var selectIndex = ViewModel.AppLanguage switch
{
"zh-Hans-CN" => 1,
"en-US" => 2,
_ => 0,
};

if (selectIndex == LanguageComboBox.SelectedIndex)
{
return;
}

LanguageComboBox.SelectedIndex = selectIndex;
}

private async void OnLanguageChangedAsync(object sender, SelectionChangedEventArgs e)
{
var item = e.AddedItems.First() as ComboBoxItem;
var lanCode = item?.Tag as string;
if (string.IsNullOrEmpty(lanCode)
|| (!string.IsNullOrEmpty(ViewModel.AppLanguage) && ViewModel.AppLanguage == lanCode))
{
return;
}

ViewModel.AppLanguage = lanCode;
var dialog = new TipDialog(ResourceToolkit.GetLocalizedString(StringNames.ChangeLanguageTip));
var result = await dialog.ShowAsync();
if (result == ContentDialogResult.Primary)
{
AppInstance.GetCurrent().UnregisterKey();
_ = AppInstance.Restart(default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Style="{StaticResource BodyStrongTextBlockStyle}"
Text="{ext:Locale Name=Generic}" />
<settings:ThemeSettingSection ViewModel="{x:Bind ViewModel}" />
<settings:LanguageSettingSection ViewModel="{x:Bind ViewModel}" />
<settings:AppLogSettingSection ViewModel="{x:Bind ViewModel}" />
<settings:LibrarySettingSection ViewModel="{x:Bind ViewModel}" />
<TextBlock
Expand Down
9 changes: 9 additions & 0 deletions src/Desktop/RodelAgent.UI/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@
<data name="ChangeDirectory" xml:space="preserve">
<value>Change directory</value>
</data>
<data name="ChangeLanguageTip" xml:space="preserve">
<value>You have changed the language of the app and need to restart the app for it to take effect.</value>
</data>
<data name="ChatInputPlaceholder" xml:space="preserve">
<value>Please enter chat content...</value>
</data>
Expand Down Expand Up @@ -564,6 +567,12 @@
<data name="Language" xml:space="preserve">
<value>Language</value>
</data>
<data name="LanguageSetting" xml:space="preserve">
<value>Application language</value>
</data>
<data name="LanguageSettingDescription" xml:space="preserve">
<value>Select the preferred language of the application.</value>
</data>
<data name="LaunchApp" xml:space="preserve">
<value>Launch app</value>
</data>
Expand Down
9 changes: 9 additions & 0 deletions src/Desktop/RodelAgent.UI/Resources/zh-Hans-CN/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@
<data name="ChangeDirectory" xml:space="preserve">
<value>更改目录</value>
</data>
<data name="ChangeLanguageTip" xml:space="preserve">
<value>您更改了应用语言,需要重启应用以生效</value>
</data>
<data name="ChatInputPlaceholder" xml:space="preserve">
<value>请输入聊天内容...</value>
</data>
Expand Down Expand Up @@ -564,6 +567,12 @@
<data name="Language" xml:space="preserve">
<value>语言</value>
</data>
<data name="LanguageSetting" xml:space="preserve">
<value>应用程序语言</value>
</data>
<data name="LanguageSettingDescription" xml:space="preserve">
<value>选择应用程序的首选语言</value>
</data>
<data name="LaunchApp" xml:space="preserve">
<value>启动应用</value>
</data>
Expand Down
4 changes: 4 additions & 0 deletions src/Desktop/RodelAgent.UI/RodelAgent.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
<None Remove="Controls\Settings\AzureTranslateConfigSettingSection.xaml" />
<None Remove="Controls\Settings\HunYuanChatConfigSettingSection.xaml" />
<None Remove="Controls\Settings\HunYuanDrawConfigSettingSection.xaml" />
<None Remove="Controls\Settings\LanguageSettingSection.xaml" />
<None Remove="Controls\Settings\LibrarySettingSection.xaml" />
<None Remove="Controls\Settings\ModelClientConfigSettingSection.xaml" />
<None Remove="Controls\Settings\ModelClientEndpointConfigSettingSection.xaml" />
Expand Down Expand Up @@ -604,6 +605,9 @@
<Page Update="Controls\Chat\GroupOptionsPanel.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Controls\Settings\LanguageSettingSection.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public sealed partial class SettingsPageViewModel
[ObservableProperty]
private bool _shouldRecordTranslate;

[ObservableProperty]
private string _appLanguage;

/// <summary>
/// 在线聊天服务.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public SettingsPageViewModel(
private void InitializeGenericSettings()
{
AppTheme = SettingsToolkit.ReadLocalSetting(SettingNames.AppTheme, ElementTheme.Default);
CheckTheme();
AppLanguage = SettingsToolkit.ReadLocalSetting(SettingNames.AppLanguage, "default");
UseStreamOutput = SettingsToolkit.ReadLocalSetting(SettingNames.UseStreamOutput, true);
AppVersion = AppToolkit.GetPackageVersion();
var copyrightTemplate = ResourceToolkit.GetLocalizedString(StringNames.CopyrightTemplate);
Expand Down Expand Up @@ -156,6 +158,9 @@ partial void OnAppThemeChanged(ElementTheme value)
CheckTheme();
}

partial void OnAppLanguageChanged(string value)
=> SettingsToolkit.WriteLocalSetting(SettingNames.AppLanguage, value);

partial void OnUseStreamOutputChanged(bool value)
=> SettingsToolkit.WriteLocalSetting(SettingNames.UseStreamOutput, value);

Expand Down

0 comments on commit 94ddedb

Please sign in to comment.