Use Flow.Launcher.Localization to improve code quality

This commit is contained in:
Jack251970 2025-06-23 12:25:16 +08:00
parent 40e5c04ca1
commit fe7985d564
6 changed files with 48 additions and 47 deletions

View file

@ -1,18 +1,17 @@
using System.ComponentModel; using Flow.Launcher.Localization.Attributes;
using Flow.Launcher.Core.Resource;
namespace Flow.Launcher.Plugin.Calculator namespace Flow.Launcher.Plugin.Calculator
{ {
[TypeConverter(typeof(LocalizationConverter))] [EnumLocalize]
public enum DecimalSeparator public enum DecimalSeparator
{ {
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_use_system_locale")] [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_use_system_locale))]
UseSystemLocale, UseSystemLocale,
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_dot")] [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_dot))]
Dot, Dot,
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_comma")] [EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_comma))]
Comma Comma
} }
} }

View file

@ -42,7 +42,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Flow.Launcher.Core\Flow.Launcher.Core.csproj" />
<ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" /> <ProjectReference Include="..\..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
</ItemGroup> </ItemGroup>
@ -63,6 +62,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.3"/>
<PackageReference Include="Mages" Version="3.0.0" /> <PackageReference Include="Mages" Version="3.0.0" />
</ItemGroup> </ItemGroup>

View file

@ -5,7 +5,6 @@ using System.Runtime.InteropServices;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows.Controls; using System.Windows.Controls;
using Mages.Core; using Mages.Core;
using Flow.Launcher.Plugin.Calculator.ViewModels;
using Flow.Launcher.Plugin.Calculator.Views; using Flow.Launcher.Plugin.Calculator.Views;
namespace Flow.Launcher.Plugin.Calculator namespace Flow.Launcher.Plugin.Calculator
@ -24,19 +23,17 @@ namespace Flow.Launcher.Plugin.Calculator
@")+$", RegexOptions.Compiled); @")+$", RegexOptions.Compiled);
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled); private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static Engine MagesEngine; private static Engine MagesEngine;
private const string comma = ","; private const string Comma = ",";
private const string dot = "."; private const string Dot = ".";
private PluginInitContext Context { get; set; } internal static PluginInitContext Context { get; set; } = null!;
private static Settings _settings; private static Settings _settings;
private static SettingsViewModel _viewModel;
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
Context = context; Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>(); _settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(_settings);
MagesEngine = new Engine(new Configuration MagesEngine = new Engine(new Configuration
{ {
@ -72,10 +69,10 @@ namespace Flow.Launcher.Plugin.Calculator
var result = MagesEngine.Interpret(expression); var result = MagesEngine.Interpret(expression);
if (result?.ToString() == "NaN") if (result?.ToString() == "NaN")
result = Context.API.GetTranslation("flowlauncher_plugin_calculator_not_a_number"); result = Localize.flowlauncher_plugin_calculator_not_a_number();
if (result is Function) if (result is Function)
result = Context.API.GetTranslation("flowlauncher_plugin_calculator_expression_not_complete"); result = Localize.flowlauncher_plugin_calculator_expression_not_complete();
if (!string.IsNullOrEmpty(result?.ToString())) if (!string.IsNullOrEmpty(result?.ToString()))
{ {
@ -89,7 +86,7 @@ namespace Flow.Launcher.Plugin.Calculator
Title = newResult, Title = newResult,
IcoPath = "Images/calculator.png", IcoPath = "Images/calculator.png",
Score = 300, Score = 300,
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_calculator_copy_number_to_clipboard"), SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
CopyText = newResult, CopyText = newResult,
Action = c => Action = c =>
{ {
@ -134,16 +131,16 @@ namespace Flow.Launcher.Plugin.Calculator
return false; return false;
} }
if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) || if ((query.Search.Contains(Dot) && GetDecimalSeparator() != Dot) ||
(query.Search.Contains(comma) && GetDecimalSeparator() != comma)) (query.Search.Contains(Comma) && GetDecimalSeparator() != Comma))
return false; return false;
return true; return true;
} }
private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator) private static string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
{ {
if (String.IsNullOrEmpty(newDecimalSeparator)) if (string.IsNullOrEmpty(newDecimalSeparator))
{ {
return value.ToString(); return value.ToString();
} }
@ -161,13 +158,13 @@ namespace Flow.Launcher.Plugin.Calculator
return _settings.DecimalSeparator switch return _settings.DecimalSeparator switch
{ {
DecimalSeparator.UseSystemLocale => systemDecimalSeparator, DecimalSeparator.UseSystemLocale => systemDecimalSeparator,
DecimalSeparator.Dot => dot, DecimalSeparator.Dot => Dot,
DecimalSeparator.Comma => comma, DecimalSeparator.Comma => Comma,
_ => systemDecimalSeparator, _ => systemDecimalSeparator,
}; };
} }
private bool IsBracketComplete(string query) private static bool IsBracketComplete(string query)
{ {
var matchs = RegBrackets.Matches(query); var matchs = RegBrackets.Matches(query);
var leftBracketCount = 0; var leftBracketCount = 0;
@ -188,17 +185,17 @@ namespace Flow.Launcher.Plugin.Calculator
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()
{ {
return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_name"); return Localize.flowlauncher_plugin_caculator_plugin_name();
} }
public string GetTranslatedPluginDescription() public string GetTranslatedPluginDescription()
{ {
return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_description"); return Localize.flowlauncher_plugin_caculator_plugin_description();
} }
public Control CreateSettingPanel() public Control CreateSettingPanel()
{ {
return new CalculatorSettings(_viewModel); return new CalculatorSettings(_settings);
} }
} }
} }

