WeeChat – translation bar

WeeChat - Translation bar

I heard about the translate.py script before. It obviously translate text. You type :

/script install translate.py
/translate en_fr Welcome to WeeChat

and the english text is then replaced by french text. If you want to translate the text of another user, you would have to copy the text and to supply it to the translate command. You can highlight the text using the traditionnal way. You can also use the cursor mode. To enter this mode, click the middle button of the mouse on the text and press :

m

The highlighted text should now figure in the input bar. Now type :

CTRL+A

to go to the beginning of the line. Then add :

/translate en_fr

and press enter. The text of the user is now translated. How to automate this?

We need to change the commands involved when we type m in the cursor mode. First, we will clone m in order to keep this one intact. Let’s say t :

/key bindctxt cursor @chat:t hsignal:chat_quote_message;/cursor stop

Now, you should be able to type t instead of m. Let’s add /translate en_fr before the text and simulate the enter key to translate the text :

/key bindctxt cursor @chat:t /input delete_line;/input insert /translate en_fr\x20;hsignal:chat_quote_message;/input return;/cursor stop

So now click on any text, press t and you get the translation. Read it further if you want to show the translated text in a bar.

The text_item.py script

This script is awesome. I talk about it before. It allow you to create a bar item with the content of your choice. Then, you add the bar item to the bar you want.

Note that the following instructions are DANGEROUS. YOU MUST NOT TYPE ANYTHING DURING A TRANSLATE! Wait for the text to appears in the status bar before typing anything.

/script install text_item.py
/set plugins.var.python.text_item.translate "all nothing to translate"
/set weechat.bar.status.items [time],[buffer_count],[buffer_plugin],buffer_number+:+buffer_name+(buffer_modes)+{buffer_nicklist_count}+buffer_filter,[lag],[hotlist],completion,scroll,translate

then

/key bindctxt cursor @chat:t /input delete_line;/input insert /translate en_fr\x20;hsignal:chat_quote_message;/input return;/wait 1 /input move_beginning_of_line;/wait 1 /input insert /set plugins.var.python.text_item.translate all\x20;/wait 1 /input return;/cursor stop

If you want to put it in it’s own bar :

/bar add translate root bottom 1 0 translate
/key bindctxt cursor @chat:t /input delete_line;/input insert /translate en_fr\x20;hsignal:chat_quote_message;/input return;/wait 1 /input move_beginning_of_line;/wait 1 /input insert /set plugins.var.python.text_item.translate all\x20;/wait 1 /input return;/cursor stop

Update : If you want to erase the translated text from the bar once you read it, bind a key like ctrl+Z :

/key bind ctrl-Z /set plugins.var.python.text_item.translate all Nothing to translate

Enjoy!

Leave a Reply