My programs for Opera

Update google-translate.js.

google-translate.js
Sample of menu: menu_bbcode_english.ini.
Button: Translate to English

Go to Page,"javascript:ujs_google_translate('auto|en')",,,"Window Mail Compose Icon"


* Google now supports automatic language detection. Therefore you can use universal button or hotkey for translation from any language to English.
* First start more fast.
* Works in Opera 8-10



About installation UserJS written here.

Block javascript pop-upsCompatibility with Opera 9.5

Comments

Vague Rantinvagrante Friday, June 6, 2008 12:50:59 PM

Apologies for being annoying, but I know nothing of Javascript and I've been trying to modify the button version of this script for hours with no notable successes. I've managed to change the name and icon, but more important to me is that I'd like to use only the URL translating (that is, ignore what I have selected and translate the referring URL) and to have the translation performed in the same tab as the original page. If these are as trivial as I expected them to be before I started trying to implement them, it'd be great if you could comment with the amended code; otherwise I'll just make do with this, thanks for the effort either way.

EDIT: OK, so I'm probably going about it completely the wrong way, but here's what I have so far:
<a class="button" title="Autotranslate" href="opera:/button/Go%20to%20page,%20"javascript:location.href='http://209.85.171.104/translate_c?hl=en&sl=auto&tl=en&u='+escape(document.location);",,,"Panel%20Notes"">Autotranslate</a>

However, for some reason the Google frame still appears, even though the link generated appears to be identical to the one Google generates for removing the frame. Perhaps it's a referrer thing? The Firefox translator extension doesn't display the frame when translating, so I'm not sure. I actually only went looking for a translator button because I found this extension while using the work computers, which run Firefox, and decided I wanted a similar functionality. I think I've almost got it, just need a bit more help.

EDIT2: Never mind, I've figured it out! Here's the code if anyone else is interested in my pointless downgrade of your version:
<a class="button" title="Autotranslate" href="opera:/button/Go%20to%20page,%20"javascript:location.href='http://209.85.171.104/translate_p?hl=en&sl=auto&tl=en&u='+escape(document.location);",,,"Panel%20Notes"">Autotranslate</a>

Or as a button: Autotranslate

It was simply a matter of changing translate_c to translate_p. Google redirects it to translate_c anyway, but the frame's not there, so whatever, I'm not complaining. Anyway, in short, this version uses the Panel Notes icon (which might look weird if you use Notes, I have that panel removed though, so the icon is free), shows the translation in the same tab, and removes the translation frame. As you might guess, I eventually started from scratch with simpler JS, thanks to my inexperience, but all credit for inspiration goes to you. Thank you.

Unregistered user Monday, June 16, 2008 12:42:44 PM

Anonymous writes: Thanks pal, worked in 9.5 and really useful. But EDIT2 buttton is not working on 9.5 Also can we make translate window bigger, it's very small I think.

A.RuzanovLex1 Monday, June 16, 2008 1:13:38 PM

But EDIT2 buttton is not working on 9.5


What's it?

Also can we make translate window bigger


