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.Core.Resource;
using Flow.Launcher.Localization.Attributes;
namespace Flow.Launcher.Plugin.Calculator
{
[TypeConverter(typeof(LocalizationConverter))]
[EnumLocalize]
public enum DecimalSeparator
{
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_use_system_locale")]
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_use_system_locale))]
UseSystemLocale,
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_dot")]
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_dot))]
Dot,
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_comma")]
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_seperator_comma))]
Comma
}
}

View file

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

View file

@ -5,7 +5,6 @@ using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using Mages.Core;
using Flow.Launcher.Plugin.Calculator.ViewModels;
using Flow.Launcher.Plugin.Calculator.Views;
namespace Flow.Launcher.Plugin.Calculator
@ -24,19 +23,17 @@ namespace Flow.Launcher.Plugin.Calculator
@")+$", RegexOptions.Compiled);
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
private static Engine MagesEngine;
private const string comma = ",";
private const string dot = ".";
private const string Comma = ",";
private const string Dot = ".";
private PluginInitContext Context { get; set; }
internal static PluginInitContext Context { get; set; } = null!;
private static Settings _settings;
private static SettingsViewModel _viewModel;
public void Init(PluginInitContext context)
{
Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(_settings);
MagesEngine = new Engine(new Configuration
{
@ -72,10 +69,10 @@ namespace Flow.Launcher.Plugin.Calculator
var result = MagesEngine.Interpret(expression);
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)
result = Context.API.GetTranslation("flowlauncher_plugin_calculator_expression_not_complete");
result = Localize.flowlauncher_plugin_calculator_expression_not_complete();
if (!string.IsNullOrEmpty(result?.ToString()))
{
@ -89,7 +86,7 @@ namespace Flow.Launcher.Plugin.Calculator
Title = newResult,
IcoPath = "Images/calculator.png",
Score = 300,
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_calculator_copy_number_to_clipboard"),
SubTitle = Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
CopyText = newResult,
Action = c =>
{
@ -134,16 +131,16 @@ namespace Flow.Launcher.Plugin.Calculator
return false;
}
if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) ||
(query.Search.Contains(comma) && GetDecimalSeparator() != comma))
if ((query.Search.Contains(Dot) && GetDecimalSeparator() != Dot) ||
(query.Search.Contains(Comma) && GetDecimalSeparator() != Comma))
return false;
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();
}
@ -161,13 +158,13 @@ namespace Flow.Launcher.Plugin.Calculator
return _settings.DecimalSeparator switch
{
DecimalSeparator.UseSystemLocale => systemDecimalSeparator,
DecimalSeparator.Dot => dot,
DecimalSeparator.Comma => comma,
DecimalSeparator.Dot => Dot,
DecimalSeparator.Comma => Comma,
_ => systemDecimalSeparator,
};
}
private bool IsBracketComplete(string query)
private static bool IsBracketComplete(string query)
{
var matchs = RegBrackets.Matches(query);
var leftBracketCount = 0;
@ -188,17 +185,17 @@ namespace Flow.Launcher.Plugin.Calculator
public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_name");
return Localize.flowlauncher_plugin_caculator_plugin_name();
}
public string GetTranslatedPluginDescription()
{
return Context.API.GetTranslation("flowlauncher_plugin_caculator_plugin_description");
return Localize.flowlauncher_plugin_caculator_plugin_description();
}
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)
{
Settings = settings;
DecimalSeparatorLocalized.UpdateLabels(AllDecimalSeparator);
}
public Settings Settings { get; init; }
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:x="http://schemas.microsoft.com/winfx/2006/xaml"
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: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"
d:DataContext="{d:DesignInstance Type=viewModels:SettingsViewModel}"
d:DesignHeight="450"
d:DesignWidth="800"
Loaded="CalculatorSettings_Loaded"
mc:Ignorable="d">
<UserControl.Resources>
<core:LocalizationConverter x:Key="LocalizationConverter" />
</UserControl.Resources>
<Grid Margin="{StaticResource SettingPanelMargin}">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
@ -42,14 +37,10 @@
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
ItemsSource="{Binding Source={ui:EnumBindingSource {x:Type calculator:DecimalSeparator}}}"
SelectedItem="{Binding Settings.DecimalSeparator}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="14" Text="{Binding Converter={StaticResource LocalizationConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
DisplayMemberPath="Display"
ItemsSource="{Binding AllDecimalSeparator}"
SelectedValue="{Binding SelectedDecimalSeparator, Mode=TwoWay}"
SelectedValuePath="Value" />
<TextBlock
Grid.Row="1"

View file

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