View file

@ -8,10 +8,26 @@ namespace Flow.Launcher.Plugin.Calculator.ViewModels
public SettingsViewModel(Settings settings) public SettingsViewModel(Settings settings)
{ {
Settings = settings; Settings = settings;
DecimalSeparatorLocalized.UpdateLabels(AllDecimalSeparator);
} }
public Settings Settings { get; init; } public Settings Settings { get; init; }
public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20); public IEnumerable<int> MaxDecimalPlacesRange => Enumerable.Range(1, 20);
public List<DecimalSeparatorLocalized> AllDecimalSeparator { get; } = DecimalSeparatorLocalized.GetValues();
public DecimalSeparator SelectedDecimalSeparator
{
get => Settings.DecimalSeparator;
set
{
if (Settings.DecimalSeparator != value)
{
Settings.DecimalSeparator = value;
OnPropertyChanged();
}
}
}
} }
} }

View file

@ -3,20 +3,15 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:calculator="clr-namespace:Flow.Launcher.Plugin.Calculator" xmlns:calculator="clr-namespace:Flow.Launcher.Plugin.Calculator"
xmlns:core="clr-namespace:Flow.Launcher.Core.Resource;assembly=Flow.Launcher.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:Flow.Launcher.Infrastructure.UI;assembly=Flow.Launcher.Infrastructure"
xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.Calculator.ViewModels" xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.Calculator.ViewModels"
d:DataContext="{d:DesignInstance Type=viewModels:SettingsViewModel}"
d:DesignHeight="450" d:DesignHeight="450"
d:DesignWidth="800" d:DesignWidth="800"
Loaded="CalculatorSettings_Loaded" Loaded="CalculatorSettings_Loaded"
mc:Ignorable="d"> mc:Ignorable="d">
<UserControl.Resources>
<core:LocalizationConverter x:Key="LocalizationConverter" />
</UserControl.Resources>
<Grid Margin="{StaticResource SettingPanelMargin}"> <Grid Margin="{StaticResource SettingPanelMargin}">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="auto" /> <RowDefinition Height="auto" />
@ -42,14 +37,10 @@
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}" Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
HorizontalAlignment="Left" HorizontalAlignment="Left"
VerticalAlignment="Center" VerticalAlignment="Center"
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type calculator:DecimalSeparator}}}" DisplayMemberPath="Display"
SelectedItem="{Binding Settings.DecimalSeparator}"> ItemsSource="{Binding AllDecimalSeparator}"
<ComboBox.ItemTemplate> SelectedValue="{Binding SelectedDecimalSeparator, Mode=TwoWay}"
<DataTemplate> SelectedValuePath="Value" />
<TextBlock FontSize="14" Text="{Binding Converter={StaticResource LocalizationConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<TextBlock <TextBlock
Grid.Row="1" Grid.Row="1"

View file

@ -1,4 +1,4 @@
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Flow.Launcher.Plugin.Calculator.ViewModels; using Flow.Launcher.Plugin.Calculator.ViewModels;
@ -12,11 +12,11 @@ namespace Flow.Launcher.Plugin.Calculator.Views
private readonly SettingsViewModel _viewModel; private readonly SettingsViewModel _viewModel;
private readonly Settings _settings; private readonly Settings _settings;
public CalculatorSettings(SettingsViewModel viewModel) public CalculatorSettings(Settings settings)
{ {
_viewModel = viewModel; _viewModel = new SettingsViewModel(settings);
_settings = viewModel.Settings; _settings = _viewModel.Settings;
DataContext = viewModel; DataContext = _viewModel;
InitializeComponent(); InitializeComponent();
} }
@ -26,6 +26,4 @@ namespace Flow.Launcher.Plugin.Calculator.Views
MaxDecimalPlaces.SelectedItem = _settings.MaxDecimalPlaces; MaxDecimalPlaces.SelectedItem = _settings.MaxDecimalPlaces;
} }
} }
} }