A size depends on the amount of text. You can change it.
{h = 15; w = 20; if(txtLen > 2000){h = 50; w = 66}else if(txtLen > 250){h = 25; w = 33};

h - height in percent from Opera window.
w - width in percent from Opera window.
txtLen - number of symbols in translation.

Misty Blueseemille Tuesday, June 17, 2008 3:11:16 AM

it works well~~
Thanks very much.

nizamx Tuesday, June 17, 2008 10:15:15 AM

Thanks now I can resize the window.

I mean this button in 9.5 opens a page with an error:

Autotranslate

A.RuzanovLex1 Tuesday, June 17, 2008 10:44:48 AM

nizamx
Paranoiacs work in my.opera.com team smile . They do blocking a word "javascript" in links.
Go to page, "javascript:location.href='http://209.85.171.104/translate_p?hl=en&sl=auto&tl=en&u='+escape(document.location);",,,"Panel Notes"

nizamx Tuesday, June 17, 2008 7:56:03 PM

I added the button I selected some russian text and new page opened saying translating then gave this message:

Translation from English into English is not supported.

Also in 9.5 can't add custom icon from http://nontroppo.org/tools/buttonmaker/ in fact icon appears fine on that site but on Opera it always notes icon.

A.RuzanovLex1 Wednesday, June 18, 2008 12:16:05 PM

I selected some russian text


Where? Google translates majority, but not all of pages.

http://nontroppo.org/tools/buttonmaker/


Works for me.

Adam Dziuraadas Wednesday, June 18, 2008 2:27:02 PM

Please add to your post button links:
Translate to French
Translate to Polish
Translate to Deutsch
...

smile

nizamx Wednesday, June 18, 2008 5:15:50 PM

Well sorry, now it works but this button does not translate selected text but the whole page while your first button translates selected text in a window. Loading the whole page takes quite longer time considering I want to translate selected sentences only.

A.RuzanovLex1 Wednesday, June 18, 2008 5:49:30 PM

nizamx
Yes, Vague_Rant was needed exactly it. If you needed translations of selected text, use my button.

adas
«javascript:google_translate('auto|de')», etc.

Adam Dziuraadas Wednesday, June 18, 2008 5:54:59 PM

Lex1: I know... But will better when you add to post. Then post will interesting for user non-english too...

Leonardo Alassialeoalassia Thursday, June 19, 2008 7:01:37 PM

http://lexi.ucoz.ru is not working!

A.RuzanovLex1 Friday, June 20, 2008 7:49:24 AM

Works already.

Leonardo Alassialeoalassia Friday, June 20, 2008 9:35:49 PM

thanks! it is great! I see a big improvement.

Unregistered user Tuesday, June 24, 2008 6:24:12 PM

The_blur writes: Thank you for making this great update. I love being able to translate little snippets of text with the click of a button. The button works as a charm and can be easily modified to translate to any language other than English. I was able to create a keyboard shortcut to transalte to English (Alt + E) and to translate to Spanish (Alt + S). Again, thank you very much for this update.

Leonardo Alassialeoalassia Tuesday, June 24, 2008 8:39:53 PM

Hi the_blur, how can you do that? (shortcuts)

Unregistered user Thursday, June 26, 2008 1:20:52 AM

The_Blur writes: leoalassia, I'm sure you know how to create your own keyboard shortcuts already but if not, simply follow these instructions http://my.opera.com/Tamil/blog/add-modify-keyboard-shortcut Once you're there, assign a key and as an action, enter the following (which is the same as the button): Go to page, "javascript:if(typeof google_translate=='function'){google_translate('auto|en')}else{void(window.open('http://translate.google.com/translate'+(document.selection.createRange().text?'_t?text='+document.selection.createRange().text:'?u='+escape(location.href))+'&hl=en&langpair=auto|en&tbb=1'+(document.characterSet?'&ie='+document.characterSet:'')));}" If you would like translate to some other language such as spanish, then just change anything that says "en" to "es". Then to use it, simply highlight the text you want translated and press the shortcut key you assigned to it.

Leonardo Alassialeoalassia Thursday, June 26, 2008 12:16:58 PM

thanks! I didn't know I could add javascript code to a shortcut!
thanks for your help!

nizamx Tuesday, August 19, 2008 8:28:45 PM

Version 1.6.14 solved several bugs such as auto go bottom of page while translating, thanks for the update.

Leonardo Alassialeoalassia Friday, August 22, 2008 3:45:07 AM

hi lex, I want to know where I have to add the code if I want to translate a text that it's a link too (because the context menu is different)

A.RuzanovLex1 Friday, August 22, 2008 8:27:52 AM

leoalassia
[Link Selection Popup Menu]

Leonardo Alassialeoalassia Tuesday, August 26, 2008 6:12:15 PM

I have another issue. If the text that I want to translate is into a frame, it doesn't work. (I'm talking about <frame> tags)

A.RuzanovLex1 Tuesday, August 26, 2008 8:51:37 PM

frame


Yes, it well known problem for any js-buttons. You can use allow-ini-variables.js and button:
Button, "Translate to English"="Go to Page,"javascript:void(inivar='%t')",,,"Window Mail Compose Icon" & Go to Page,"javascript:ujs_google_translate('auto|en')" "

Leonardo Alassialeoalassia Wednesday, August 27, 2008 4:21:01 AM

