DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: FarSubScript : Google Translate.  (Read 15,305 times)

verszipo

  • Member
  • Joined in 2009
  • **
  • Posts: 26
    • Plugin Coding Medal
FarSubScript : Google Translate.
« on: May 27, 2013, 10:55 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 (click to show/hide)
Code: (javascript)
plugins["GoogleTranslate"] =
{
	version: "1.0",
	lastChange: new Date(fso.GetFile(currentDirectory+"\\fsubscript.js").DateLastModified).toDateString(),
	displayName: "GoogleTranslate",
	directory: currentDirectory,
	icon: currentDirectory + "\\favicon.ico",
	aliasstr: "gt",
	description: "Translate the word using Google Translate"
};

(function ()
{
	var js = plugins["GoogleTranslate"];
	var loaded = false;
	var match = false;
	var match2 = false;
	var loadurl = "";
	js.search = function search(querykey, explicit, queryraw, querynokeyword,modifierstring,triggermethod)
	{
		if(!explicit) return;

		idletime = 0;
		// first : match when a space is at the end of the string
		match = queryraw.match(new RegExp("^"+js.aliasstr+" ([a-z,A-Z,0-9]+) ([a-z,A-Z,0-9]+) (.+) $"));
		// second : match when the user presses enter
		match2= queryraw.match(new RegExp("^"+js.aliasstr+" ([a-z,A-Z,0-9]+) ([a-z,A-Z,0-9]+) (.+)$"));

		if((match && match[1] && match[2] && match[3])||
		  (triggermethod && match2 && match2[1] && match2[2] && match2[3]))
		{
			loaded = true;
			// formated HTML must be like this :
			// http://translate.google.com/#<srclng>/<destlng>/<word>
			// you can use "auto" as srclng for autodetection
			// Example : http://translate.google.com/#auto/en/word
			if (match)
				loadurl = "http://translate.google.com/#"+match[1]+"/"+match[2]+"/"+match[3];
			else if (match2)
				loadurl = "http://translate.google.com/#"+match2[1]+"/"+match2[2]+"/"+match2[3];
			else
				loadurl = "";
			htmlViewURL(loadurl);
			showPleaseWait("Loading..");
		}
		else
		{
			showHTML("<b>GoogleTranslate</b>\
				<br>Translate a given text using the Google Translate service\
				<br><br>\
				<br>Syntax : <b>"+js.aliasstr+" srclng destlng word</b>\
				<br><b>srclng</b> is the source language\
				<br><b>destlng</b> is the destination language\
				<br><b>word</b> is the word we want to translate\
				<br><br>Example: <b><br>"+js.aliasstr+" en de computer</b>\
				<br> this will translate the 'computer' word from english to german\
				<br><br>Note : for source language you can type <b>'auto'</b> and Google will try to detect the language\
			");
		}
	};
	js.setStrValue = function onSetStrValue(varname,value)
	{
		if(varname == "EmbeddedWb.DocumentComplete")
		{
			var pl = pluginInCharge();
			if(pl == "GoogleTranslate" && loaded)
			{
				loaded = false;
				// hide the "Chrome browser" advertisment
				// hide the "Search | Images |..." toolbar
				execSafeJS(
					"document.getElementById('gt-bbar').style.display='none';\n\
					document.getElementById('gb').style.display='none';\
				");
				hidePleaseWait();
			}
		}
	}
}());

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, 01:23 PM by verszipo »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 40,931
    • Gathering Badge
    • Plugin Coding Medal
    • Participated in NANY Challenge Seventeen Times
    • Cody Award
    • Coding Snacks
    • Volunteers/Staff
Re: FarSubScript : Google Translate.
« Reply #1 on: May 27, 2013, 10:10 PM »
Thanks for sharing this!  :Thmbsup:

IT_Fan

  • Participant
  • Joined in 2012
  • *
  • Posts: 8
Re: FarSubScript : Google Translate.
« Reply #2 on: March 30, 2014, 09:59 PM »
Doesn't work any more, got "error: No translation found..."  :(

verszipo

  • Member
  • Joined in 2009
  • **
  • Posts: 26
    • Plugin Coding Medal
Re: FarSubScript : Google Translate.
« Reply #3 on: March 31, 2014, 04:12 PM »
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.

Original address: https://www.donationcoder.com/forum/index.php?topic=35033.0