Skip to content

Commit

Permalink
Merge pull request #11 from jpavonabian/traductor
Browse files Browse the repository at this point in the history
Refactorización de código
  • Loading branch information
metalalchemist authored Sep 5, 2023
2 parents 5769877 + 80c33e0 commit fda362f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
9 changes: 8 additions & 1 deletion VeTube.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: <encoding name> -*-
import json,wx,wx.adv,threading,languageHandler,restart,translator,time,funciones,google_currency,fajustes,ajustes
import json,wx,wx.adv,threading,languageHandler,restart,time,funciones,google_currency,fajustes,ajustes
from keyboard_handler.wx_handler import WXKeyboardHandler
from playsound import playsound
from TTS.lector import configurar_tts, detect_onnx_models
Expand All @@ -12,6 +12,7 @@
from TikTokLive import TikTokLiveClient
from TikTokLive.types.events import CommentEvent, GiftEvent, DisconnectEvent, ConnectEvent,LikeEvent,JoinEvent,FollowEvent,ShareEvent,ViewerUpdateEvent,EnvelopeEvent, EmoteEvent
from menu_accesible import Accesible
from translator import TranslatorWrapper

yt=0
# revisar la configuración primero, ya que necesitamos determinar el sistema TTS a través de ella.
Expand Down Expand Up @@ -456,6 +457,7 @@ def historialItemsMenu(self, event):
self.list_box_1.PopupMenu(menu)
menu.Destroy()
def traducirMenu(self, event):
translator = TranslatorWrapper()
noti =wx.adv.NotificationMessage(_("Mensaje traducido"), _("el mensaje se ha traducido al idioma del programa y se a copiado en el portapapeles."))
noti.Show(timeout=10)
copy(translator.translate(self.list_box_1.GetString(self.list_box_1.GetSelection()),target=languageHandler.curLang[:2]))
Expand Down Expand Up @@ -578,6 +580,7 @@ def guardarLista(self, event):
def guardar(self):
global lista,config,leer
rest=False
translator = TranslatorWrapper()
config=ajustes.config
config['categorias']=[]
config['listasonidos']=[]
Expand Down Expand Up @@ -734,6 +737,7 @@ def retornarMensaje(self):
if self.list_box_1.GetCount()>0 and lista[yt][0]=='General': return self.list_box_1.GetString(self.list_box_1.GetSelection())
if lista[yt][0]!='General' and len(lista[yt])>0: return lista[yt][pos[yt]]
def mostrarMensaje(self,event=None):
translator = TranslatorWrapper()
idiomas_disponibles =[translator.LANGUAGES[k] for k in translator.LANGUAGES]
if self.dentro and self.retornarMensaje():
my_dialog = wx.Dialog(self, wx.ID_ANY, _("mensaje"))
Expand All @@ -756,6 +760,7 @@ def mostrarMensaje(self,event=None):
my_dialog.ShowModal()
def cambiarTraducir(self,event): self.traducir.SetLabel(_("&traducir el mensaje") if self.choice_idiomas.GetString(self.choice_idiomas.GetSelection()) != translator.LANGUAGES[languageHandler.curLang[:2]] else _("&Traducir mensaje al idioma del programa"))
def traducirMensaje(self,event):
translator = TranslatorWrapper()
for k in translator.LANGUAGES:
if translator.LANGUAGES[k] == self.choice_idiomas.GetStringSelection():
self.text_message.SetValue(translator.translate(self.text_message.GetValue(),target=k))
Expand Down Expand Up @@ -819,6 +824,7 @@ def favoritoTeclas(self,event):
if event.GetKeyCode() == 32: self.acceder(url=favorite[self.list_favorite.GetSelection()]['url'])
def recibirYT(self):
global lista
translator = TranslatorWrapper()
for message in self.chat:
if message['message']==None: message['message']=''
if self.dst: message['message'] = translator.translate(text=message['message'], target=self.dst)
Expand Down Expand Up @@ -1062,6 +1068,7 @@ def recibirTiktok(self):
self.chat.add_listener("disconnect", self.on_disconnect)
self.chat.run()
def recibirTwich(self):
translator = TranslatorWrapper()
for message in self.chat:
if self.dst: message['message'] = translator.translate(text=message['message'], target=self.dst)
if not message['author']['name'] in self.usuarios:
Expand Down
5 changes: 3 additions & 2 deletions ajustes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import wx,languageHandler,fajustes
from google_currency import CODES
from translator import LANGUAGES
from translator import TranslatorWrapper
from accessible_output2.outputs import sapi5
from TTS.lector import configurar_tts, detect_onnx_models
from TTS.list_voices import piper_list_voices, install_piper_voice
Expand All @@ -19,6 +19,7 @@ class configuracionDialog(wx.Dialog):
def __init__(self, parent):
global config, lista_voces, prueba_piper
# idioma:
translator = TranslatorWrapper()
languageHandler.setLanguage(config['idioma'])
idiomas = languageHandler.getAvailableLanguages()
langs = []
Expand All @@ -30,7 +31,7 @@ def __init__(self, parent):
idiomas_disponibles = [""]
monedas=[_('Por defecto')]
for k in CODES: monedas.append(f'{CODES[k]}, ({k})')
for k in LANGUAGES: idiomas_disponibles.append(LANGUAGES[k])
for k in translator.LANGUAGES: idiomas_disponibles.append(translator.LANGUAGES[k])
# voces:
if config['sistemaTTS'] == "piper":
if not lista_voces_piper is None:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ git+https://github.com/metalalchemist/google-currency
git+https://github.com/accessibleapps/accessible_output2
git+https://github.com/rmcpantoja/googletrans.git
numpy
onnxruntime==1.14.1
onnxruntime
git+https://github.com/mush42/espeak-phonemizer-windows
sounddevice
18 changes: 8 additions & 10 deletions translator.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
# -*- coding: utf-8 -*-
from googletrans import Translator, LANGUAGES

class TranslatorWrapper:
def __init__(self):
self.translator = Translator()

# create a single translator instance
# see https://github.com/ssut/py-googletrans/issues/234
t = None
def translate(self, text="", target="en"):
return self.translator.translate(text, dest=target).text

def translate(text="", target="en"):
global t
if t == None:
t = Translator()
vars = dict(text=text, dest=target)
return t.translate(**vars).text
@property
def LANGUAGES(self):
return LANGUAGES

0 comments on commit fda362f

Please sign in to comment.