From cbfd5bfa7e1b42257d5e8f80f7e08f52154a3598 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Fri, 14 Jul 2017 23:15:54 +0300 Subject: [PATCH] eliminate translation doc names when calling GetString() --- .../TextViewerPanel.xaml.cs | 5 +---- QuickLook/App.xaml.cs | 6 ++---- QuickLook/Helpers/TranslationHelper.cs | 12 ++++++++++-- QuickLook/Helpers/Updater.cs | 6 +++--- QuickLook/MainWindowTransparent.xaml.cs | 10 +++++----- QuickLook/Plugin/ContextObject.cs | 6 ++++-- QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs | 10 +++++----- QuickLook/TrayIconManager.cs | 8 ++++---- 8 files changed, 34 insertions(+), 29 deletions(-) diff --git a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs index d01f399..1c1607d 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.TextViewer/TextViewerPanel.xaml.cs @@ -16,7 +16,6 @@ // along with this program. If not, see . 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); } diff --git a/QuickLook/App.xaml.cs b/QuickLook/App.xaml.cs index 433aeeb..2931365 100644 --- a/QuickLook/App.xaml.cs +++ b/QuickLook/App.xaml.cs @@ -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(); diff --git a/QuickLook/Helpers/TranslationHelper.cs b/QuickLook/Helpers/TranslationHelper.cs index f5e97a2..3fbb457 100644 --- a/QuickLook/Helpers/TranslationHelper.cs +++ b/QuickLook/Helpers/TranslationHelper.cs @@ -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 FileCache = new Dictionary(); private static readonly Dictionary StringCache = new Dictionary(); - 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]; diff --git a/QuickLook/Helpers/Updater.cs b/QuickLook/Helpers/Updater.cs index 9eeae7a..b49dfe0 100644 --- a/QuickLook/Helpers/Updater.cs +++ b/QuickLook/Helpers/Updater.cs @@ -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))); } }); } diff --git a/QuickLook/MainWindowTransparent.xaml.cs b/QuickLook/MainWindowTransparent.xaml.cs index 0a25322..d8827a0 100644 --- a/QuickLook/MainWindowTransparent.xaml.cs +++ b/QuickLook/MainWindowTransparent.xaml.cs @@ -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() diff --git a/QuickLook/Plugin/ContextObject.cs b/QuickLook/Plugin/ContextObject.cs index c44cbd5..77d00a1 100644 --- a/QuickLook/Plugin/ContextObject.cs +++ b/QuickLook/Plugin/ContextObject.cs @@ -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 /// /// Get a string from translation Xml document. /// - 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()); } /// diff --git a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs index 1b0c087..25a2793 100644 --- a/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs +++ b/QuickLook/Plugin/InfoPanel/InfoPanel.xaml.cs @@ -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 diff --git a/QuickLook/TrayIconManager.cs b/QuickLook/TrayIconManager.cs index 4321b5d..3099f7d 100644 --- a/QuickLook/TrayIconManager.cs +++ b/QuickLook/TrayIconManager.cs @@ -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()) }) };