Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 35ffe9e14e | |||
| 427ad80fc8 | |||
|
|
3ebedbc727 | ||
|
|
d849958898 | ||
|
|
b0932c7bbe | ||
| a78d7799b8 | |||
| 1d20fbd4eb |
10
README.md
10
README.md
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Google Translate client on your console (Unofficial)
|
Google Translate client on your console (Unofficial)
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Warning
|
## 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.**
|
**It just a practice for npyscreen, this respository may not update any more.**
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
- Install packages
|
||||||
|
- `pip install -r requirements.txt`
|
||||||
|
|
||||||
- Change Language Settings: `vim config.json`
|
- 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`
|
- 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`
|
- `python main.py`
|
||||||
|
|
||||||
## Controls
|
## Controls
|
||||||
|
- General
|
||||||
- Send Request: `ALT + ENTER`
|
- Send Request: `ALT + ENTER`
|
||||||
|
- Swap Language: `CTRL + T`
|
||||||
- Delete all input: `CTRL + D`
|
- Delete all input: `CTRL + D`
|
||||||
- Exit: `Ctrl + Q`
|
- Exit: `Ctrl + Q`
|
||||||
- Select Language: `Ctrl + S`
|
- Select Language: `Ctrl + S`
|
||||||
- Select other Widget: `TAB`/`Shift+TAB`
|
- Select other Widget: `TAB`/`Shift+TAB`
|
||||||
- Select: `ENTER`
|
- Select: `ENTER`
|
||||||
|
- Sound
|
||||||
|
- Play Sound on left: `CTRL + K`
|
||||||
|
- Play Sound on right: `CTRL + L`
|
||||||
|
|||||||
BIN
Screenshot_20211224_150251.png
Normal file
BIN
Screenshot_20211224_150251.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 131 KiB |
23
Sound.py
Normal file
23
Sound.py
Normal 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)
|
||||||
@ -1,9 +1,45 @@
|
|||||||
import npyscreen
|
import npyscreen
|
||||||
import curses
|
import curses
|
||||||
|
from Sound import Sound
|
||||||
from TUI.Box import EditBox
|
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):
|
class MainForm(npyscreen.FormBaseNew):
|
||||||
|
|
||||||
|
def while_waiting(self):
|
||||||
|
if not self.lock:
|
||||||
|
self.DISPLAY()
|
||||||
|
else:
|
||||||
|
self.lock = True
|
||||||
|
|
||||||
def create(self):
|
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
|
# set event handler
|
||||||
event_handlers = {
|
event_handlers = {
|
||||||
@ -18,6 +54,15 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
|
|
||||||
# select language
|
# select language
|
||||||
"^S": self.change_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)
|
self.add_handlers(event_handlers)
|
||||||
|
|
||||||
@ -32,7 +77,7 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
|
|
||||||
# avoid some language input error
|
# avoid some language input error
|
||||||
# ex. Chinese
|
# 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,
|
self.output = self.add(EditBox, name="Output (to)", footer=self.parentApp.translator.outputLanguage,
|
||||||
max_width=x//2-5, max_height=y//3,
|
max_width=x//2-5, max_height=y//3,
|
||||||
@ -44,20 +89,7 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
self.readme = self.add(EditBox, name="README",
|
self.readme = self.add(EditBox, name="README",
|
||||||
max_width=x-5, max_height=y//3*2-6,
|
max_width=x-5, max_height=y//3*2-6,
|
||||||
relx=3, rely=y//3+4,
|
relx=3, rely=y//3+4,
|
||||||
value='''
|
value=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.
|
|
||||||
|
|
||||||
- ^Q : Quit
|
|
||||||
- ALT + ENTER : Search
|
|
||||||
- CTRL + D : Delete all input
|
|
||||||
- CTRL + S : Select Language
|
|
||||||
''',
|
|
||||||
editable=False
|
editable=False
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -114,6 +146,33 @@ class MainForm(npyscreen.FormBaseNew):
|
|||||||
# I can't display Chinese or Japanese,etc correctly when I didn't use this function.
|
# I can't display Chinese or Japanese,etc correctly when I didn't use this function.
|
||||||
self.DISPLAY()
|
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):
|
def remove_text(self, event):
|
||||||
self.input.value = ""
|
self.input.value = ""
|
||||||
self.input.update()
|
self.input.update()
|
||||||
|
|||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
npyscreen==4.10.5
|
||||||
|
requests==2.25.1
|
||||||
|
urllib3>=1.26.5
|
||||||
|
pygame
|
||||||
Loading…
Reference in New Issue
Block a user