Merge pull request #2468 from flooxo/sys_cmd

Add translation keys for System Command Plugin
This commit is contained in:
VictoriousRaptor 2024-01-28 12:25:04 +08:00 committed by GitHub
commit 1e4589447e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 74 additions and 4 deletions

View file

@ -7,6 +7,28 @@
<system:String x:Key="flowlauncher_plugin_sys_command">Command</system:String>
<system:String x:Key="flowlauncher_plugin_sys_desc">Description</system:String>
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer_cmd">Shutdown</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_computer_cmd">Restart</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_advanced_cmd">Restart With Advanced Boot Options</system:String>
<system:String x:Key="flowlauncher_plugin_sys_log_off_cmd">Log Off/Sign Out</system:String>
<system:String x:Key="flowlauncher_plugin_sys_lock_cmd">Lock</system:String>
<system:String x:Key="flowlauncher_plugin_sys_sleep_cmd">Sleep</system:String>
<system:String x:Key="flowlauncher_plugin_sys_hibernate_cmd">Hibernate</system:String>
<system:String x:Key="flowlauncher_plugin_sys_indexoption_cmd">Index Option</system:String>
<system:String x:Key="flowlauncher_plugin_sys_emptyrecyclebin_cmd">Empty Recycle Bin</system:String>
<system:String x:Key="flowlauncher_plugin_sys_openrecyclebin_cmd">Open Recycle Bin</system:String>
<system:String x:Key="flowlauncher_plugin_sys_exit_cmd">Exit</system:String>
<system:String x:Key="flowlauncher_plugin_sys_save_all_settings_cmd">Save Settings</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_cmd">Restart Flow Launcher"</system:String>
<system:String x:Key="flowlauncher_plugin_sys_setting_cmd">Settings</system:String>
<system:String x:Key="flowlauncher_plugin_sys_reload_plugin_data_cmd">Reload Plugin Data</system:String>
<system:String x:Key="flowlauncher_plugin_sys_check_for_update_cmd">Check For Update</system:String>
<system:String x:Key="flowlauncher_plugin_sys_open_log_location_cmd">Open Log Location</system:String>
<system:String x:Key="flowlauncher_plugin_sys_open_docs_tips_cmd">Flow Launcher Tips</system:String>
<system:String x:Key="flowlauncher_plugin_sys_open_userdata_location_cmd">Flow Launcher UserData Folder</system:String>
<system:String x:Key="flowlauncher_plugin_sys_toggle_game_mode_cmd">Toggle Game Mode</system:String>
<!-- Command Descriptions -->
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer">Shutdown Computer</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_computer">Restart Computer</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_advanced">Restart the computer with Advanced Boot Options for Safe and Debugging modes, as well as other options</system:String>

View file

@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.SharedCommands;
using Application = System.Windows.Application;
@ -19,6 +21,7 @@ namespace Flow.Launcher.Plugin.Sys
public class Main : IPlugin, ISettingProvider, IPluginI18n
{
private PluginInitContext context;
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();
#region DllImport
@ -59,6 +62,8 @@ namespace Flow.Launcher.Plugin.Sys
var results = new List<Result>();
foreach (var c in commands)
{
c.Title = GetDynamicTitle(query, c);
var titleMatch = StringMatcher.FuzzySearch(query.Search, c.Title);
var subTitleMatch = StringMatcher.FuzzySearch(query.Search, c.SubTitle);
@ -77,9 +82,52 @@ namespace Flow.Launcher.Plugin.Sys
return results;
}
private string GetDynamicTitle(Query query, Result result)
{
if (!KeywordTitleMappings.TryGetValue(result.Title, out var translationKey))
{
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Dynamic Title not found for: {result.Title}");
return "Title Not Found";
}
var translatedTitle = context.API.GetTranslation(translationKey);
if (result.Title == translatedTitle)
{
return result.Title;
}
var englishTitleMatch = StringMatcher.FuzzySearch(query.Search, result.Title);
var translatedTitleMatch = StringMatcher.FuzzySearch(query.Search, translatedTitle);
return englishTitleMatch.Score >= translatedTitleMatch.Score ? result.Title : translatedTitle;
}
public void Init(PluginInitContext context)
{
this.context = context;
KeywordTitleMappings = new Dictionary<string, string>{
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
{"Restart With Advanced Boot Options", "flowlauncher_plugin_sys_restart_advanced_cmd"},
{"Log Off/Sign Out", "flowlauncher_plugin_sys_log_off_cmd"},
{"Lock", "flowlauncher_plugin_sys_lock_cmd"},
{"Sleep", "flowlauncher_plugin_sys_sleep_cmd"},
{"Hibernate", "flowlauncher_plugin_sys_hibernate_cmd"},
{"Index Option", "flowlauncher_plugin_sys_indexoption_cmd"},
{"Empty Recycle Bin", "flowlauncher_plugin_sys_emptyrecyclebin_cmd"},
{"Open Recycle Bin", "flowlauncher_plugin_sys_openrecyclebin_cmd"},
{"Exit", "flowlauncher_plugin_sys_exit_cmd"},
{"Save Settings", "flowlauncher_plugin_sys_save_all_settings_cmd"},
{"Restart Flow Launcher", "flowlauncher_plugin_sys_restart_cmd"},
{"Settings", "flowlauncher_plugin_sys_setting_cmd"},
{"Reload Plugin Data", "flowlauncher_plugin_sys_reload_plugin_data_cmd"},
{"Check For Update", "flowlauncher_plugin_sys_check_for_update_cmd"},
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"}
};
}
private List<Result> Commands()
@ -139,7 +187,7 @@ namespace Flow.Launcher.Plugin.Sys
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_restart_computer_advanced"),
context.API.GetTranslation("flowlauncher_plugin_sys_restart_computer"),
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
Process.Start("shutdown", "/r /o /t 0");
@ -158,7 +206,7 @@ namespace Flow.Launcher.Plugin.Sys
context.API.GetTranslation("flowlauncher_plugin_sys_dlgtext_logoff_computer"),
context.API.GetTranslation("flowlauncher_plugin_sys_log_off"),
MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
ExitWindowsEx(EWX_LOGOFF, 0);
@ -198,7 +246,7 @@ namespace Flow.Launcher.Plugin.Sys
info.UseShellExecute = true;
ShellCommand.Execute(info);
return true;
}
},
@ -317,7 +365,7 @@ namespace Flow.Launcher.Plugin.Sys
context.API.GetTranslation(
"flowlauncher_plugin_sys_dlgtext_all_applicableplugins_reloaded")),
System.Threading.Tasks.TaskScheduler.Current);
return true;
}
},