LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-18-2023, 07:20 PM   #1
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,151
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
A small CLI google language translator


https://www.linuxquestions.org/quest...les-api-38971/

Take it and play with it. Use it. Improve it. It has minimal depends. I would not hit google too fast or too much from one IP.

You can replace urllib with requests if you want. You can eliminate json if you remark out the Pretty print json section.

It's a quick CLI interface to translate a non english post on LQ, or translate a few sentences. Also low bandwidth, unlike having a browser open with scripts running.

Let me see...

Open iftop

Open GoogTrans.py

Translate from English:
The fast brown horse is running very fast and the rider can barely hang on.

Into Spanish:
El rápido caballo marrón corre muy rápido y el jinete apenas puede sostenerse.

iftop says 3.05KB
 
Old 05-03-2023, 06:28 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Thanks!

Fewer interested multi-lingual people here than I would have thought.
 
Old 05-03-2023, 07:39 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,149

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Nah, more than that maybe - I subscribed when it was first posted, but didn't want to yank it from the zero bump.
 
Old 05-04-2023, 07:38 AM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,151

Original Poster
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
This uses jq to pretty print the json. You could eliminate the jq depend.

Goog_trans.sh
Code:
#!/usr/bin/bash

#Bash google language translator

agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:112.0) Gecko/20100101 Firefox/112.0"

echo -e "\nGoogle Translator\n"
echo -e "For language abbreviations look at: "
echo -e "https://developers.google.com/admin-sdk/directory/v1/languages\n"

read -p "Enter input language: " inLang
read -p "Enter output language: " outLang

read -p "Enter phrase to translate from "$inLang" to "$outLang" :" phrase

phrase=${phrase// /+}

url="http://translate.googleapis.com/translate_a/single?client=gtx&sl=${inLang}&tl=${outLang}&dt=t&q=${phrase}"

echo -e "\nThis is the google api url: "$url"\n"

txt=$(curl -A "$agent" "$url")

echo "$txt" | jq .
Looks like this, except colorized:

Code:
bash ./trans.sh

Google Translator

For language abbreviations look at: 
https://developers.google.com/admin-sdk/directory/v1/languages

Enter input language: en
Enter output language: es
Enter phrase to translate from en to es :Fewer interested multi-lingual people here than I would have thought.

This is the google api url: http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=es&dt=t&q=Fewer+interested+multi-lingual+people+here+than+I+would+have+thought.

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   272    0   272    0     0   2047      0 --:--:-- --:--:-- --:--:--  2076
[
  [
    [
      "Menos personas multilingües interesadas aquí de lo que hubiera pensado.",
      "Fewer interested multi-lingual people here than I would have thought.",
      null,
      null,
      3,
      null,
      null,
      [
        []
      ],
      [
        [
          [
            "03214100749bedbdf1de4331ab408186",
            "en_es_2021q4.md"
          ]
        ]
      ]
    ]
  ],
  null,
  "en",
  null,
  null,
  null,
  null,
  []
]
And that's a basic outline. Just like the python script.

The trouble with doing translations from the command line is: You need the fonts/locale available to your terminal. You can use different ones.

I have en_US.UTF-8 in my locale,

Copying and pasting from online newspapers.

Code:
Enter input language: de
Enter output language: en
Enter phrase to translate from de to en :Wir erheben personenbezogene Daten und übermitteln diese auch an Drittanbieter,

[
  [
    [
      "We collect personal data and also transfer it to third parties,",
      "Wir erheben personenbezogene Daten und übermitteln diese auch an Drittanbieter,",
      null
Code:
Enter input language: fr
Enter output language: en
Enter phrase to translate from fr to en :Le chef de l’Etat a détaillé, depuis un lycée professionnel de Saintes

[
  [
    [
      "The Head of State detailed, from a professional high school in Saintes,",
      "Le chef de lâEtat a détaillé, depuis un lycée professionnel de Saintes,",
      null,
Code:
Enter input language: pl
Enter output language: en
Enter phrase to translate from pl to en :Blisko milionowe granty z Narodowego Centrum Badań i Rozwoju dostawały pseudoprojekty, które nigdy nie miały być realizowane

[
  [
    [
      "Almost one million grants from the National Center for Research and Development were awarded to pseudo-projects that were never to be implemented",
      "Blisko milionowe granty z Narodowego Centrum BadaÅ i Rozwoju dostawaÅy pseudoprojekty, które nigdy nie miaÅy byÄ realizowane",
      null
You mileage may vary, depends on how your machine is set up.

I can translate from english into anything:
https://0x0.st/HZot.png

Not necessarily the other way around. Depends on your fonts etc.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Crow Translate: Desktop / CLI Text Translation App Using Google Translate, Yandex Translate and Bing Translator LXer Syndicated Linux News 0 05-11-2019 05:13 AM
Babylon translator Schopy Linux - Software 4 11-24-2018 04:27 AM
Language translator for skype baronobeefdip Linux - Software 1 08-08-2013 04:46 AM
A language translator for Linux yuran Linux - Software 5 01-07-2010 07:04 PM
Language translator/lookup TuxLives Linux - Software 1 09-10-2007 07:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration