Compare commits

...

7 Commits

Author SHA1 Message Date
35ffe9e14e
docs: update README 2021-12-24 15:04:37 +08:00
427ad80fc8
feat: Swap Language 2021-12-24 14:55:05 +08:00
snsd0805
3ebedbc727
Merge pull request #1 from eeeXun/sound
feat: add sound supporting
2021-12-23 13:16:02 +08:00
eeeXun
d849958898 feat: add sound supporting 2021-12-23 10:03:12 +08:00
snsd0805
b0932c7bbe
fix: for GitHub security alert 2021-08-29 23:40:46 +08:00
a78d7799b8
fix: change frequency of calling Form.DISPLAY() 2021-04-27 16:58:03 +08:00
1d20fbd4eb
docs: Add requirements.txt 2021-04-27 16:26:56 +08:00
5 changed files with 116 additions and 24 deletions

View File

@ -2,7 +2,7 @@
Google Translate client on your console (Unofficial)
![](Screenshot_20210424_224030.png)
![](Screenshot_20211224_150251.png)
## Warning
@ -11,6 +11,8 @@ It use Google Translate's API(free), so it may not work when you send too many r
**It just a practice for npyscreen, this respository may not update any more.**
## Usage
- Install packages
- `pip install -r requirements.txt`
- Change Language Settings: `vim config.json`
- You can find Language Code in [ISO-639-1](https://en.wikipedia.org/wiki/ISO_639-1) or [Google Support](https://cloud.google.com/translate/docs/languages) or `LanguageCode.csv`
@ -25,10 +27,14 @@ It use Google Translate's API(free), so it may not work when you send too many r
- `python main.py`
## Controls
- Send Request: `ALT + ENTER`
- Delete all input: `CTRL + D`
- Exit: `Ctrl + Q`
- Select Language: `Ctrl + S`
- Select other Widget: `TAB`/`Shift+TAB`
- Select: `ENTER`
- General
- Send Request: `ALT + ENTER`
- Swap Language: `CTRL + T`
- Delete all input: `CTRL + D`
- Exit: `Ctrl + Q`
- Select Language: `Ctrl + S`
- Select other Widget: `TAB`/`Shift+TAB`
- Select: `ENTER`
- Sound
- Play Sound on left: `CTRL + K`
- Play Sound on right: `CTRL + L`

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

23
Sound.py Normal file
View File

@ -0,0 +1,23 @@
from urllib.request import urlopen
from urllib.parse import quote
from pygame import mixer
import time
class Sound:
def __init__(self):
self.url = "https://translate.google.com.vn/translate_tts?ie=UTF-8&q={}&tl={}&client=tw-ob"
self.cache_file = "/tmp/translate.mp3"
mixer.init()
def play(self, message, language):
source = urlopen(self.url.format(quote(message), language))
# write to file
with open(self.cache_file, "wb") as fp:
fp.write(source.read())
# play sound
mixer.music.load(self.cache_file)
mixer.music.play()
while mixer.music.get_busy():
time.sleep(0.1)

View File

@ -1,9 +1,45 @@
import npyscreen
import curses
from Sound import Sound
from TUI.Box import EditBox
readme = '''
This is an unofficial Google Translate client.
It use Google Translate's API(free), so it may not work when you send too many requests.
It just a practice for npyscreen, this respository may not update any more.
- General
- ^Q : Quit
- ALT + ENTER : Search
- CTRL + T : Swap language
- CTRL + D : Delete all input
- CTRL + S : Select Language
-
- Sound
- CTRL + K : Play left sound
- CTRL + L : Play right sound
'''
class MainForm(npyscreen.FormBaseNew):
def while_waiting(self):
if not self.lock:
self.DISPLAY()
else:
self.lock = True
def create(self):
self.keypress_timeout = 5
# lock is for INPUT's DISPLAY() updatting
self.lock = False
def inputUpdate():
self.lock = False
# set event handler
event_handlers = {
@ -18,6 +54,15 @@ class MainForm(npyscreen.FormBaseNew):
# select language
"^S": self.change_language,
# play sound on the left window
"^K": self.play_left,
# play sound on the right window
"^L": self.play_right,
# reverse language
"^T": self.reverse_language
}
self.add_handlers(event_handlers)
@ -32,7 +77,7 @@ class MainForm(npyscreen.FormBaseNew):
# avoid some language input error
# ex. Chinese
self.input.entry_widget.when_value_edited = lambda: self.DISPLAY()
self.input.entry_widget.when_value_edited = inputUpdate
self.output = self.add(EditBox, name="Output (to)", footer=self.parentApp.translator.outputLanguage,
max_width=x//2-5, max_height=y//3,
@ -44,20 +89,7 @@ class MainForm(npyscreen.FormBaseNew):
self.readme = self.add(EditBox, name="README",
max_width=x-5, max_height=y//3*2-6,
relx=3, rely=y//3+4,
value='''
This is an unofficial Google Translate client.
It use Google Translate's API(free), so it may not work when you send too many requests.
It just a practice for npyscreen, this respository may not update any more.
- ^Q : Quit
- ALT + ENTER : Search
- CTRL + D : Delete all input
- CTRL + S : Select Language
''',
value=readme,
editable=False
)
@ -113,7 +145,34 @@ class MainForm(npyscreen.FormBaseNew):
# Though npyscreen's documentation mention that we should avoid using DISPLAY() function
# I can't display Chinese or Japanese,etc correctly when I didn't use this function.
self.DISPLAY()
def play_left(self, event):
if self.input.value != "":
message = self.input.value
language = self.parentApp.translator.fr
Sound().play(message, language)
def play_right(self, event):
if self.output.value != "":
message = self.output.value
language = self.parentApp.translator.to
Sound().play(message, language)
def reverse_language(self, event):
translator = self.parentApp.translator
translator.to, translator.fr = translator.fr, translator.to
translator.inputLanguage, translator.outputLanguage = translator.outputLanguage, translator.inputLanguage
self.input.value, self.output.value = self.output.value, self.input.value
self.input.footer = translator.inputLanguage
self.input.update()
self.output.footer = translator.outputLanguage
self.output.update()
def remove_text(self, event):
self.input.value = ""
self.input.update()

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
npyscreen==4.10.5
requests==2.25.1
urllib3>=1.26.5
pygame