eliminate translation doc names when calling GetString()

This commit is contained in:
Paddy Xu
2017-07-14 23:15:54 +03:00
parent 584d56731f
commit cbfd5bfa7e
8 changed files with 34 additions and 29 deletions

View File

@@ -16,7 +16,6 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
@@ -35,9 +34,7 @@ namespace QuickLook.Plugin.TextViewer
InitializeComponent();
viewer.FontFamily =
new FontFamily(context.GetString(
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Translations.config"),
"Editor_FontFamily", failsafe: "Consolas"));
new FontFamily(context.GetString("Editor_FontFamily", failsafe: "Consolas"));
LoadFile(path);
}

View File

@@ -36,8 +36,6 @@ namespace QuickLook
public static readonly string AppFullPath = Assembly.GetExecutingAssembly().Location;
public static readonly string AppPath = Path.GetDirectoryName(AppFullPath);
internal static readonly string Translations = Path.Combine(AppPath, @"Translations.config");
private bool _isFirstInstance;
private Mutex _isRunning;
@@ -64,7 +62,7 @@ namespace QuickLook
RemoteCallShowPreview(e);
// second instance: duplicate
else
MessageBox.Show(TranslationHelper.GetString(Translations, "APP_SECOND"));
MessageBox.Show(TranslationHelper.GetString("APP_SECOND"));
Shutdown();
return;
@@ -87,7 +85,7 @@ namespace QuickLook
TrayIconManager.GetInstance();
if (!e.Args.Contains("/autorun") && !IsUWP)
TrayIconManager.GetInstance()
.ShowNotification("", TranslationHelper.GetString(Translations, "APP_START"));
.ShowNotification("", TranslationHelper.GetString("APP_START"));
if (e.Args.Contains("/first"))
AutoStartupHelper.CreateAutorunShortcut();

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Xml.XPath;
namespace QuickLook.Helpers
@@ -13,8 +15,14 @@ namespace QuickLook.Helpers
private static readonly Dictionary<string, XPathNavigator> FileCache = new Dictionary<string, XPathNavigator>();
private static readonly Dictionary<string, string> StringCache = new Dictionary<string, string>();
public static string GetString(string file, string id, CultureInfo locale = null, string failsafe = null)
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetString(string id, string file = null, CultureInfo locale = null, string failsafe = null,
Assembly calling = null)
{
if (file == null)
file = Path.Combine(Path.GetDirectoryName((calling ?? Assembly.GetCallingAssembly()).Location),
"Translations.config");
if (!File.Exists(file))
return failsafe ?? id;
@@ -44,7 +52,7 @@ namespace QuickLook.Helpers
private static string GetStringFromXml(XPathNavigator nav, string id, CultureInfo locale)
{
var cacheKey = $"{locale.Name}::{id}";
var cacheKey = $"{nav.BaseURI.GetHashCode()}::{locale.Name}::{id}";
if (StringCache.ContainsKey(cacheKey))
return StringCache[cacheKey];

View File

@@ -53,7 +53,7 @@ namespace QuickLook.Helpers
if (!silent)
Application.Current.Dispatcher.Invoke(
() => TrayIconManager.GetInstance().ShowNotification("",
TranslationHelper.GetString(App.Translations, "Update_NoUpdate")));
TranslationHelper.GetString("Update_NoUpdate")));
return;
}
@@ -67,7 +67,7 @@ namespace QuickLook.Helpers
{
ViewWindowManager.GetInstance().InvokeViewer(changeLogPath);
TrayIconManager.GetInstance().ShowNotification("",
string.Format(TranslationHelper.GetString(App.Translations, "Update_Found"), nVersion),
string.Format(TranslationHelper.GetString("Update_Found"), nVersion),
clickEvent: () => Process.Start(
@"https://github.com/xupefei/QuickLook/releases/latest"));
});
@@ -77,7 +77,7 @@ namespace QuickLook.Helpers
Debug.WriteLine(e.Message);
Application.Current.Dispatcher.Invoke(
() => TrayIconManager.GetInstance().ShowNotification("",
string.Format(TranslationHelper.GetString(App.Translations, "Update_Error"), e.Message)));
string.Format(TranslationHelper.GetString("Update_Error"), e.Message)));
}
});
}

View File

