├── README.md ├── Wox.Plugin.Translate ├── Images │ └── pic.png ├── packages.config ├── plugin.json ├── app.config ├── Properties │ └── AssemblyInfo.cs ├── Wox.Plugin.GoogleTranslate.csproj └── GoogleTranslatePlugin.cs ├── Wox.Plugin.GoogleTranslate.sln └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | # Wox.Plugin.GoogleTranslate 2 | 3 | use `wpm install google translate` 4 | -------------------------------------------------------------------------------- /Wox.Plugin.Translate/Images/pic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lances101/Wox.Plugin.GoogleTranslate/HEAD/Wox.Plugin.Translate/Images/pic.png -------------------------------------------------------------------------------- /Wox.Plugin.Translate/packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Wox.Plugin.Translate/plugin.json: -------------------------------------------------------------------------------- 1 | { 2 | "ID": "FD6F596596184119B027D6D81D0C2742", 3 | "ActionKeyword": "tr", 4 | "Name": "Google Translate", 5 | "Description": "Plugin that allow you to translate any words to any languages", 6 | "Author": "Armaldio, boromak", 7 | "Version": "1.0.5", 8 | "Language": "csharp", 9 | "Website": "http://www.getwox.com", 10 | "IcoPath": "Images\\pic.png", 11 | "ExecuteFileName": "Wox.Plugin.GoogleTranslate.dll" 12 | } 13 | -------------------------------------------------------------------------------- /Wox.Plugin.Translate/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Wox.Plugin.GoogleTranslate.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wox.Plugin.GoogleTranslate", "Wox.Plugin.Translate\Wox.Plugin.GoogleTranslate.csproj", "{A75CBF0D-5253-419A-B123-C52E1D0A2C95}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {A75CBF0D-5253-419A-B123-C52E1D0A2C95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {A75CBF0D-5253-419A-B123-C52E1D0A2C95}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {A75CBF0D-5253-419A-B123-C52E1D0A2C95}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {A75CBF0D-5253-419A-B123-C52E1D0A2C95}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /Wox.Plugin.Translate/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Wox.Plugin.Utils")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Wox.Plugin.Utils")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("a75cbf0d-5253-419a-b123-c52e1d0a2c95")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Wox.Plugin.Translate/Wox.Plugin.GoogleTranslate.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {A75CBF0D-5253-419A-B123-C52E1D0A2C95} 8 | Library 9 | Properties 10 | Wox.Plugin.GoogleTranslate 11 | Wox.Plugin.GoogleTranslate 12 | v4.5.2 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | TRACE;DEBUG; 22 | prompt 23 | 4 24 | AnyCPU 25 | false 26 | 27 | 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | false 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | ..\packages\Wox.Plugin.1.3.159\lib\net452\Wox.Plugin.dll 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | Always 59 | 60 | 61 | 62 | 63 | Always 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 82 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | *.sap 88 | 89 | # TFS 2012 Local Workspace 90 | $tf/ 91 | 92 | # Guidance Automation Toolkit 93 | *.gpState 94 | 95 | # ReSharper is a .NET coding add-in 96 | _ReSharper*/ 97 | *.[Rr]e[Ss]harper 98 | *.DotSettings.user 99 | 100 | # JustCode is a .NET coding add-in 101 | .JustCode 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | _NCrunch_* 111 | .*crunch*.local.xml 112 | nCrunchTemp_* 113 | 114 | # MightyMoose 115 | *.mm.* 116 | AutoTest.Net/ 117 | 118 | # Web workbench (sass) 119 | .sass-cache/ 120 | 121 | # Installshield output folder 122 | [Ee]xpress/ 123 | 124 | # DocProject is a documentation generator add-in 125 | DocProject/buildhelp/ 126 | DocProject/Help/*.HxT 127 | DocProject/Help/*.HxC 128 | DocProject/Help/*.hhc 129 | DocProject/Help/*.hhk 130 | DocProject/Help/*.hhp 131 | DocProject/Help/Html2 132 | DocProject/Help/html 133 | 134 | # Click-Once directory 135 | publish/ 136 | 137 | # Publish Web Output 138 | *.[Pp]ublish.xml 139 | *.azurePubxml 140 | # TODO: Comment the next line if you want to checkin your web deploy settings 141 | # but database connection strings (with potential passwords) will be unencrypted 142 | *.pubxml 143 | *.publishproj 144 | 145 | # NuGet Packages 146 | *.nupkg 147 | # The packages folder can be ignored because of Package Restore 148 | **/packages/* 149 | # except build/, which is used as an MSBuild target. 150 | !**/packages/build/ 151 | # Uncomment if necessary however generally it will be regenerated when needed 152 | #!**/packages/repositories.config 153 | 154 | # Windows Azure Build Output 155 | csx/ 156 | *.build.csdef 157 | 158 | # Windows Store app package directory 159 | AppPackages/ 160 | 161 | # Visual Studio cache files 162 | # files ending in .cache can be ignored 163 | *.[Cc]ache 164 | # but keep track of directories ending in .cache 165 | !*.[Cc]ache/ 166 | 167 | # Others 168 | ClientBin/ 169 | [Ss]tyle[Cc]op.* 170 | ~$* 171 | *~ 172 | *.dbmdl 173 | *.dbproj.schemaview 174 | *.pfx 175 | *.publishsettings 176 | node_modules/ 177 | orleans.codegen.cs 178 | 179 | # RIA/Silverlight projects 180 | Generated_Code/ 181 | 182 | # Backup & report files from converting an old project file 183 | # to a newer Visual Studio version. Backup files are not needed, 184 | # because we have git ;-) 185 | _UpgradeReport_Files/ 186 | Backup*/ 187 | UpgradeLog*.XML 188 | UpgradeLog*.htm 189 | 190 | # SQL Server files 191 | *.mdf 192 | *.ldf 193 | 194 | # Business Intelligence projects 195 | *.rdl.data 196 | *.bim.layout 197 | *.bim_*.settings 198 | 199 | # Microsoft Fakes 200 | FakesAssemblies/ 201 | 202 | # Node.js Tools for Visual Studio 203 | .ntvs_analysis.dat 204 | 205 | # Visual Studio 6 build log 206 | *.plg 207 | 208 | # Visual Studio 6 workspace options file 209 | *.opt 210 | 211 | # Visual Studio LightSwitch build output 212 | **/*.HTMLClient/GeneratedArtifacts 213 | **/*.DesktopClient/GeneratedArtifacts 214 | **/*.DesktopClient/ModelManifest.xml 215 | **/*.Server/GeneratedArtifacts 216 | **/*.Server/ModelManifest.xml 217 | _Pvt_Extensions 218 | sh.exe.stackdump 219 | -------------------------------------------------------------------------------- /Wox.Plugin.Translate/GoogleTranslatePlugin.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.IO; 5 | using System.Net; 6 | using System.Runtime.Caching; 7 | using System.Text; 8 | using System.Threading; 9 | using System.Web; 10 | using System.Windows; 11 | 12 | namespace Wox.Plugin.GoogleTranslate 13 | { 14 | /// 15 | /// Google Translate plugin for Wox. Parses the website, does not use the API. This is against the TOS. 16 | /// 17 | public class GoogleTranslatePlugin : IPlugin, IContextMenu 18 | { 19 | /// 20 | /// Name of the file that is checked for in the plugin's directory. 21 | /// 22 | private const string FirstRunFileName = "accepted_terms"; 23 | 24 | /// 25 | /// Identifier of the top thread. Is rewritten every time a new query is 26 | /// executed and checked by already running queries to be able to return 27 | /// without actually sending any requests. 28 | /// 29 | public static int topThreadId; 30 | 31 | /// 32 | /// Stored reference 33 | /// 34 | private PluginInitContext _context; 35 | 36 | private bool _isFirstRun; 37 | private static string _firstRunFilePath; 38 | 39 | public string[,] Languages = 40 | { 41 | {"Afrikaans", "af"}, {"Albanian", "sq"}, {"Arabic", "ar"}, 42 | {"Azerbaijani", "az"}, {"Bengali", "bn"}, {"Basque", "eu"}, 43 | {"Korean", "ko"}, {"Belarusian", "be"}, {"Bulgarian", "bg"}, 44 | {"Catalan", "ca"}, {"Chinese Simplified", "zh-CN"}, 45 | {"Chinese Traditional", "zh-TW"}, {"Croatian", "hr"}, {"Czech", "cs"}, 46 | {"Danish", "da"}, {"Dutch", "nl"}, {"English", "en"}, 47 | {"Esperanto", "eo"}, {"Estonian", "et"}, {"Filipino", "tl"}, 48 | {"Finnish", "fi"}, {"French", "fr"}, {"Galician", "gl"}, 49 | {"Georgian", "ka"}, {"German", "de"}, {"Greek", "el"}, 50 | {"Gujarati", "gu"}, {"Haitian Creole", "ht"}, {"Hebrew", "iw"}, 51 | {"Hindi", "hi"}, {"Hungarian", "hu"}, {"Icelandic", "is"}, 52 | {"Indonesian", "id"}, {"Irish", "ga"}, {"Italian", "it"}, 53 | {"Japanese", "ja"}, {"Kannada", "kn"}, {"Bengali", "bn"}, 54 | {"Latin", "la"}, {"Latvian", "lv"}, {"Lithuanian", "lt"}, 55 | {"Macedonian", "mk"}, {"Malay", "ms"}, {"Maltese", "mt"}, 56 | {"Norwegian", "no"}, {"Persian", "fa"}, {"Polish", "pl"}, 57 | {"Portuguese", "pt"}, {"Romanian", "ro"}, {"Russian", "ru"}, 58 | {"Serbian", "sr"}, {"Slovak", "sk"}, {"Slovenian", "sl"}, 59 | {"Spanish", "es"}, {"Swahili", "sw"}, {"Swedish", "sv"}, 60 | {"Tamil", "ta"}, {"Telugu", "te"}, {"Thai", "th"}, {"Turkish", "tr"}, 61 | {"Ukrainian", "uk"}, {"Urdu", "ur"}, {"Vietnamese", "vi"}, 62 | {"Welsh", "cy"}, {"Yiddish", "yi"} 63 | }; 64 | 65 | /// 66 | /// Returns a list of as the contextual menu for a specific 67 | /// 68 | /// 69 | /// The selected result. 70 | /// 71 | /// 72 | /// The . 73 | /// 74 | public List LoadContextMenus(Result selectedResult) 75 | { 76 | return new List 77 | { 78 | new Result 79 | { 80 | Title = "Copy to clipboard", 81 | IcoPath = "Images\\pic.png", 82 | Action = e => 83 | { 84 | Clipboard.SetText(selectedResult.Title); 85 | return true; 86 | } 87 | } 88 | }; 89 | } 90 | 91 | public void Init(PluginInitContext context) 92 | { 93 | _context = context; 94 | _firstRunFilePath = Path.Combine(context.CurrentPluginMetadata.PluginDirectory, FirstRunFileName); 95 | _isFirstRun = GetFirstRunStatus(_firstRunFilePath); 96 | } 97 | 98 | public List Query(Query query) 99 | { 100 | if (_isFirstRun) 101 | { 102 | var result = ShowFirstRunDialog(); 103 | if (result != MessageBoxResult.OK) 104 | { 105 | return null; 106 | } 107 | using (File.Create(_firstRunFilePath)) { } 108 | _isFirstRun = false; 109 | } 110 | topThreadId = Thread.CurrentThread.ManagedThreadId; 111 | var list = new List(); 112 | if (query.ThirdSearch == string.Empty || query.ThirdSearch == " ") 113 | { 114 | for (var index = 0; index < Languages.GetLength(0); ++index) 115 | { 116 | var str = Languages[index, 0]; 117 | var countrycode = Languages[index, 1]; 118 | list.Add( 119 | new Result 120 | { 121 | Title = str, 122 | SubTitle = countrycode, 123 | IcoPath = "Images\\pic.png", 124 | Action = e => 125 | { 126 | _context.API.ChangeQuery(query.RawQuery + countrycode + " "); 127 | return false; 128 | } 129 | }); 130 | } 131 | } 132 | else 133 | { 134 | var input = query.SecondToEndSearch.Substring(query.SecondToEndSearch.IndexOf(query.ThirdSearch)); 135 | var from = query.FirstSearch; 136 | var to = query.SecondSearch; 137 | Thread.Sleep(500); 138 | if (topThreadId != Thread.CurrentThread.ManagedThreadId) return list; 139 | var str = TranslateText(input, from, to); 140 | list.Add( 141 | new Result 142 | { 143 | Title = str, 144 | SubTitle = "from " + from + " to " + to + " : " + input, 145 | IcoPath = "Images\\pic.png", 146 | Action = e => 147 | { 148 | Process.Start( 149 | "http://www.google.com/translate_t?hl=en&ie=UTF8&text=" + input 150 | + "&langpair=" + from + "|" + to); 151 | return false; 152 | } 153 | }); 154 | } 155 | 156 | return list; 157 | } 158 | 159 | /// 160 | /// Returns true if this is the first time this plugin is used 161 | /// 162 | /// 163 | /// The . 164 | /// 165 | public bool GetFirstRunStatus(string checkFilePath) 166 | { 167 | return !File.Exists(checkFilePath); 168 | } 169 | 170 | /// 171 | /// Displays the dialog notifying the user that the plugin does not use the official Google Translate API 172 | /// 173 | private MessageBoxResult ShowFirstRunDialog() 174 | { 175 | const string titleText = "Google Translate plugin"; 176 | const string mainText = 177 | "This plugin does not use the official Google Translate API.\r\n" 178 | + "Using this plugin is against Google's Terms of Service.\r\n" 179 | + "We are not responsible for any repercussions that happen \r\n" 180 | + "due to the use of this plugin.\r\n" 181 | + "Pressing OK confirms that you acknowledge this.\r\n" 182 | + "If you do not agree with this, uninstall the plugin."; 183 | return MessageBox.Show(mainText, titleText, MessageBoxButton.OKCancel, MessageBoxImage.Warning); 184 | } 185 | 186 | /// 187 | /// Translates the using the languages passed 188 | /// in the and parameters 189 | /// by formatting a url with them and executing a query against 190 | /// Google's servers. The result is then parsed by extracting the 191 | /// translation from the HTML file 192 | /// 193 | /// 194 | /// text to translate 195 | /// 196 | /// 197 | /// language code to translate from 198 | /// 199 | /// 200 | /// language code to translate to 201 | /// 202 | /// 203 | /// resulting translation 204 | /// 205 | public string TranslateText(string input, string from, string to) 206 | { 207 | var str1 = 208 | GetPageHtml($"http://www.google.com/translate_t?hl=en&ie=UTF8&text={input}&langpair={from}|{to}"); 209 | var str2 = str1.Substring(str1.IndexOf("") + 1); 211 | return HttpUtility.HtmlDecode(str3.Substring(0, str3.IndexOf("")).Trim()); 212 | } 213 | 214 | /// 215 | /// Downloads the page located at the specified , 216 | /// returns it as a string 217 | /// 218 | /// url to download the document from 219 | /// downloaded string 220 | public string GetPageHtml(string url) 221 | { 222 | string cached_page = GetCachedPageForUrl(url); 223 | if(cached_page != null) 224 | return cached_page; 225 | var client = new WebClient {Encoding = Encoding.UTF8}; 226 | client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 227 | 228 | using (client) 229 | { 230 | try 231 | { 232 | var pageString = client.DownloadString(url); 233 | CachePage(url, pageString); 234 | return pageString; 235 | } 236 | catch 237 | { 238 | return null; 239 | } 240 | } 241 | } 242 | 243 | private void CachePage(string url, string pageString) 244 | { 245 | var cache = MemoryCache.Default; 246 | cache.Add(url, pageString, DateTimeOffset.Now.AddHours(1)); 247 | } 248 | private string GetCachedPageForUrl(string url) 249 | { 250 | var cache = MemoryCache.Default; 251 | return cache.Get(url) as string; 252 | } 253 | } 254 | } --------------------------------------------------------------------------------