Hola,
Te he preparado una de las más grandes chapuzas que he escrito en mucho tiempo xDDDD (esta subido al
FTP del club porqué habia problemas con mostrar el HTML en el mensaje)
Para probarlo, métele un TEdit, un TButton, un TTimer y un TWebBrowser, y aquí la chapuza:
Código Delphi
[-]unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, mshtml, ExtCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
WebBrowser1: TWebBrowser;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
procedure Timer1Timer(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Left:=-MaxInt;
Timer1.Enabled:=False;
Timer1.Interval:=500;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
html: String;
htmlFichero: TStringList;
begin
html:='<_script type="text/javascript" src="http://www.google.com/jsapi">' +
'<_script type="text/javascript">google.load("language", "1");' +
'<_script type="text/javascript">'+
'function Traducir() { var text = "%s"; var srcLang = "es"; var dstLang = "en"; ' +
'google.language.translate(text, srcLang, dstLang, function(result) ' +
'{ if (!result.error) { var resultado = document.getElementById("result"); ' +
'resultado.innerHTML = result.translation; } else alert(result.error.message);});}' +
'';
html:=Format(html, [Edit1.Text]);
htmlFichero:=TStringList.Create;
try
htmlFichero.Text:=html;
htmlFichero.SaveToFile('tmp.html');
finally
htmlFichero.Free;
end;
WebBrowser1.Navigate(ExtractFilePath(Application.ExeName) + 'tmp.html');
end;
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
Timer1.Enabled:=True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
iall : IHTMLElement;
begin
Timer1.Enabled:=False;
if Assigned(WebBrowser1.Document) then
begin
iall:=(WebBrowser1.Document AS IHTMLDocument2).body;
if Assigned(iall) then
begin
while Assigned(iall.parentElement) do iall:=iall.parentElement;
ShowMessage(iall.outerText);
end;
end;
DeleteFile('tmp.html');
end;
end.
Lo que hace este código, es crear un fichero temporal html, donde se le mete el texto a traducir usando tu HTML (le he quitado el title, porqué sinó también me lo mostraba xD). Luego carga este HTML y captura el texto resultante del TWebBrowser xD
Si te preguntas para qué se necesita el TTimer, pues es otra chapuza... xD Ya que si el código de obtener el texto lo metia justo después de cargar la página, éste no me devolvía bien el "outerText"...
Para ocultar el TWebBrowser, como el "visible:=false" y el "Height y Width a 0" no funcionaba, pues lo he mandando a tomar viento... sacándolo de la ventana xD
Ya te digo, que es una cadena de chapuzas, que juntas realizan lo que tu querías xDDDD
Saludos.
P.D.: Si el mensaje que te devuelve es '', sube el tiempo del Timer... xD