@@ -42,7 +42,7 @@ namespace QuickLook
InitializeComponent();
FontFamily =
new FontFamily(TranslationHelper.GetString(App.Translations, "UI_FontFamily", failsafe: "Segoe UI"));
new FontFamily(TranslationHelper.GetString("UI_FontFamily", failsafe: "Segoe UI"));
SourceInitialized += (sender, e) =>
{
@@ -181,13 +181,13 @@ namespace QuickLook
buttonOpenWith.Content = isExe == null
? Directory.Exists(PreviewPath)
? string.Format(TranslationHelper.GetString(App.Translations, "MW_BrowseFolder"),
? string.Format(TranslationHelper.GetString("MW_BrowseFolder"),
Path.GetFileName(PreviewPath))
: string.Format(TranslationHelper.GetString(App.Translations, "MW_Open"),
: string.Format(TranslationHelper.GetString("MW_Open"),
Path.GetFileName(PreviewPath))
: isExe == true
? string.Format(TranslationHelper.GetString(App.Translations, "MW_Run"), appFriendlyName)
: string.Format(TranslationHelper.GetString(App.Translations, "MW_OpenWith"), appFriendlyName);
? string.Format(TranslationHelper.GetString("MW_Run"), appFriendlyName)
: string.Format(TranslationHelper.GetString("MW_OpenWith"), appFriendlyName);
}
internal void BeginHide()

View File

@@ -18,6 +18,7 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using QuickLook.Annotations;
@@ -130,9 +131,10 @@ namespace QuickLook.Plugin
/// <summary>
/// Get a string from translation Xml document.
/// </summary>
public string GetString(string file, string id, CultureInfo locale = null, string failsafe = null)
[MethodImpl(MethodImplOptions.NoInlining)]
public string GetString(string id, string file = null, CultureInfo locale = null, string failsafe = null)
{
return TranslationHelper.GetString(file, id, locale, failsafe);
return TranslationHelper.GetString(id, file, locale, failsafe, Assembly.GetCallingAssembly());
}
/// <summary>

View File

@@ -62,7 +62,7 @@ namespace QuickLook.Plugin.InfoPanel
filename.Text = string.IsNullOrEmpty(name) ? path : name;
var last = File.GetLastWriteTime(path);
modDate.Text = string.Format(TranslationHelper.GetString(App.Translations, "InfoPanel_LastModified"),
modDate.Text = string.Format(TranslationHelper.GetString("InfoPanel_LastModified"),
last.ToString(CultureInfo.CurrentCulture));
Stop = false;
@@ -85,7 +85,7 @@ namespace QuickLook.Plugin.InfoPanel
Dispatcher.Invoke(() =>
{
totalSize.Text =
string.Format(TranslationHelper.GetString(App.Translations, "InfoPanel_DriveSize"),
string.Format(TranslationHelper.GetString("InfoPanel_DriveSize"),
totalSpace.ToPrettySize(2),
totalFreeSpace.ToPrettySize(2));
});
@@ -101,15 +101,15 @@ namespace QuickLook.Plugin.InfoPanel
string t;
var d = totalDirsL != 0
? string.Format(
TranslationHelper.GetString(App.Translations, "InfoPanel_Folders"), totalDirsL)
TranslationHelper.GetString("InfoPanel_Folders"), totalDirsL)
: string.Empty;
var f = totalFilesL != 0
? string.Format(
TranslationHelper.GetString(App.Translations, "InfoPanel_Files"), totalFilesL)
TranslationHelper.GetString("InfoPanel_Files"), totalFilesL)
: string.Empty;
if (!string.IsNullOrEmpty(d) && !string.IsNullOrEmpty(f))
t = string.Format(
TranslationHelper.GetString(App.Translations, "InfoPanel_FolderAndFile"), d, f);
TranslationHelper.GetString("InfoPanel_FolderAndFile"), d, f);
else if (string.IsNullOrEmpty(d) && string.IsNullOrEmpty(f))
t = string.Empty;
else

View File

@@ -29,7 +29,7 @@ namespace QuickLook
private readonly NotifyIcon _icon;
private readonly MenuItem _itemAutorun =
new MenuItem(TranslationHelper.GetString(App.Translations, "Icon_RunAtStartup"),
new MenuItem(TranslationHelper.GetString("Icon_RunAtStartup"),
(sender, e) =>
{
if (AutoStartupHelper.IsAutorun())
@@ -42,7 +42,7 @@ namespace QuickLook
{
_icon = new NotifyIcon
{
Text = string.Format(TranslationHelper.GetString(App.Translations, "Icon_ToolTip"),
Text = string.Format(TranslationHelper.GetString("Icon_ToolTip"),
Application.ProductVersion),
Icon = Resources.app,
Visible = true,
@@ -50,10 +50,10 @@ namespace QuickLook
{
new MenuItem($"v{Application.ProductVersion}") {Enabled = false},
new MenuItem("-"),
new MenuItem(TranslationHelper.GetString(App.Translations, "Icon_CheckUpdate"),
new MenuItem(TranslationHelper.GetString("Icon_CheckUpdate"),
(sender, e) => Updater.CheckForUpdates()) {Enabled = !App.IsUWP},
_itemAutorun,
new MenuItem(TranslationHelper.GetString(App.Translations, "Icon_Quit"),
new MenuItem(TranslationHelper.GetString("Icon_Quit"),
(sender, e) => System.Windows.Application.Current.Shutdown())
})
};