I'm sorry, but I did what you said and it doesn't works for me...
I've analized your code because the function's name in my google-translate.js is not ujs_google_translate (it's just google_translate), but I fixed it with your last update, so I don't know where I'm wrong

A.RuzanovLex1 Wednesday, August 27, 2008 4:15:02 PM

Yes, you are right, it works only for iframe. I did small fix for this bug.

Leonardo Alassialeoalassia Wednesday, August 27, 2008 6:15:50 PM

thanks! now it works, but I want the frame object (your 'f' var) into the same page (not open a new window), so I've made this add to your code:

line 87:
else if(t && location.hostname != 'translate.google.com' && document.body instanceof HTMLFrameSetElement){
...
...
document.frames[1].document.documentElement.appendChild(f);
}

so it works fine, but as you can see I'm getting the frame[1] because I know that this frame is what I want... bigsmile

A.RuzanovLex1 Thursday, August 28, 2008 2:22:10 PM

Fine idea. I added similar check of text selection for frames in script.

Leonardo Alassialeoalassia Sunday, September 7, 2008 12:38:18 AM

Hi Lex, I have a problem in Opera 9.60 RC 1, when the text contains this character: ' there is an error in javascript because it expects ;

I am using "javascript:inivar='%t';ujs_google_translate('auto|es')"

so it seems that %t is not escaped

A.RuzanovLex1 Sunday, September 7, 2008 9:42:51 AM

I have a problem in Opera 9.60 RC 1, when the text contains this character: ' there is an error in javascript because it expects ;


Yes. allow-ini-variables.js exists for correction of problem with quotes ('") and backslash (\) in Opera variables.

A.RuzanovLex1 Sunday, September 7, 2008 9:52:08 AM

But you must use two commands:
Go to Page,"javascript:void(inivar='%t')",,, "l_english" & Go to Page,"javascript:ujs_google_translate('auto|en')""

Leonardo Alassialeoalassia Sunday, September 7, 2008 3:59:52 PM

yes, I was using only one command, everything in the same line...p

arnyq Sunday, October 19, 2008 5:29:32 PM

I wonder, if it'll be faster and/or at all possible to show ONLY (imported from Google page) translated text in a small popup window rather than opening the whole Google Translate page in a separate Tab, when clicking Translate Button? If hard to implement, can the Google Translate window be open as a small popup window with its address hidden?

A.RuzanovLex1 Sunday, October 19, 2008 6:17:08 PM

If exists selected text, then shows small window, otherwise translated all page.

arnyq Sunday, October 19, 2008 10:49:16 PM

Your button always opens in my browser a full Google Translate Tab with all its interface - instead of a small window with Translated Text ONLY, and nothing else in the window. The window should be left open & reusable, if a user wants to translate another text later.

A.RuzanovLex1 Monday, October 20, 2008 9:09:23 AM

arnyq
Install UserJS smile

arnyq Tuesday, October 21, 2008 8:28:02 PM

Installed UserJS. Now your small window is looking good, but overlaps selected text a bit on a laptop screen. Sorry, I can't add a picture here - can I? How I can edit your UserJS to move it a bit down and to the right?

Also, can you consider adding a couple of options to your UserJS and Button:

- make dropdown Button to select a translation service:

A) PROMT
a) Translate (RU) to EN
b) Translate (EN) to RU

B) Google
a) Translate to EN
b) Translate to RU

Google returns a lot of translation and language detection errors.

Also, how to add this upgrade to Menu? Will a separate PROMT JS do better - they slow browser performance a lot. PROMT might be slow at times, but it's the best for RU.

PROMT offers several lexicon dictionaries. "General" is default, so I wonder, how to add Lexicon Choice to the Button in a compact way?

I'd expect Google to autoselect the right language & lexicon based on the source page content, if it's address sent to Google with selected text. I doubt, they offer it via website UI, but is there a way to add this via query?

A.RuzanovLex1 Wednesday, October 22, 2008 12:57:25 PM

Now your small window is looking good, but overlaps selected text a bit


This «small window» can be dragged wink

PROMT


Last version (1.6.18) of the script supports PROMT.
From my extended menu
[JS Translate Menu]
Item, "Translate on Russian"="Go to Page,"javascript:if(typeof ujs_google_translate=='function'){ujs_google_translate('auto|ru')}else{void(window.open('http://translate.google.com/translate'+(document.selection.createRange().text?'_t?text='+document.selection.createRange().text:'?u='+escape(location.href))+'&hl=ru&langpair=auto|ru&tbb=1'+(document.characterSet?'&ie='+document.characterSet:'')));}",,, "l_russian""
Item, "Translate on English"="Go to Page,"javascript:if(typeof ujs_google_translate=='function'){ujs_google_translate('auto|en')}else{void(window.open('http://translate.google.com/translate'+(document.selection.createRange().text?'_t?text='+document.selection.createRange().text:'?u='+escape(location.href))+'&hl=ru&langpair=auto|en&tbb=1'+(document.characterSet?'&ie='+document.characterSet:'')));}",,, "l_english""
Submenu, "Translate with PROMT", JS Translate Menu Addon="Show popup menu,,,, "Window Mail Compose Icon""

