Skip to content

Commit

Permalink
[PT Run] [Program plugin] Add localized name (#19149)
Browse files Browse the repository at this point in the history
* create common localization class

* add loc name to prog plugin

* fixes

* Tool tip fixes and comments

* cleanup and highlight fix

* change

* Improvements

* Add GetLocalizedPath()

* smal code improvements
  • Loading branch information
htcfreek committed Jul 20, 2022
1 parent 28751d2 commit e11bafc
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .github/actions/spell-check/expect.txt
Expand Up @@ -460,6 +460,7 @@ DNLEN
Dns
doctype
DONOTROUND
DONTRESOLVEDLLREFERENCES
DONTVALIDATEPATH
dotnet
DPICHANGED
Expand Down Expand Up @@ -1081,6 +1082,7 @@ lmcons
LMEM
LMENU
lnk
LOADLIBRARYASDATAFILE
LOADSTRING
LOBYTE
LOCALAPPDATA
Expand Down
Expand Up @@ -19,6 +19,7 @@
using Wox.Infrastructure;
using Wox.Infrastructure.FileSystemHelper;
using Wox.Plugin;
using Wox.Plugin.Common;
using Wox.Plugin.Logger;
using DirectoryWrapper = Wox.Infrastructure.FileSystemHelper.DirectoryWrapper;

Expand Down Expand Up @@ -46,6 +47,9 @@ public class Win32Program : IProgram

public string LnkResolvedExecutableName { get; set; }

// Localized name based on windows display language
public string LocalizedName { get; set; } = string.Empty;

public string ParentDirectory { get; set; }

public string ExecutableName { get; set; }
Expand Down Expand Up @@ -97,10 +101,11 @@ public enum ApplicationType
private int Score(string query)
{
var nameMatch = StringMatcher.FuzzySearch(query, Name);
var locNameMatch = StringMatcher.FuzzySearch(query, LocalizedName);
var descriptionMatch = StringMatcher.FuzzySearch(query, Description);
var executableNameMatch = StringMatcher.FuzzySearch(query, ExecutableName);
var lnkResolvedExecutableNameMatch = StringMatcher.FuzzySearch(query, LnkResolvedExecutableName);
var score = new[] { nameMatch.Score, descriptionMatch.Score / 2, executableNameMatch.Score, lnkResolvedExecutableNameMatch.Score }.Max();
var score = new[] { nameMatch.Score, locNameMatch.Score, descriptionMatch.Score / 2, executableNameMatch.Score, lnkResolvedExecutableNameMatch.Score }.Max();
return score;
}

Expand Down Expand Up @@ -221,7 +226,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)
var result = new Result
{
// To set the title for the result to always be the name of the application
Title = Name,
Title = !string.IsNullOrEmpty(LocalizedName) ? LocalizedName : Name,
SubTitle = GetSubtitle(),
IcoPath = IcoPath,
Score = score,
Expand All @@ -237,7 +242,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)
},
};

result.TitleHighlightData = StringMatcher.FuzzySearch(query, Name).MatchData;
result.TitleHighlightData = StringMatcher.FuzzySearch(query, result.Title).MatchData;

// Using CurrentCulture since this is user facing
var toolTipTitle = string.Format(CultureInfo.CurrentCulture, "{0}: {1}", Properties.Resources.powertoys_run_plugin_program_file_name, result.Title);
Expand Down Expand Up @@ -370,6 +375,9 @@ private static Win32Program CreateWin32Program(string path)
ExecutableName = Path.GetFileName(path),
IcoPath = path,

// Localized name based on windows display language
LocalizedName = ShellLocalization.GetLocalizedName(path),

// Using InvariantCulture since this is user facing
FullPath = path.ToLowerInvariant(),
UniqueIdentifier = path,
Expand Down
93 changes: 93 additions & 0 deletions src/modules/launcher/Wox.Plugin/Common/ShellLocalization.cs
@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace Wox.Plugin.Common
{
/// <summary>
/// Class to get localized name of shell items like 'My computer'. The localization is based on the 'windows display language'.
/// Reused code from https://stackoverflow.com/questions/41423491/how-to-get-localized-name-of-known-folder for the method <see cref="GetLocalizedName"/>
/// </summary>
public static class ShellLocalization
{
internal const uint DONTRESOLVEDLLREFERENCES = 0x00000001;
internal const uint LOADLIBRARYASDATAFILE = 0x00000002;

[DllImport("shell32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode)]
internal static extern int SHGetLocalizedName(string pszPath, StringBuilder pszResModule, ref int cch, out int pidsRes);

[DllImport("user32.dll", EntryPoint = "LoadStringW", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode)]
internal static extern int LoadString(IntPtr hModule, int resourceID, StringBuilder resourceValue, int len);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, EntryPoint = "LoadLibraryExW")]
internal static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags);

[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern int FreeLibrary(IntPtr hModule);

[DllImport("kernel32.dll", EntryPoint = "ExpandEnvironmentStringsW", CharSet = CharSet.Unicode, ExactSpelling = true)]
internal static extern uint ExpandEnvironmentStrings(string lpSrc, StringBuilder lpDst, int nSize);

/// <summary>
/// Returns the localized name of a shell item.
/// </summary>
/// <param name="path">Path to the shell item (e. g. shortcut 'File Explorer.lnk').</param>
/// <returns>The localized name as string or <see cref="string.Empty"/>.</returns>
public static string GetLocalizedName(string path)
{
StringBuilder resourcePath = new StringBuilder(1024);
StringBuilder localizedName = new StringBuilder(1024);
int len, id;
len = resourcePath.Capacity;

// If there is no resource to localize a file name the method returns a non zero value.
if (SHGetLocalizedName(path, resourcePath, ref len, out id) == 0)
{
_ = ExpandEnvironmentStrings(resourcePath.ToString(), resourcePath, resourcePath.Capacity);
IntPtr hMod = LoadLibraryEx(resourcePath.ToString(), IntPtr.Zero, DONTRESOLVEDLLREFERENCES | LOADLIBRARYASDATAFILE);
if (hMod != IntPtr.Zero)
{
if (LoadString(hMod, id, localizedName, localizedName.Capacity) != 0)
{
string lString = localizedName.ToString();
_ = FreeLibrary(hMod);
return lString;
}

_ = FreeLibrary(hMod);
}
}

return string.Empty;
}

/// <summary>
/// This method returns the localized path to a shell item (folder or file)
/// </summary>
/// <param name="path">The path to localize</param>
/// <returns>The localized path or the original path if localized version is not available</returns>
public static string GetLocalizedPath(string path)
{
path = Environment.ExpandEnvironmentVariables(path);
string ext = Path.GetExtension(path);
var pathParts = path.Split("\\");
string[] locPath = new string[pathParts.Length];

for (int i = 0; i < pathParts.Length; i++)
{
int iElements = i + 1;
string lName = GetLocalizedName(string.Join("\\", pathParts[..iElements]));
locPath[i] = !string.IsNullOrEmpty(lName) ? lName : pathParts[i];
}

string newPath = string.Join("\\", locPath);
newPath = !newPath.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase) ? newPath + ext : newPath;

return newPath;
}
}
}

0 comments on commit e11bafc

Please sign in to comment.