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.