├── translate.py ├── README.md ├── copy-translate.py └── paste_and_copy.py /translate.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file has been written with by Serkan Erip (serkanerip) for translate texts via Python 3.5. 3 | Feel free to cloning, sharing, editing and committing some new examples. 4 | I have tried to explain each part basicly as I can. 5 | For communicating with me: 6 | mail: serkanerip@gmail.com 7 | github: github.com/serkanerip 8 | """ 9 | 10 | import requests 11 | 12 | class Translate(): 13 | KEY_API = 'trnsl.1.1.20160914T190012Z.bba0cb3c0d74686a.368f6665226161cc052c656b2f00af0ae1b872d1' # yandex key api for using translate api 14 | API_URL = "https://translate.yandex.net/api/v1.5/tr.json/translate?lang={}-{}&key={}" 15 | def __init__(self, word, source, target): 16 | self.source = source 17 | self.target = target 18 | self.word = word 19 | 20 | def translate(self): 21 | self.API_URL = self.API_URL.format( self.source, self.target, self.KEY_API ) 22 | payload = {'text': self.word} 23 | response = requests.post(self.API_URL, data = payload) 24 | response = response.json() 25 | if(response['code'] == 200): 26 | return response['text'][0] 27 | else: 28 | raise Exception( response ) 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## What does this application do ? 2 | This application is doing fast translation with keyboard shortcuts. 3 | ## Which os do you support ? 4 | Just only linux, windows and mac os is coming soon. 5 | ## How does it work ? 6 | This app gets current copied text from the system clipboard and then translates the text via yandex-api and shows translation like a notification.Immediately after translation, clipboard (original text) value is changed with translated text so you can do control + V to paste it. By using this application you do not need to open new web pages and copy paste around. 7 | 8 | ## Requirements 9 | * Linux 10 | * Git 11 | * Python 3 12 | * Xsel or Xclip clipboard package 13 | 14 | ## Getting started 15 | 16 | ### Before Build 17 | If you don't have requirements prepared, install it. 18 | 19 | ``` 20 | sudo apt-get install xsel or sudo apt-get install xclip 21 | sudo apt-get install git 22 | sudo apt-get install python3 23 | ``` 24 | ### Github Config 25 | If you haven't already configured your git: 26 | ``` 27 | git config --global user.name "username" 28 | git config --global user.email example@email.com 29 | ``` 30 | 31 | ### Installing Application 32 | 33 | ``` 34 | git clone git@github.com:serkanerip/copy-and-translate.git 35 | sudo mv copy-and-translate /usr/share/ 36 | 37 | ``` 38 | 39 | ### Adding Keyboard Shortcut 40 | 41 | ``` 42 | Go system settings and open keyboard shortcuts 43 | Add a custom shortcut 44 | Set the command of shortcut : 45 | * python3 '/usr/share/copy-and-translate/copy-translate.py' 46 | And set your keyboard shortcut 47 | ``` 48 | 49 | ### After All Logout And Enjoy :) 50 | 51 | ![alt-tag](http://oi68.tinypic.com/2hh2n37.png) 52 | 53 | ### Some Examples 54 | 55 | ![alt tag](http://oi67.tinypic.com/23vjdw5.jpg) 56 | 57 | ![alt-tag](http://oi68.tinypic.com/20541g5.png) 58 | 59 | ![alt-tag](http://oi67.tinypic.com/24eosvr.jpg) 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /copy-translate.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file has been written with by Serkan Erip (serkanerip) for translate copied texts from clipboard and set clipboard that translated text via Python 3.5. 3 | Feel free to cloning, sharing, editing and committing some new examples. 4 | I have tried to explain each part basicly as I can. 5 | For communicating with me: 6 | mail: serkanerip@gmail.com 7 | github: github.com/serkanerip 8 | """ 9 | 10 | import paste_and_copy 11 | import translate 12 | import os 13 | import platform 14 | # variables 15 | last_copied_text = paste_and_copy.last_copy() 16 | linux_command = 'nofity-send' 17 | notify_title = 'Paste-translate app' 18 | source_language = 'en' # translate from english 19 | target_language = 'tr' # transalte to turkish 20 | 21 | def check_last_copied_text(): # if just haven't any text on the clip board send informing notification 22 | test_text = last_copied_text 23 | if(test_text.replace(' ', '') == ''): 24 | send_notification("Clipboard haven't any copied text yet") 25 | exit() 26 | 27 | def send_notification(notif): # sending translated text via linux notify-send command 28 | if(platform.system() == 'Linux'): 29 | os.system('notify-send "{}" "{}"'.format(notify_title, notif)) 30 | elif(platform.system() == 'Darwin'): 31 | if(paste_and_copy.check_is_exists_notifysender_on_mac()): 32 | os.system('terminal-notifier -title "{}" -message "{}"'.format(notify_title, notif)) 33 | 34 | 35 | if __name__ == '__main__': 36 | check_last_copied_text() 37 | translate_obj = translate.Translate(word=last_copied_text, source='en', target='tr') 38 | text_translate = translate_obj.translate() 39 | message = '\nTranslated from {} to {}\n\nTranslated text = {}\n\nTranslate = {}\n' 40 | message = message.format( source_language, target_language, last_copied_text, text_translate ) 41 | send_notification(message) 42 | paste_and_copy.paste_the_translate_to_clipboard(text_translate) # after showing translating set clipboard copy is translate text 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /paste_and_copy.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file has been written with by Serkan Erip (serkanerip) for get last copied text from clipboard and set the clipboard new text via Python 3.5. 3 | Feel free to cloning, sharing, editing and committing some new examples. 4 | I have tried to explain each part basicly as I can. 5 | For communicating with me: 6 | mail: serkanerip@gmail.com 7 | github: github.com/serkanerip 8 | """ 9 | 10 | import subprocess 11 | import platform 12 | 13 | # variables 14 | clipboards = ['xsel', 'xclip'] 15 | current_os = platform.system() 16 | check_cmd = 'where' if platform.system() == 'Windows' else 'which' 17 | osx_notify_sender_package = 'terminal-notifier' 18 | 19 | def check_clipboards(): # check system is have it clipboards packages 20 | clipboard = '' 21 | for cp in clipboards: 22 | clipboard = cp if subprocess.call([check_cmd, cp], 23 | stdout=subprocess.PIPE, 24 | stderr=subprocess.PIPE) == 0 else '' 25 | return clipboard 26 | 27 | def check_is_exists_notifysender_on_mac(): 28 | return subprocess.call([check_cmd], osx_notify_sender_package, 29 | stdout=subprocess.PIPE, 30 | shell=True, 31 | stderr=subprocess.PIPE) == 0 32 | 33 | def execute_command(command): 34 | p = subprocess.Popen(command, stdout=subprocess.PIPE, close_fds=True) 35 | result, err = p.communicate() 36 | if(err): 37 | raise Exception( err ) 38 | else: 39 | return result.decode('utf-8') 40 | 41 | def execute_pasting_the_translate(command, translated_text): 42 | p = subprocess.Popen(command, stdin=subprocess.PIPE, close_fds=True) 43 | p.communicate(input=translated_text.encode('utf-8')) 44 | p.stdin.close() 45 | 46 | def paste_the_translate_to_clipboard(translated_text): # set the translated text to clipboard last copy 47 | if (current_os == 'Linux'): 48 | return paste_onclipboard_linux(translated_text) 49 | elif (current_os == 'Darwin'): 50 | return paste_onclipboard_osx(translated_text) 51 | else: 52 | raise Exception('Operating system cannot defined.') 53 | 54 | def paste_onclipboard_linux(translated_text): 55 | clipboard = check_clipboards() 56 | if (clipboard == ''): 57 | print("You need install xclip or xsel package on your linux system") 58 | exit() 59 | else: 60 | if (clipboard == 'xsel'): 61 | return execute_pasting_the_translate(['xsel', '-b', '-i'], translated_text) 62 | else: 63 | return execute_pasting_the_translate(['xclip', '-selection', 'c'], translated_text) 64 | 65 | def get_currentcopy_linux(): 66 | clipboard = check_clipboards() 67 | if (clipboard == ''): 68 | print("You need install xclip or xsel package on your linux system") 69 | exit() 70 | else: 71 | if (clipboard == 'xsel'): 72 | return execute_command(['xsel', '-b', '-o']) 73 | else: 74 | return execute_command(['xclip', '-selection', 'c', '-o']) 75 | 76 | def paste_onclipboard_osx(translated_text): 77 | return execute_pasting_the_translate(['pbcopy', 'w']) 78 | 79 | def get_currentcopy_osx(): 80 | return execute_command(['pbpaste', 'r']) 81 | 82 | def last_copy(): 83 | if(current_os == 'Linux'): 84 | return get_currentcopy_linux() 85 | elif( current_os == 'Darwin' ): 86 | return get_currentcopy_osx() 87 | else: 88 | raise Exception('Operating system cannot defined.') 89 | 90 | 91 | 92 | --------------------------------------------------------------------------------