[PT Run] Option for system commands localization (#12939)

This commit is contained in:
Davide Giacometti
2021-09-07 00:18:14 +02:00
committed by GitHub
parent a6cca7cfb0
commit 2fe6282157
3 changed files with 62 additions and 31 deletions

View File

@@ -4,17 +4,17 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Interop; using System.Windows.Interop;
using ManagedCommon; using ManagedCommon;
using Microsoft.PowerToys.Run.Plugin.System.Properties;
using Microsoft.PowerToys.Run.Plugin.System.Win32; using Microsoft.PowerToys.Run.Plugin.System.Win32;
using Microsoft.PowerToys.Settings.UI.Library; using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin; using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.System namespace Microsoft.PowerToys.Run.Plugin.System
@@ -23,6 +23,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
{ {
private PluginInitContext _context; private PluginInitContext _context;
private const string ConfirmSystemCommands = nameof(ConfirmSystemCommands); private const string ConfirmSystemCommands = nameof(ConfirmSystemCommands);
private const string LocalizeSystemCommands = nameof(LocalizeSystemCommands);
internal const int EWXLOGOFF = 0x00000000; internal const int EWXLOGOFF = 0x00000000;
internal const int EWXSHUTDOWN = 0x00000001; internal const int EWXSHUTDOWN = 0x00000001;
@@ -35,20 +36,27 @@ namespace Microsoft.PowerToys.Run.Plugin.System
public ICommand Command { get; set; } public ICommand Command { get; set; }
public string Name => Properties.Resources.Microsoft_plugin_sys_plugin_name; public string Name => Resources.Microsoft_plugin_sys_plugin_name;
public string Description => Properties.Resources.Microsoft_plugin_sys_plugin_description; public string Description => Resources.Microsoft_plugin_sys_plugin_description;
private bool _confirmSystemCommands; private bool _confirmSystemCommands;
private bool _localizeSystemCommands;
public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>() public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{ {
new PluginAdditionalOption() new PluginAdditionalOption()
{ {
Key = ConfirmSystemCommands, Key = ConfirmSystemCommands,
DisplayLabel = Properties.Resources.confirm_system_commands, DisplayLabel = Resources.confirm_system_commands,
Value = false, Value = false,
}, },
new PluginAdditionalOption()
{
Key = LocalizeSystemCommands,
DisplayLabel = Resources.Use_localized_system_commands,
Value = true,
},
}; };
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
@@ -84,73 +92,80 @@ namespace Microsoft.PowerToys.Run.Plugin.System
private List<Result> Commands() private List<Result> Commands()
{ {
CultureInfo culture = CultureInfo.CurrentUICulture;
if (!_localizeSystemCommands)
{
culture = new CultureInfo("en-US");
}
var results = new List<Result>(); var results = new List<Result>();
results.AddRange(new[] results.AddRange(new[]
{ {
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_shutdown_computer, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_shutdown_computer), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_shutdown_computer_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_shutdown_computer_description), culture),
IcoPath = $"Images\\shutdown.{IconTheme}.png", IcoPath = $"Images\\shutdown.{IconTheme}.png",
Action = c => Action = c =>
{ {
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_shutdown_computer_confirmation, () => Helper.OpenInShell("shutdown", "/s /t 0")); return ExecuteCommand(Resources.Microsoft_plugin_sys_shutdown_computer_confirmation, () => Helper.OpenInShell("shutdown", "/s /t 0"));
}, },
}, },
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_restart_computer, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_restart_computer), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_restart_computer_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_restart_computer_description), culture),
IcoPath = $"Images\\restart.{IconTheme}.png", IcoPath = $"Images\\restart.{IconTheme}.png",
Action = c => Action = c =>
{ {
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_restart_computer_confirmation, () => Helper.OpenInShell("shutdown", "/r /t 0")); return ExecuteCommand(Resources.Microsoft_plugin_sys_restart_computer_confirmation, () => Helper.OpenInShell("shutdown", "/r /t 0"));
}, },
}, },
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_sign_out, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sign_out), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_sign_out_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sign_out_description), culture),
IcoPath = $"Images\\logoff.{IconTheme}.png", IcoPath = $"Images\\logoff.{IconTheme}.png",
Action = c => Action = c =>
{ {
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_sign_out_confirmation, () => NativeMethods.ExitWindowsEx(EWXLOGOFF, 0)); return ExecuteCommand(Resources.Microsoft_plugin_sys_sign_out_confirmation, () => NativeMethods.ExitWindowsEx(EWXLOGOFF, 0));
}, },
}, },
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_lock, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_lock), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_lock_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_lock_description), culture),
IcoPath = $"Images\\lock.{IconTheme}.png", IcoPath = $"Images\\lock.{IconTheme}.png",
Action = c => Action = c =>
{ {
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_lock_confirmation, () => NativeMethods.LockWorkStation()); return ExecuteCommand(Resources.Microsoft_plugin_sys_lock_confirmation, () => NativeMethods.LockWorkStation());
}, },
}, },
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_sleep, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sleep), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_sleep_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_sleep_description), culture),
IcoPath = $"Images\\sleep.{IconTheme}.png", IcoPath = $"Images\\sleep.{IconTheme}.png",
Action = c => Action = c =>
{ {
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_sleep_confirmation, () => NativeMethods.SetSuspendState(false, true, true)); return ExecuteCommand(Resources.Microsoft_plugin_sys_sleep_confirmation, () => NativeMethods.SetSuspendState(false, true, true));
}, },
}, },
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_hibernate, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_hibernate), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_hibernate_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_hibernate_description), culture),
IcoPath = $"Images\\sleep.{IconTheme}.png", // Icon change needed IcoPath = $"Images\\sleep.{IconTheme}.png", // Icon change needed
Action = c => Action = c =>
{ {
return ExecuteCommand(Properties.Resources.Microsoft_plugin_sys_hibernate_confirmation, () => NativeMethods.SetSuspendState(true, true, true)); return ExecuteCommand(Resources.Microsoft_plugin_sys_hibernate_confirmation, () => NativeMethods.SetSuspendState(true, true, true));
}, },
}, },
new Result new Result
{ {
Title = Properties.Resources.Microsoft_plugin_sys_emptyrecyclebin, Title = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_emptyrecyclebin), culture),
SubTitle = Properties.Resources.Microsoft_plugin_sys_emptyrecyclebin_description, SubTitle = Resources.ResourceManager.GetString(nameof(Resources.Microsoft_plugin_sys_emptyrecyclebin_description), culture),
IcoPath = $"Images\\recyclebin.{IconTheme}.png", IcoPath = $"Images\\recyclebin.{IconTheme}.png",
Action = c => Action = c =>
{ {
@@ -160,7 +175,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
var result = NativeMethods.SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0); var result = NativeMethods.SHEmptyRecycleBin(new WindowInteropHelper(Application.Current.MainWindow).Handle, 0);
if (result != (uint)NativeMethods.HRESULT.S_OK && result != 0x8000FFFF) if (result != (uint)NativeMethods.HRESULT.S_OK && result != 0x8000FFFF)
{ {
var name = "Plugin: " + Properties.Resources.Microsoft_plugin_sys_plugin_name; var name = "Plugin: " + Resources.Microsoft_plugin_sys_plugin_name;
var message = $"Error emptying recycle bin, error code: {result}\n" + var message = $"Error emptying recycle bin, error code: {result}\n" +
"please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137"; "please refer to https://msdn.microsoft.com/en-us/library/windows/desktop/aa378137";
_context.API.ShowMsg(name, message); _context.API.ShowMsg(name, message);
@@ -192,12 +207,12 @@ namespace Microsoft.PowerToys.Run.Plugin.System
public string GetTranslatedPluginDescription() public string GetTranslatedPluginDescription()
{ {
return Properties.Resources.Microsoft_plugin_sys_plugin_description; return Resources.Microsoft_plugin_sys_plugin_description;
} }
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()
{ {
return Properties.Resources.Microsoft_plugin_sys_plugin_name; return Resources.Microsoft_plugin_sys_plugin_name;
} }
private bool ExecuteCommand(string confirmationMessage, Action command) private bool ExecuteCommand(string confirmationMessage, Action command)
@@ -206,7 +221,7 @@ namespace Microsoft.PowerToys.Run.Plugin.System
{ {
MessageBoxResult messageBoxResult = MessageBox.Show( MessageBoxResult messageBoxResult = MessageBox.Show(
confirmationMessage, confirmationMessage,
Properties.Resources.Microsoft_plugin_sys_confirmation, Resources.Microsoft_plugin_sys_confirmation,
MessageBoxButton.YesNo, MessageBoxButton.YesNo,
MessageBoxImage.Warning); MessageBoxImage.Warning);
@@ -228,15 +243,19 @@ namespace Microsoft.PowerToys.Run.Plugin.System
public void UpdateSettings(PowerLauncherPluginSettings settings) public void UpdateSettings(PowerLauncherPluginSettings settings)
{ {
var confirmSystemCommands = false; var confirmSystemCommands = false;
var localizeSystemCommands = true;
if (settings != null && settings.AdditionalOptions != null) if (settings != null && settings.AdditionalOptions != null)
{ {
var option = settings.AdditionalOptions.FirstOrDefault(x => x.Key == ConfirmSystemCommands); var optionConfirm = settings.AdditionalOptions.FirstOrDefault(x => x.Key == ConfirmSystemCommands);
confirmSystemCommands = optionConfirm?.Value ?? false;
confirmSystemCommands = option == null ? false : option.Value; var optionLocalize = settings.AdditionalOptions.FirstOrDefault(x => x.Key == LocalizeSystemCommands);
localizeSystemCommands = optionLocalize?.Value ?? true;
} }
_confirmSystemCommands = confirmSystemCommands; _confirmSystemCommands = confirmSystemCommands;
_localizeSystemCommands = localizeSystemCommands;
} }
} }
} }

View File

@@ -275,5 +275,14 @@ namespace Microsoft.PowerToys.Run.Plugin.System.Properties {
return ResourceManager.GetString("Microsoft_plugin_sys_sleep_description", resourceCulture); return ResourceManager.GetString("Microsoft_plugin_sys_sleep_description", resourceCulture);
} }
} }
/// <summary>
/// Looks up a localized string similar to Use localized system commands instead of english ones.
/// </summary>
internal static string Use_localized_system_commands {
get {
return ResourceManager.GetString("Use_localized_system_commands", resourceCulture);
}
}
} }
} }

View File

@@ -212,4 +212,7 @@
<value>Put computer to sleep</value> <value>Put computer to sleep</value>
<comment>This should align to the action in Windows of a making your computer go to sleep.</comment> <comment>This should align to the action in Windows of a making your computer go to sleep.</comment>
</data> </data>
<data name="Use_localized_system_commands" xml:space="preserve">
<value>Use localized system commands instead of english ones</value>
</data>
</root> </root>