Home » Extra libraries, Code snippets, applications etc. » Applications created with U++ » Google Translator
| Google Translator [message #20392] |
Mon, 16 March 2009 13:06  |
|
|
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   |
|
|
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   |
|
|
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   |
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   |
|
|
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   |
 |
forlano
Messages: 1239 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 #21184 is a reply to message #21180] |
Thu, 07 May 2009 21:09   |
|
|
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   |
|
|
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 #21475 is a reply to message #21471] |
Fri, 22 May 2009 08:09   |
|
|
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 #21508 is a reply to message #21507] |
Sat, 23 May 2009 13:29   |
|
|
| 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 #21580 is a reply to message #21565] |
Thu, 28 May 2009 10:10   |
|
|
| cbpporter wrote on Wed, 27 May 2009 10:17 | 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.
|
I was released on the forum. and every time when I will change this I will release this.
I would like to put this in bazaar, in case if exist who interested in this!
Thank you for suggestion!
|
|
|
|
|
|
|
|
| Re: Google Translator [message #21700 is a reply to message #21698] |
Mon, 01 June 2009 12:59   |
|
|
| koldo wrote on Mon, 01 June 2009 13:13 | Hello tojocky
GoogleTranslator works well for me without proxy, but with proxy it does not.
..............
Perhaps this problem is not due to GoogleTranslator but U++ http client. I cannot answer about this.
Best regards
Koldo
|
I use not default port and proxy address and everything works fine!
Add:
Maybe you didn't check option "Use HTTP proxy" on the connection tab in options?
[Updated on: Mon, 01 June 2009 13:03] Report message to a moderator
|
|
|
|
|
|
| Re: Google Translator [message #21705 is a reply to message #21704] |
Mon, 01 June 2009 15:20   |
|
|
| koldo wrote on Mon, 01 June 2009 14:57 | Hello tojocky
The "Use HTTP proxy" option is checked.
.............
Any ideas?
Best regards
Koldo
|
Maybe in proxy server setting is:
1. username and password. I didn't implemented yet. But if is need I will add for you!
2. filter by specific protocols (ex: Mozila, IE or other)
Over Ideas I have not.
Ion Lupascu (tojocky)
|
|
|
|
|
|
| Re: Google Translator [message #23847 is a reply to message #21707] |
Thu, 26 November 2009 10:48   |
|
|
| koldo wrote on Mon, 01 June 2009 17:42 | Hello Ion
I do not have any idea about proxy, but perhaps it is a matter of username and password. If you can implement it I will try it.
Best regards
Koldo
|
Hello Koldo,
Sorry for my delay.
I was finished with new release of GoogleTranslator.
I was attached the new release here. In evening I will upload in svn repository.
Please test user name and password. I didn't tested this.
Change log:
- User name and password for proxy setting (in option form)
- [Ctrl]+[C]+[C] shortcut works for linux too(thanks to Mirek). To show notify message of translatet text need to apply the patch from andrei_natanael(full address:http://www.ultimatepp.org/forum/index.php?t=msg& goto=23834&#msg_23823)
- New languages for translate (total 80 languagies)
- Translit method (non latin chars will write in latin chars).
- Show translation without parsing. (option: "Do not parse translated text"). This option will show original translation text as "{"sentences":[{"trans":"écrivez ici le texte à traduire","orig":"write here text for translation","translit":""}],"src":"en"}"
- Can translate from Unknown language.
[Updated on: Thu, 26 November 2009 10:51] Report message to a moderator
|
|
|
|
|
|
| Re: Google Translator [message #23857 is a reply to message #23850] |
Fri, 27 November 2009 09:53   |
|
|
| masu wrote on Thu, 26 November 2009 15:21 | Hello Ion,
thanks for the tool.
I have tried it in WinXP, compiled with MSC9.
I had to change declaration of 2nd function parameter of SetProxyAuth to String (it was int before).
What is not working correctly is translation for multi-line phrases. Translation always stops after first line.
Regards,
Matthias
|
Thank you Matthias for test.
I was attached the new version of GoogleTranslator with correction!
Can you test?
Regards,
Ion.
file deleted!
[Updated on: Fri, 27 November 2009 19:56] Report message to a moderator
|
|
|
|
|
|
| Re: Google Translator [message #23860 is a reply to message #23859] |
Fri, 27 November 2009 13:30   |
|
|
| koldo wrote on Fri, 27 November 2009 13:31 | Hello Ion
I have tried it under a proxy and I get this:
| Quote: | Error:HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
, status: 407, HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
, header: Via: 1.1 BNT44
Proxy-Authenticate: Negotiate
Proxy-Authenticate: Kerberos
Proxy-Authenticate: NTLM
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 4112
|
In configuration I have entered the same proxy data I have in Firefox, that works well.
Perhaps I can try something to try to solve this. Could you give me some advice ?
Best regards
Koldo
|
Did you check the option "Use HTTP proxy"?
regards, Ion
|
|
|
|
|
|
| Re: Google Translator [message #23862 is a reply to message #23860] |
Fri, 27 November 2009 14:16   |
 |
koldo
Messages: 3460 Registered: August 2008
|
Senior Veteran |
|
|
| tojocky wrote on Fri, 27 November 2009 13:30 |
| koldo wrote on Fri, 27 November 2009 13:31 | Hello Ion
I have tried it under a proxy and I get this:
| Quote: | Error:HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
, status: 407, HTTP/1.1 407 Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy filter is denied. )
, header: Via: 1.1 BNT44
Proxy-Authenticate: Negotiate
Proxy-Authenticate: Kerberos
Proxy-Authenticate: NTLM
Connection: Keep-Alive
Proxy-Connection: Keep-Alive
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 4112
|
In configuration I have entered the same proxy data I have in Firefox, that works well.
Perhaps I can try something to try to solve this. Could you give me some advice ?
Best regards
Koldo
|
Did you check the option "Use HTTP proxy"?
regards, Ion
|
Hello Ion
Yes. I check "Use HTTP Proxy:" and "Use HTTP Proxy password:" and I fill "Address", "port", "Username" and "Password". This data is the same I use for Internet browser Firefox I use daily.
I have also debugged the program and it does what it seems it have to do. Any additional advice ?
Best regards
Koldo
Best regards
Iñaki
|
|
|
|
| Re: Google Translator [message #23864 is a reply to message #23862] |
Fri, 27 November 2009 16:08   |
|
|
| koldo wrote on Fri, 27 November 2009 15:16 |
Hello Ion
Yes. I check "Use HTTP Proxy:" and "Use HTTP Proxy password:" and I fill "Address", "port", "Username" and "Password". This data is the same I use for Internet browser Firefox I use daily.
I have also debugged the program and it does what it seems it have to do. Any additional advice ?
Best regards
Koldo
|
Hello Koldo,
Try this:
If it is web authorization, I thing that you need first login in Firefox, leave Firefox browser opened and try to translate with GoogleTranslator without option "Use HTTP Proxy password:".
Can you upload screenshots with your firefox proxy settings and user authentication?
Regards,
Ion.
P.S. I thing that your http proxy server do not filter by browser Agent from request packet data. Now the GoogleTranslator Agent is: "Ultimate++ HTTP clientUltimate++ HTTP client":
request << "Agent: " << Nvl(agent, "Ultimate++ HTTP client") << "\r\n";
|
|
|
|
| Re: Google Translator [message #23865 is a reply to message #23864] |
Fri, 27 November 2009 19:53   |
|
|
Hello All!
I was changed a little my Google Translator.
Change log:
- in option form when I do not check option "Use HTTP Proxy" set disable proxy address, port, username, password.
Any other suggestions are welcome!
New to do to do item:
- Set in option form available languages and possibility user add new language. To find needed language in all 80 languages is too boring.
regards,
Ion.
[Updated on: Fri, 27 November 2009 19:54] Report message to a moderator
|
|
|
|
|
|
| Re: Google Translator [message #23885 is a reply to message #23877] |
Tue, 01 December 2009 08:27   |
 |
mirek
Messages: 14291 Registered: November 2005
|
Ultimate Member |
|
|
While trying to compile to check the reported DocEdit bug:
u:\temp.src\googletranslator\GoogleTranslator.h(32) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversio
(password parameter needs to be String)
[Updated on: Tue, 01 December 2009 08:29] Report message to a moderator
|
|
|
|
| Re: Google Translator [message #23972 is a reply to message #23885] |
Sun, 13 December 2009 10:40   |
|
|
| luzr wrote on Tue, 01 December 2009 09:27 | While trying to compile to check the reported DocEdit bug:
u:\temp.src\googletranslator\GoogleTranslator.h(32) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversio
(password parameter needs to be String)
|
Thank you Mirek for found error.
Now I updated the latest version to svn! I tested your commend. it seems I already corrected this!
|
|
|
|
| Re: Google Translator [message #24125 is a reply to message #23877] |
Sun, 27 December 2009 18:37   |
|
|
| koldo wrote on Mon, 30 November 2009 09:50 | Hello Ion
Probably the problem does not come from Google Translator but from the U++ http support. Perhaps my proxy is tricky in any way so that Firefox is enough smart to pass it but not U++.
Best regards
Koldo
|
Can you put screenshot of your firefox settings?
|
|
|
|
Goto Forum:
Current Time: Wed Jun 03 16:13:30 GMT+2 2026
Total time taken to generate the page: 0.01106 seconds
|