; PROMT
[JS Translate Menu Addon]
Item, "English to Russian"="Go to Page,"javascript:ujs_google_translate('er');",,, "l_russian""
Item, "German to Russian"="Go to Page,"javascript:ujs_google_translate('gr');",,, "l_russian""
Item, "French to Russian"="Go to Page,"javascript:ujs_google_translate('fr');",,, "l_russian""
Item, "Italian to Russian"="Go to Page,"javascript:ujs_google_translate('ir');",,, "l_russian""
Item, "Spanish to Russian"="Go to Page,"javascript:ujs_google_translate('sr');",,, "l_russian""
--------------------1
Item, "Russian to English"="Go to Page,"javascript:ujs_google_translate('re');",,, "l_english""
Item, "Russian to German"="Go to Page,"javascript:ujs_google_translate('rg');",,, "l_german""
Item, "Russian to French"="Go to Page,"javascript:ujs_google_translate('rf');",,, "l_french""
Item, "Russian to Spanish"="Go to Page,"javascript:ujs_google_translate('rs');",,, "l_spanish""

[Document Popup Menu]
Include, JS Translate Menu

[Hotclick Popup Menu]
Include, JS Translate Menu

nizamx Wednesday, October 22, 2008 2:19:06 PM

I installed latest version but how to use extended menu. Where we should put this ini file?

arnyq Wednesday, October 22, 2008 4:26:16 PM

This «small window» can be dragged. faint


It doesn't answer my question. I keep dragging it all the time.scared

Thanks for adding Promt support! yes
How about the rest of my questions:
- Lexicon choice in Promt
- dropdown Button for Google / Promt
- adding "selected text" site's address to Google Translate query for translation accuracy. penguin

A.RuzanovLex1 Thursday, October 23, 2008 9:54:24 AM

nizamx
In folder \profile\menu (see path in Help->About) and select menu in Opera preferences.
It menu in Russian, English version will later.

arnyq

It doesn't answer my question


Screenshot please.

dropdown Button


«Button9, "Translate"="Show hidden popup menu, "JS Translate Menu"»

arnyq Thursday, October 23, 2008 4:03:18 PM

lex1

Screenshot please

Here we go.

A.RuzanovLex1 Thursday, October 23, 2008 5:10:44 PM

nizamx
English version my extended menu.

arnyq

http://img233.imageshack.us/my.php?image=googletranslateau2.png


It is absolute normally. Position of window depends on position last click of mouse. You can do a window lower, with replacing
toolbar.style.top = (opera && opera.lastClickY < mY) ? opera.lastClickY : mY;

on
toolbar.style.top = (opera && opera.lastClickY+10 < mY) ? opera.lastClickY+10 : mY;


Lexicon choice in Promt


Not supported with used version of online translator.

site's address to Google Translate


Afaik, Google don't support it.

arnyq Thursday, October 23, 2008 5:21:07 PM

Yes, +10 is great! How to move the window a bit to the right if I want to?smile
Also, the window should jump above the selected text, if there is insufficient space to show it below the text without overlapping it. Now it doesn't always happen.

Lexicon Choice in Promt.bigeyes

A.RuzanovLex1 Thursday, October 23, 2008 7:34:57 PM

How to move the window a bit to the right


Replace
toolbar.style.left = (opera && opera.lastClickX < mX+halfWidth) ? (opera.lastClickX > halfWidth ? (opera.lastClickX-halfWidth) : 0) : mX;

on
toolbar.style.left = (opera && opera.lastClickX+10 < mX+halfWidth) ? (opera.lastClickX+10 > halfWidth ? (opera.lastClickX+10-halfWidth) : 0) : mX;


Lexicon Choice in Promt


I use wap.translate.ru. It work more fast.

nizamx Thursday, October 23, 2008 8:27:11 PM

Thanks I added code to my menu successfully thank you. Now where to put "dropdown Button". I created it as new button from opera button maker, can't produce any result. Does this button for your translator window if so how to add it?

nizamx Friday, October 31, 2008 4:22:04 PM

How can I add a Prompt button? The button doesn't work with button maker.

Go to Page,"javascript:void(inivar='%t')",,, "l_english" & Go to Page,"javascript:ujs_google_translate('auto|en')""


A.RuzanovLex1 Friday, October 31, 2008 9:10:22 PM

Russian to English (PROMT)

I created it as new button from opera button maker, can't produce any result


Or you use other name for the section in menu, or yours the button maker not good wink

nizamx Saturday, November 1, 2008 12:53:23 PM

This one works, thanks.
By the way is it possible to create context menu or button for Babylon?

Vague Rantinvagrante Monday, November 10, 2008 12:56:33 PM

Hi folks; looks like my button stopped working; I did a quick fix for it, so if anybody is still interested in it, here's the button:

Autotranslate

EDIT: Although it looks like the comment field is stripping out my javascript, so the name won't appear as Autotranslate. It should still work, however.

Write a comment

New comments have been disabled for this post.