Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » Applications created with U++ » Google Translator
Google Translator [message #20392] Mon, 16 March 2009 13:06 Go to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

I was finished first variant of Google Translator in u++.
It seems nice... but i don't know how to resolve:
1. Problems with char encode, (for example when i translate from English to Russian)?
2. Set progress bar on execute http.
3. Integrate user suggestion to improve Google Translator Dictionary!

I can continue to develop:
1. Parse dictionary when i translate only an word.
2. Cash translated terms in xml file.
3. Languages icons.

I attached the source code of GoogleTranslator!

Any ideas is welcome!

[Updated on: Mon, 16 March 2009 13:19]

Report message to a moderator

Re: Google Translator [message #20419 is a reply to message #20392] Tue, 17 March 2009 22:54 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

I test Google translator on php and it work fine in utf8 encoding!
The function is (this php code is from here):
function translate($str, $from='en', $to='ru'){
	$fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
	if (!$fp) {
		trigger_error("$errstr ($errno) \n", E_USER_WARNING);
		return "";
	} else {
		$text = "text=".urlencode($str);
		$out = "POST /translate_a/t?client=t&sl=".$from."&tl=".$to." HTTP/1.1\r\n";
		$out .= "Host: www.google.com\r\n";
		$out .= "User-Agent: Mozilla/5.0\r\n";
		$out .= "Accept-Encoding: deflate\r\n";
		$out .= "Content-length: ".strlen($text)."\r\n";
		$out .= "Connection: Close\r\n\r\n";
		$out .= $text;
		
		fputs($fp, $out);
		$res = "";
		while (!feof($fp)) {
			$res .=  fgets($fp, 1024);
		}
		fclose($fp);
	}
	
	$res = explode("\r\n\r\n",$res);
	$res = explode("\r\n",$res[1]);
	return stripslashes(substr($res[1],1,-1));
}


Anybody can help what wrong I did in u++ that i get unknown char encoding?

[Updated on: Wed, 18 March 2009 11:45]

Report message to a moderator

Re: Google Translator [message #20425 is a reply to message #20419] Wed, 18 March 2009 10:00 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

The problem with char encoding is resolved! U++ works fine. The problem was in incomplete url address "&ie=utf-8&oe=utf-8";
Also i was corrected code when compile on linux (thanks to Andrei).

Remain how can I put the progress bar on execute http.

The current source is in attachment.
Re: Google Translator [message #20426 is a reply to message #20425] Wed, 18 March 2009 10:37 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Depends on how you want it to work. First you should be running the Http request in a Thread so that you can still process Gui messages.

Normally to get a progress bar on an Http request you would just pass in a Gate (the Progress class has an operator for this) that the HttpClient uses to update the progress indicator. Remember to create the Gate outside of the Http thread or it won't work properly (I think).

In your case however, I don't think the amount of data you're recieving is actually large enough to make a progress bar work properly. It will do nothing until connection is established and then update and close almost immediately. I'd suggest faking it with a timer and a looping Progress indicator. Something like:

(Add to header)
	Progress progress;
	String result;
	volatile Atomic translating;

	enum {
		TIMEID_PROGRESS = Ctrl::TIMEID_COUNT,
		TIMEID_COUNT	
	};
	
	void HttpThread(String url, String post, Gate2<int, int> _progress);
	void UpdateProgress();
	bool CheckCancel(int, int);


Source:
void GoogleTranslator::TranslateText(){
	String from = inputwindow.fromlanguagectrl.GetKey(inputwindow.fromlanguagectrl.GetIndex());
	String to = inputwindow.tolanguagectrl.GetKey(inputwindow.tolanguagectrl.GetIndex());
	//... need to add proxy
	//http_client.Proxy("");
	String url = "www.google.com/translate_a/t?client=t&sl=" + from + "&tl=" + to;
	String post = String("text=") << inputwindow.textfrom.GetData();

	progress.Reset();
	progress.SetText("Contacting Google Translator");
	progress.Title("Translating");
	progress.Set(0, 50);
	
	AtomicWrite(translating, 1);
	SetTimeCallback(-200, THISBACK(UpdateProgress), TIMEID_PROGRESS);
	Thread().Run(THISBACK3(HttpThread, url, post, THISBACK(CheckCancel)));
	while(AtomicRead(translating)) {
		ProcessEvents();
		GuiSleep(300);
	}
	KillTimeCallback(TIMEID_PROGRESS);
	progress.Close();
	textto.Set(result, CHARSET_UTF8);	
}

void GoogleTranslator::UpdateProgress()
{
	int p = progress.GetPos() + 1;
	progress.Set((p > progress.GetTotal()) ? 0 : p, progress.GetTotal());
}

bool GoogleTranslator::CheckCancel(int, int)
{
	return progress.Canceled();
}

void GoogleTranslator::HttpThread(String url, String post, Gate2<int, int> _progress)
{
	HttpClient http_client;
	
	http_client.URL(url);
	//http_client.Agent("Mozilla/5.0");
	http_client.TimeoutMsecs(5000);
	http_client.Post();
	http_client.PostData(post);

	result = http_client.ExecuteRedirect(HttpClient::DEFAULT_MAX_REDIRECT, 
						HttpClient::DEFAULT_RETRIES, _progress);

	result = Nvl(result
		,String(", error:")<<Nvl(http_client.GetError(), "")
		<<", status: "<<http_client.GetStatusCode()<<", "<<http_client.GetStatusLine()
		<<", header: "<<http_client.GetHeaders());
	AtomicWrite(translating, 0);	
}
Re: Google Translator [message #20428 is a reply to message #20426] Wed, 18 March 2009 10:55 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Thanks! Very useful example!
Maximum amount of data is 32kb. My goal is to understand how can i use progress bar! Thank you!

This example may be add in examples? From this example, new user can learn how to use http client, progress bar and a little threads.

ADD:
In attachment is source code with progress bar!

[Updated on: Wed, 18 March 2009 11:43]

Report message to a moderator

Re: Google Translator [message #21170 is a reply to message #20428] Wed, 06 May 2009 13:01 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
tojocky wrote on Wed, 18 March 2009 10:55

Thanks! Very useful example!
Maximum amount of data is 32kb. My goal is to understand how can i use progress bar! Thank you!



Hi,
I compiled your package and got the following error:

main.cpp
C:\MyApps\GoogleTranslator\main.cpp: In member function 'void GoogleTranslator::TranslateText()':
C:\MyApps\GoogleTranslator\main.cpp:80: error: no match for 'operator<<' in 'Upp::String(((const char*)"text=")) << Upp::UrlEncode(Upp::S
tring)()'
C:\upp20081\uppsrc/Web/html.h:224: note: candidates are: Upp::Htmls& Upp::operator<<(Upp::Htmls&, const char*)
C:\upp20081\uppsrc/Web/html.h:223: note: Upp::Htmls& Upp::operator<<(Upp::Htmls&, Upp::String)
C:\upp20081\uppsrc/Web/html.h:222: note: Upp::Htmls& Upp::operator<<(Upp::Htmls&, const Upp::Htmls&)
C:\upp20081\uppsrc/Web/html.h:221: note: Upp::Htmls& Upp::operator<<(Upp::Htmls&, const Upp::HtmlTag&)
C:\upp20081\uppsrc/CtrlCore/CtrlCore.h:1389: note: Upp::Ctrl* Upp::operator<<(Upp::Ctrl*, Upp::Ctrl&)
C:\upp20081\uppsrc/Core/Cbgen.h:850: note: Upp::Gate& Upp::operator<<(Upp::Gate&, Upp::Gate)
C:\upp20081\uppsrc/Core/Cbgen.h:108: note: Upp::Callback& Upp::operator<<(Upp::Callback&, Upp::Callback)
C:\upp20081\uppsrc/Core/Stream.h:611: note: Upp::Stream& Upp::operator<<(Upp::Stream&, void*)
C:\upp20081\uppsrc/Core/Stream.h:605: note: Upp::Stream& Upp::operator<<(Upp::Stream&, const void*)
C:\upp20081\uppsrc/Core/Stream.h:599: note: Upp::Stream& Upp::operator<<(Upp::Stream&, char)
C:\upp20081\uppsrc/Core/Stream.h:593: note: Upp::Stream& Upp::operator<<(Upp::Stream&, const Upp::String&)
C:\upp20081\uppsrc/Core/Stream.h:587: note: Upp::Stream& Upp::operator<<(Upp::Stream&, char*)
C:\upp20081\uppsrc/Core/Stream.h:581: note: Upp::Stream& Upp::operator<<(Upp::Stream&, const char*)
C:\upp20081\uppsrc/Core/String.h:518: note: Upp::StringBuffer& Upp::operator<<(Upp::StringBuffer&, void*)
C:\upp20081\uppsrc/Core/String.h:512: note: Upp::StringBuffer& Upp::operator<<(Upp::StringBuffer&, const void*)
C:\upp20081\uppsrc/Core/String.h:506: note: Upp::StringBuffer& Upp::operator<<(Upp::StringBuffer&, char)
C:\upp20081\uppsrc/Core/String.h:500: note: Upp::StringBuffer& Upp::operator<<(Upp::StringBuffer&, const Upp::String&)
C:\upp20081\uppsrc/Core/String.h:494: note: Upp::StringBuffer& Upp::operator<<(Upp::StringBuffer&, char*)
C:\upp20081\uppsrc/Core/String.h:488: note: Upp::StringBuffer& Upp::operator<<(Upp::StringBuffer&, const char*)
C:\upp20081\uppsrc/Core/String.h:434: note: Upp::String& Upp::operator<<(Upp::String&, void*)
C:\upp20081\uppsrc/Core/String.h:428: note: Upp::String& Upp::operator<<(Upp::String&, const void*)
C:\upp20081\uppsrc/Core/String.h:422: note: Upp::String& Upp::operator<<(Upp::String&, char)
C:\upp20081\uppsrc/Core/String.h:416: note: Upp::String& Upp::operator<<(Upp::String&, const Upp::String&)
C:\upp20081\uppsrc/Core/String.h:410: note: Upp::String& Upp::operator<<(Upp::String&, char*)
C:\upp20081\uppsrc/Core/String.h:404: note: Upp::String& Upp::operator<<(Upp::String&, const char*)
GoogleTranslator: 1 file(s) built in (0:03.34), 3344 msecs / file, duration = 3344 msecs

May it depend by the fact that I'm using the old 2008.1 and mingw?

Thanks,
Luigi
Re: Google Translator [message #21171 is a reply to message #21170] Wed, 06 May 2009 13:12 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
It's mingw. Using GCC/Mingw temporary variables are always 'const', and the << is a non-const operator.

Just change all the << operators to + and it will work (also in HttpThread)
Re: Google Translator [message #21176 is a reply to message #21171] Wed, 06 May 2009 20:22 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
mrjt wrote on Wed, 06 May 2009 13:12

It's mingw. Using GCC/Mingw temporary variables are always 'const', and the << is a non-const operator.

Just change all the << operators to + and it will work (also in HttpThread)


Thanks, now it works.
This is a very useful application much better than that offered by online site!

Luigi
Re: Google Translator [message #21180 is a reply to message #20392] Thu, 07 May 2009 01:10 Go to previous messageGo to next message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello tojocky

Nice example. I promise to use it.

One thing I have found is that if in the text to translate there are cr+lf, they are translated as '\' 'r' '\' 'n' letters instead of the cr = \r = ascii 13 and the same for \n.

Best regards
Koldo


Best regards
Iñaki
Re: Google Translator [message #21184 is a reply to message #21180] Thu, 07 May 2009 21:09 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Sorry for my delay!
I was improve a little this project with the following changes:

- Add proxy settings (address and port)
- Changed main interface.
- Add possibility for show program in tray, close in tray, minimize in tray.
- Save and restore settings
- Integrate progress bar (many thanks to "mrjt")
- Show confirmation message box when exit from application.

I was attached the latest version!

In future I would like to add:
- Possibility for send to google corrected translation by user.
- Parse translated terms and other specific characters
- Handle global shortcut events for translation, as in Abby Lingvo
- Save and restore more settings, as: language from, language to, source text, destination text and other.
- Make multilingual interface!

If anybody have other ideas or want to contribute, I would be glad!


I do not know how can I handle global events (keyboard for shortcut, mouse)?
Can anybody help me?

[Updated on: Sat, 09 May 2009 22:10]

Report message to a moderator

Re: Google Translator [message #21450 is a reply to message #21184] Thu, 21 May 2009 10:56 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Hello everyone!

I was improve a little this project with the following changes:

- Possibility for send to google corrected translation by user.
- Parse translated terms and other specific characters
- Handle global shortcut [Ctrl]+[C]+[C] events for translate copied text, as in Abby Lingvo (only for win32). In this case will translate in baloon from trayicon.
- Save and restore more settings, as: language from, language to, source text.
- Separated core google translate by GUI which named "GoogleTranslator" but demo package I named GoogleTranslatorDemo.
- Corrected errors on mingw and linux (ubuntu 9.04) GCC build
- Correct other errors.
- Added a simple console example to demonstrate google translator
#include <GoogleTranslator/GoogleTranslator.h>

using namespace Upp;

CONSOLE_APP_MAIN
{
	String str_status = "";
	GoogleTranslator g_instance;
	Cout() << "Set proxy: 172.16.0.65 , port 3128" << "\n";
	Cout() << "Translate \"translator\" word" << "\n";
	g_instance.SetProxy("172.16.0.65", 3128);
	if(g_instance.Translate("translator", String("en"), String("ro"))){
		Cout() << "Translation: " << g_instance.GetFormatedText() << "\n";
		if(g_instance.GetCorrectionText()!="translator"){
			Cout() << "Send corrected translator if not correct: \"translator\"" << "\n";
			if(g_instance.SetCorrectionText("translator", str_status))
				Cout() << str_status;
		}
	}
}


I was attached the latest version!

In future I would like to add:
- Multilingual interface!
- finish global shortcut for x11 and improve for win32.

Ballon message is only for win32, if somebody can add for x11 too I will be glad.

If anybody have other ideas or want to contribute, I would be glad!
Re: Google Translator [message #21471 is a reply to message #21450] Fri, 22 May 2009 00:36 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Excuse me for little offtopic, but I have found google translate client app is out:
http://googletranslateclient.com/googletranslateclient.exe
( discussion @ http://mokkey.habrahabr.ru/blog/60103/#comments )
so I recommend to discover it`s features and may be liberate some of them.
Re: Google Translator [message #21475 is a reply to message #21471] Fri, 22 May 2009 08:09 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Mindtraveller wrote on Fri, 22 May 2009 01:36

Excuse me for little offtopic, but I have found google translate client app is out:
http://googletranslateclient.com/googletranslateclient.exe
( discussion @ http://mokkey.habrahabr.ru/blog/60103/#comments )
so I recommend to discover it`s features and may be liberate some of them.


Very nice!

Thanks, I will do!

But the main goal is to use GoogleTranslator class in other programs as source code, not only for general translate. For ex. it can be integrated in auto translate of interface.
Re: Google Translator [message #21499 is a reply to message #21475] Sat, 23 May 2009 08:48 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Hello!

My last question about GoogleTranslator is:
Is need to add this package in bazaar or not?

Re: Google Translator [message #21507 is a reply to message #21499] Sat, 23 May 2009 12:41 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
tojocky wrote on Sat, 23 May 2009 08:48

Hello!

My last question about GoogleTranslator is:
Is need to add this package in bazaar or not?




I vote yes. I'm thinking to integrate it in my application if there are not license restriction.

Thank you,
Luigi
Re: Google Translator [message #21508 is a reply to message #21507] Sat, 23 May 2009 13:29 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

forlano wrote on Sat, 23 May 2009 13:41

tojocky wrote on Sat, 23 May 2009 08:48

Hello!

My last question about GoogleTranslator is:
Is need to add this package in bazaar or not?




I vote yes. I'm thinking to integrate it in my application if there are not license restriction.

Thank you,
Luigi


I didn't found any restriction about this!
Re: Google Translator [message #21564 is a reply to message #21508] Wed, 27 May 2009 08:40 Go to previous messageGo to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

OK, Is only one person is interested in GoogleTranslator to add in bazaar.
Mirek, is any reason to put this in bazaar?
Re: Google Translator [message #21565 is a reply to message #21564] Wed, 27 May 2009 09:17 Go to previous messageGo to next message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
It is up to you to decide if you want to keep it in bazaar or just release on the forum. If you wish to put it in bazaar and you don't have write access to it, please pm Mirek and he may grant you the right.
Re: Google Translator [message #21573 is a reply to message #20392] Wed, 27 May 2009 21:04 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hi,

I think Google Translator can be very intersting.

For example how about creating a default translation for an app (if the language is not available ) by using Google Translator facility ?? Rolling Eyes

I don't time for this right now but some other person may have it in the future.

Re: Google Translator [message #21575 is a reply to message #21573] Wed, 27 May 2009 22:03 Go to previous messageGo to previous message
koldo is currently offline  koldo
Messages: 3355
Registered: August 2008
Senior Veteran
Hello all

I also think it is interesting to have this technology available for all Upp users.

Best regards
Koldo


Best regards
Iñaki
Previous Topic: Open Source Project
Next Topic: Distance - geodesic - Vincenty - very accurate
Goto Forum:
  


Current Time: Thu Mar 28 15:09:23 CET 2024

Total time taken to generate the page: 0.01775 seconds