Google Translator Python

1 - A propos de Googletrans

Googletrans est une bibliothèque python Compatible avec Python 3.6+ gratuite et illimitée implémentant l'API Google Translate. Elle utilise l'API Google Translate Ajax pour communiquer avec les méthodes de détection et de traduction d'un texte. Pour plus de détails, reportez-vous à la documentation officielle de l'API Google Translate: https://py-googletrans.readthedocs.io/en/latest/

2 - Installation de Googletrans





L'installation du module googletrans peut  d'une façon assez rappide et simple via l'utilitaire pip:

pip install google_trans_new

3 - Liste des langages supportés par Googletrans

Pour obtenir la liste des langages supportés par l'API Googletrans, il suffit d'importer le module googletrans et de passer à l'attribut: LANGUAGES

import googletrans
print (googletrans.LANGUAGES)

Ce qui affiche à la sortie:


{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'}

4 - Utilisation basique de googletrans

4.1 - Détection de la langue d'un texte

Pour détecter la langue d'un texte, googletrans utilise la méthode detect():

4.2 - Traduction d'une langue à une

#coding: utf-8
from google_trans_new import google_translator

texte = "J'apprends à coder"

trans = google_translator()

dt = trans.detect(texte)
print(dt) # Affiche: ['fr', 'french']

autre

Pour traduire un texte avec googletrans d'une langue à une autre, on utilise la méthode translate() de l'objet Translator tout en indiquant la langue source et la langue de destination:

#coding: utf-8
from google_trans_new import google_translator

texte = "J'apprends à coder en Python"

# Création d'un objet translator
trans = google_translator()

# traduction du texte du français à l'anglais
translated = trans.translate(texte, lang_src = 'fr', lang_tgt = 'en')

print(translated) # affiche: I learn to code in Python 

Ce qui affiche à la sortie:

I am learning to code in Python

5 - Application: création d'un système de traduction graphique





Maintenant on peut appliquer ce qu'on a appris dans les paragraphes précédents pour créer un système de traduction graphique basé sur la bibliothèque tkinter et ses widgets:

  1. liste combobox pour sélectionner  la langue d'entrée et la langue de sorite
  2. widget texte pour créer les champs de saisie
  3. widget label pour créer les étiquettes

python-googletrans-module

Système de traduction en Python
# coding: utf-8 
#-------------------------------------------------------------
# Younes Derfoufi 
# chanel : https://www.youtube.com/user/InformatiquesFacile
# Site   : https://www.tresfacile.net/
#--------------------------------------------------------------
# Vous devez installer le module de traduction de google
#--------------------------------------------------------------
# pip install google_trans_new
#--------------------------------------------------------------
from tkinter import *
from tkinter import ttk
from google_trans_new import google_translator

# définir les variables globales à utiliser
source  = ""
destination = ""
t = ""

def comboAction(event):  
    global source
    global destination
    source = combo1.get()  
    destination = combo2.get()
    
    
def Traduct(event):
    
    trans  = google_translator()
    global t
    t = ""
    t = T1.get("1.0" , END)
    
    translated = trans.translate(t, lang_src = source, lang_tgt = destination)
    T2.delete('1.0', END)
    T2.insert(END , translated)
    


root = Tk()
root.geometry("800x300")

#-------------------------------
# Création de la liste combobox
#-------------------------------
labelChooseLang = Label(root, text = "Choose language source") 
labelChooseLang.place(x = 20 , y = 50)

labelLangTraduct = Label(root, text = "Destination language") 
labelLangTraduct.place(x = 430 , y = 50)


# Liste des valeurs d'option de la combobox
languages =['fr' , 'en' , 'es' , 'ar']

# Création des listes combobox
combo1 = ttk.Combobox(root, values = languages )
combo1.place(x = 230 , y = 50)
# Définir l'élément qui s'affiche par défaut
combo1.current(0)
# Associé une bind action à la liste combo
combo1.bind("<<ComboboxSelected>>", comboAction)

combo2 = ttk.Combobox(root, values = languages)
combo2.place(x = 590 , y = 50)
# Définir l'élément qui s'affiche par défaut
combo2.current(0)
# Associé une bind action à la liste combo
combo2.bind("<<ComboboxSelected>>", comboAction)


T1 = Text(root )
T1.place(x = 20 , y = 100 , width = 400 , height = 150)
T1.bind("<Return>" , Traduct)

T2 = Text(root)
T2.place(x = 430 , y = 100 , width = 350 , height = 150)

root.mainloop()

 

 

Younes Derfoufi
CRMEF OUJDA

1 thought on “Google Translator Python

Leave a Reply