Free google translate (python in termux)

Dimlos

Well-known member
This macro allows you to use Google Translate for free, but it is limited and unreliable.
For reliability, please use the regular API.

Please see the following thread for basic configuration of Termux and Termux:Tasker.
This time pkg install nmap and pkg install android-tools are not necessary.

apt update
apt upgrade
pkg install vim
pkg install python
pip3 install googletrans==4.0.0-rc1
mkdir -p ~/.termux/tasker
cd ~/.termux/tasker

Copy translate.py to the designated location using the following procedure.
translate.py

chmod +x translate.py

Follow the steps below to give MacroDroid the permission to run Termux.

Import and execute macros.
Set parameters in local variables.

"terget_lang" specifies the target language for translation.
The source language is automatically recognized, but words that are too short will be misrecognized.

In the "text" field, enter the text you wish to translate.
When the macro is executed, the translated text will be displayed in "output".

For more information
 

Attachments

  • Macro.jpg
    Macro.jpg
    442.3 KB · Views: 24
  • Translate_Text.macro
    6.3 KB · Views: 21
  • translate_py.jpg
    translate_py.jpg
    275.4 KB · Views: 22

Dimlos

Well-known member
Everyone seems interested, but I am hesitant because Termux is CUI.
If it works, please let me know.
If you have any stumbling blocks along the way, please write to me and I will explain them additionally.
 

getuxagig

New member
Thanks! It worked, but sometimes the translation doesn't go through, getting this error:
Bash:
Traceback (most recent call last):
  File "/data/data/com.termux/files/home/.termux/tasker/translate.py", line 9, in <module>
    result = translator.translate(text, dest=terget_lang)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/site-packages/googletrans/client.py", line 219, in translate
    parsed = json.loads(data[0][2])
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.11/json/__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType

As I tried to fix the code, I got here:
where I found there are other methods for translating:
Where Animenosekai suggested this solution that proved to work reliably:
Python:
from json import loads
from requests import get
request_result = get("https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=en&tl=fr&q=Hello")
translated_text = loads(request_result.text)[0][0][0]
print(translated_text)

Finally getting this code, which I saved it as google_translate_alternative_macrodroid_termux.py:
Python:
#!/bin/env python

import sys
import re
from json import loads
from requests import get

text = sys.argv[1]
terget_lang = sys.argv[2]
request_url = f"https://translate.googleapis.com/translate_a/single?client=gtx&dt=t&sl=auto&tl={terget_lang}&q={text}"
request_result = get(request_url)
translated_text = loads(request_result.text)[0][0][0]

encoded_text = translated_text.encode(sys.stdout.encoding, errors='replace')
decoded_text = encoded_text.decode(sys.stdout.encoding)
print(decoded_text)

Here you can see the py file (saved as zip) and the macro file that I got.
1686742786153.png

This google translate API can be converted into a macro where it uses an HTTP request, removing the need for Termux, as such:
1686745411309.png
Below I've attached Test_google_translation.macro.
 

Attachments

  • Google_translate_alternative_macrodroid_termux.macro
    6.6 KB · Views: 8
  • google_translate_alternative_macrodroid_termux.zip
    490 bytes · Views: 5
  • Test_google_translation.macro
    5.3 KB · Views: 5
Last edited:

Dimlos

Well-known member
I can't say for sure since I've only tried it on one device, but if it works in your environment, that's fine.
I like Termux and will continue to use my scripts as long as there are no serious errors.
 

Dimlos

Well-known member
I think Animenosekai has analyzed what googletrans is doing internally.
I think that's great, and it's great that he was able to figure it out and make it possible to complete it with an HTTP request.

But I have tested my script about 50 times with no errors.
During development, I often got errors when I didn't pass parameters correctly...
 
Top