Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 05, 2016, 12:39:47 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: FarSubScript : Google Translate.  (Read 3462 times)

verszipo

  • Member
  • Joined in 2009
  • **
  • Posts: 26
    • View Profile
    • Donate to Member
FarSubScript : Google Translate.
« on: May 27, 2013, 05:55:26 AM »
Since CZB's Google Translate script doesn't work anymore (because of the Google Api changes) I thought I'd make a script that loads the GoogleTranslate page in FARR with a given word.

So, here's my first attempt at it, I've used the NinjaWords script as a starting point :
Spoiler
Code: Javascript [Select]
  1. plugins["GoogleTranslate"] =
  2. {
  3.         version: "1.0",
  4.         lastChange: new Date(fso.GetFile(currentDirectory+"\\fsubscript.js").DateLastModified).toDateString(),
  5.         displayName: "GoogleTranslate",
  6.         directory: currentDirectory,
  7.         icon: currentDirectory + "\\favicon.ico",
  8.         aliasstr: "gt",
  9.         description: "Translate the word using Google Translate"
  10. };
  11.  
  12. (function ()
  13. {
  14.         var js = plugins["GoogleTranslate"];
  15.         var loaded = false;
  16.         var match = false;
  17.         var match2 = false;
  18.         var loadurl = "";
  19.         js.search = function search(querykey, explicit, queryraw, querynokeyword,modifierstring,triggermethod)
  20.         {
  21.                 if(!explicit) return;
  22.  
  23.                 idletime = 0;
  24.                 // first : match when a space is at the end of the string
  25.                 match = queryraw.match(new RegExp("^"+js.aliasstr+" ([a-z,A-Z,0-9]+) ([a-z,A-Z,0-9]+) (.+) $"));
  26.                 // second : match when the user presses enter
  27.                 match2= queryraw.match(new RegExp("^"+js.aliasstr+" ([a-z,A-Z,0-9]+) ([a-z,A-Z,0-9]+) (.+)$"));
  28.  
  29.                 if((match && match[1] && match[2] && match[3])||
  30.                   (triggermethod && match2 && match2[1] && match2[2] && match2[3]))
  31.                 {
  32.                         loaded = true;
  33.                         // formated HTML must be like this :
  34.                         // http://translate.google.com/#<srclng>/<destlng>/<word>
  35.                         // you can use "auto" as srclng for autodetection
  36.                         // Example : http://translate.google.com/#auto/en/word
  37.                         if (match)
  38.                                 loadurl = "http://translate.google.com/#"+match[1]+"/"+match[2]+"/"+match[3];
  39.                         else if (match2)
  40.                                 loadurl = "http://translate.google.com/#"+match2[1]+"/"+match2[2]+"/"+match2[3];
  41.                         else
  42.                                 loadurl = "";
  43.                         htmlViewURL(loadurl);
  44.                         showPleaseWait("Loading..");
  45.                 }
  46.                 else
  47.                 {
  48.                         showHTML("<b>GoogleTranslate</b>\
  49.                                 <br>Translate a given text using the Google Translate service\
  50.                                 <br><br>\
  51.                                 <br>Syntax : <b>"+js.aliasstr+" srclng destlng word</b>\
  52.                                 <br><b>srclng</b> is the source language\
  53.                                 <br><b>destlng</b> is the destination language\
  54.                                 <br><b>word</b> is the word we want to translate\
  55.                                 <br><br>Example: <b><br>"+js.aliasstr+" en de computer</b>\
  56.                                 <br> this will translate the 'computer' word from english to german\
  57.                                 <br><br>Note : for source language you can type <b>'auto'</b> and Google will try to detect the language\
  58.                         ");
  59.                 }
  60.         };
  61.         js.setStrValue = function onSetStrValue(varname,value)
  62.         {
  63.                 if(varname == "EmbeddedWb.DocumentComplete")
  64.                 {
  65.                         var pl = pluginInCharge();
  66.                         if(pl == "GoogleTranslate" && loaded)
  67.                         {
  68.                                 loaded = false;
  69.                                 // hide the "Chrome browser" advertisment
  70.                                 // hide the "Search | Images |..." toolbar
  71.                                 execSafeJS(
  72.                                         "document.getElementById('gt-bbar').style.display='none';\n\
  73.                                         document.getElementById('gb').style.display='none';\
  74.                                 ");
  75.                                 hidePleaseWait();
  76.                         }
  77.                 }
  78.         }
  79. }());


Usage :
gt source_language destination_language word <space>
or
gt source_language destination_language word <enter>

source_language can be "auto" and then Google Translate will try to detect what language it is from.

There are still some bugs in it, the most notable ones are :
- you need to press space after the word (pressing Enter doesn't do the translation)
- doesn't support multiple word translation
- sometimes the page loads without the word(s), if you delete the space and add it again then it loads correctly
- after pressing Enter at the end of the search string FARR looses focus and the editbox from GoogleTranslate page gets the focus

Note : this script requires FarSubScript, make sure you have it installed and working in FARR.

Install :
extract the content of the archive into a subfolder under the Scripts folder of FARR (for example : plugins\GoogleTranslate) and either write "goreload" in FARR (this will reload all plugins) or restart FARR.

If anybody wants to improve the script feel free to do so.

Update : now supports multiple texts in one line and you can either press space or enter after the word(s) that you want to translate.
« Last Edit: May 27, 2013, 08:23:00 AM by verszipo »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 36,406
    • View Profile
    • Mouser's Software Zone on DonationCoder.com
    • Read more about this member.
    • Donate to Member
Re: FarSubScript : Google Translate.
« Reply #1 on: May 27, 2013, 05:10:12 PM »
Thanks for sharing this!  :Thmbsup:

IT_Fan

  • Participant
  • Joined in 2012
  • *
  • Posts: 7
    • View Profile
    • Donate to Member
Re: FarSubScript : Google Translate.
« Reply #2 on: March 30, 2014, 04:59:46 PM »
Doesn't work any more, got "error: No translation found..."  :(

verszipo

  • Member
  • Joined in 2009
  • **
  • Posts: 26
    • View Profile
    • Donate to Member
Re: FarSubScript : Google Translate.
« Reply #3 on: March 31, 2014, 11:12:40 AM »
Strange, for me it still works correctly, i usually test it with "gt en fr wolf " and it displays correctly (in case you didn't figure it, I love wolfs ;) )

Could you write what version of FARR and SubScript you have ?

I for one have FARR 2.213.01 (portable) and FSubScript 1.10 and am running Win8 64 and Win8.1 64.