MODULE InfoWeb;

	(*
	-- Translate Website with Bing or Google
	--  Helmut Zinn   
	
	Test Website
	http://forum.oberoncore.ru/viewtopic.php?f=116&t=2752
	http%3A%2F%2Fforum.oberoncore.ru%2Fviewtopic.php%3Ff%3D116%26t%3D2752
	*)

	IMPORT
		Dialog, StdLog, Strings, TextCmds, TextControllers;

	CONST
		bingTranslate = "http://www.microsofttranslator.com/bv.aspx?from=&to=en&a=";
		googleTranslate = "https://translate.google.com/translate?hl=en&sl=auto&tl=en&u=";

	VAR
		translator*: INTEGER;

	PROCEDURE Convert (VAR url: ARRAY OF CHAR);
		VAR
			i: INTEGER;
	BEGIN
		IF translator > 0 THEN
			StdLog.String(url); StdLog.Ln;
			FOR i := LEN(url$) - 1 TO 0 BY - 1 DO
				CASE url[i] OF
				| '&': Strings.Replace(url, i, 1, '%26');
				| '/': Strings.Replace(url, i, 1, '%2F');
				| ':': Strings.Replace(url, i, 1, '%3A');
				| '=': Strings.Replace(url, i, 1, '%3D');
				| '?': Strings.Replace(url, i, 1, '%3F');
				ELSE
				END;
			END;
			CASE translator OF
			| 1: url := bingTranslate + url$;
			| 2: url := googleTranslate + url$;
			ELSE
			END;
		END;
	END Convert;

	PROCEDURE Start* (IN url: ARRAY OF CHAR);
		VAR 
			s: ARRAY 256 OF CHAR;
	BEGIN
		s := url$;
		IF s # '' THEN
			Convert(s);
			Dialog.OpenExternal(s$);
		END;
	END Start;

	PROCEDURE ShowWebsite*;
	BEGIN
		Start(TextCmds.find.find);
	END ShowWebsite;

	PROCEDURE ShowOberonCoreForum*;
	BEGIN
		Dialog.OpenExternal('http://forum.oberoncore.ru/viewforum.php?f=116');
		translator := 2;
		Start('http://forum.oberoncore.ru/viewforum.php?f=116');
		translator := 0;
	END ShowOberonCoreForum;

	PROCEDURE ExecuteSelection*;
		VAR 
			c: TextControllers.Controller;
	BEGIN
		c := TextControllers.Focus();
		IF (c # NIL) & c.HasSelection() THEN
			TextCmds.InitFindDialog;
			IF TextCmds.find.find[0] <= ' ' THEN
				(* skip first 02X, tab & blank *)
				Strings.Extract(TextCmds.find.find, 1, 256, TextCmds.find.find)
			END;
			Dialog.Update(TextCmds.find);
		END;
		Start(TextCmds.find.find);
	END ExecuteSelection;

BEGIN
	translator := 0;
END InfoWeb.


MENU "Infodesk"
	"#Info:Translate Website..."	"*5"	"TextCmds.InitFindDialog; StdCmds.OpenToolDialog('Info/Rsrc/Web', '#Info:Translate Website')"	""
	"#Info:Show Website"	"*L"	"InfoWeb.ExecuteSelection"	"TextCmds.SelectionGuard"
END

