2022-07-20 16:11:33 +02:00
|
|
|
|
// 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;
|
2023-07-26 18:48:05 +03:00
|
|
|
|
using System.Collections.Concurrent;
|
2022-07-20 16:11:33 +02:00
|
|
|
|
using System.IO;
|
2023-02-13 17:30:18 +01:00
|
|
|
|
using Wox.Plugin.Common.Interfaces;
|
|
|
|
|
|
using Wox.Plugin.Common.Win32;
|
2022-07-20 16:11:33 +02:00
|
|
|
|
|
|
|
|
|
|
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'.
|
|
|
|
|
|
/// </summary>
|
2023-02-13 17:30:18 +01:00
|
|
|
|
public class ShellLocalization
|
2022-07-20 16:11:33 +02:00
|
|
|
|
{
|
2023-02-13 17:30:18 +01:00
|
|
|
|
// Cache for already localized names. This makes localization of already localized string faster.
|
2023-07-26 18:48:05 +03:00
|
|
|
|
private ConcurrentDictionary<string, string> _localizationCache = new ConcurrentDictionary<string, string>();
|
2022-07-20 16:11:33 +02:00
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2023-02-13 17:30:18 +01:00
|
|
|
|
public string GetLocalizedName(string path)
|
2022-07-20 16:11:33 +02:00
|
|
|
|
{
|
2023-07-26 18:48:05 +03:00
|
|
|
|
string lowerInvariantPath = path.ToLowerInvariant();
|
|
|
|
|
|
|
2023-02-13 17:30:18 +01:00
|
|
|
|
// Checking cache if path is already localized
|
2023-07-26 18:48:05 +03:00
|
|
|
|
if (_localizationCache.TryGetValue(lowerInvariantPath, out string value))
|
2023-02-13 17:30:18 +01:00
|
|
|
|
{
|
2023-03-16 15:51:31 +01:00
|
|
|
|
return value;
|
2023-02-13 17:30:18 +01:00
|
|
|
|
}
|
2022-07-20 16:11:33 +02:00
|
|
|
|
|
2023-02-13 17:30:18 +01:00
|
|
|
|
Guid shellItemType = ShellItemTypeConstants.ShellItemGuid;
|
|
|
|
|
|
int retCode = NativeMethods.SHCreateItemFromParsingName(path, IntPtr.Zero, ref shellItemType, out IShellItem shellItem);
|
|
|
|
|
|
if (retCode != 0)
|
2022-07-20 16:11:33 +02:00
|
|
|
|
{
|
2023-02-13 17:30:18 +01:00
|
|
|
|
return string.Empty;
|
|
|
|
|
|
}
|
2022-07-20 16:11:33 +02:00
|
|
|
|
|
2023-02-13 17:30:18 +01:00
|
|
|
|
shellItem.GetDisplayName(SIGDN.NORMALDISPLAY, out string filename);
|
|
|
|
|
|
|
2023-07-26 18:48:05 +03:00
|
|
|
|
_ = _localizationCache.TryAdd(lowerInvariantPath, filename);
|
2022-07-20 16:11:33 +02:00
|
|
|
|
|
2023-02-13 17:30:18 +01:00
|
|
|
|
return filename;
|
2022-07-20 16:11:33 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2023-02-13 17:30:18 +01:00
|
|
|
|
public string GetLocalizedPath(string path)
|
2022-07-20 16:11:33 +02:00
|
|
|
|
{
|
|
|
|
|
|
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++)
|
|
|
|
|
|
{
|
2023-02-13 17:30:18 +01:00
|
|
|
|
if (i == 0 && pathParts[i].EndsWith(':'))
|
|
|
|
|
|
{
|
|
|
|
|
|
// Skip the drive letter.
|
|
|
|
|
|
locPath[0] = pathParts[0];
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Localize path.
|
2022-07-20 16:11:33 +02:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|