├── __init__.py ├── README.md └── easygoogletranslate.py /__init__.py: -------------------------------------------------------------------------------- 1 | from .easygoogletranslate import * 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # easygoogletranslate 2 | 3 | Unofficial Google Translate API. 4 | 5 | This library does not need an api key or something else to use, it's free and simple. 6 | You can either use a string or a file to translate but the text must be equal to or less than 5000 character. 7 | You can split your text into 5000 characters to translate more. 8 | 9 | Google Translate supports 108 different languages. You can use any of them as source and target language in this application. 10 | If source language is not specified, it will detect source language automatically. 11 | This application supports multi thread translation, you can use it to translate multiple languages at once. 12 | Detailed language list can be found here: https://cloud.google.com/translate/docs/languages 13 | 14 | 15 | 16 | 19 | 20 |
17 | Don't forget to hit the star ⭐ button 18 |
21 | 22 | 23 | ## Installation: 24 | The easiest way to install easygoogletranslateis to download it from PyPI. Then you will be able to use the library. 25 | 26 | ``` 27 | pip install easygoogletranslate 28 | ``` 29 | 30 | 31 | ## Examples: 32 | 1. Specify default source and target language at beginning and use it any time. 33 | ``` 34 | from easygoogletranslate import EasyGoogleTranslate 35 | 36 | translator = EasyGoogleTranslate( 37 | source_language='en', 38 | target_language='de', 39 | timeout=10 40 | ) 41 | result = translator.translate('This is an example.') 42 | 43 | print(result) 44 | # Output: Dies ist ein Beispiel. 45 | ``` 46 | 47 | 2. Don't specify default parameters. 48 | ``` 49 | from easygoogletranslate import EasyGoogleTranslate 50 | 51 | translator = EasyGoogleTranslate() 52 | result = translator.translate('This is an example.', target_language='tr') 53 | 54 | print(result) 55 | # Output: Bu bir örnektir. 56 | ``` 57 | 58 | 3. Override default parameters. 59 | ``` 60 | from easygoogletranslate import EasyGoogleTranslate 61 | 62 | translator = EasyGoogleTranslate(target_language='tr') 63 | result = translator.translate('This is an example.', target_language='fr') 64 | 65 | print(result) 66 | # Output: Ceci est un exemple. 67 | ``` 68 | 69 | 4. Translate a text in multiple languages at once via multi-threading. 70 | ``` 71 | from easygoogletranslate import EasyGoogleTranslate 72 | 73 | translator = EasyGoogleTranslate() 74 | result = translator.translate(text='This is an example.', target_language=['tr', 'fr', 'de']) 75 | 76 | print(result) 77 | # Output: ['Bu bir örnektir.', 'Ceci est un exemple.', 'Dies ist ein Beispiel.'] 78 | ``` 79 | 80 | 5. Translate a file in multiple languages at once via multi-threading. 81 | ``` 82 | from easygoogletranslate import EasyGoogleTranslate 83 | 84 | translator = EasyGoogleTranslate() 85 | result = translator.translate_file(file_path='text.txt', target_language=['tr', 'fr', 'de']) 86 | 87 | print(result) 88 | # Output: ['Nasılsın?', 'Comment ca va?', 'Wie geht es Ihnen?'] 89 | # Text inside file: How are you? 90 | ``` 91 | 92 | ## Disclaimer 93 | This package is not an official library and is not associated with Google. This package is only developed for educational and test purposes, can be removed if desired. Do not use this package on a real life project. If you want to use a translate service on a real project use official [Google Cloud Translate](https://cloud.google.com/translate/) service. -------------------------------------------------------------------------------- /easygoogletranslate.py: -------------------------------------------------------------------------------- 1 | import concurrent.futures 2 | import requests 3 | import re 4 | import os 5 | import html 6 | import urllib.parse 7 | 8 | class EasyGoogleTranslate: 9 | 10 | ''' 11 | Unofficial Google Translate API. 12 | 13 | This library does not need an api key or something else to use, it's free and simple. 14 | You can either use a string or a file to translate but the text must be equal to or less than 5000 character. 15 | You can split your text into 5000 characters to translate more. 16 | 17 | Google Translate supports 108 different languages. You can use any of them as source and target language in this application. 18 | If source language is not specified, it will detect source language automatically. 19 | This application supports multi thread translation, you can use it to translate multiple languages at once. 20 | Detailed language list can be found here: https://cloud.google.com/translate/docs/languages 21 | 22 | 23 | Examples: 24 | #1: Specify default source and target language at beginning and use it any time. 25 | translator = GoogleTranslateRequest( 26 | source_language='en', 27 | target_language='de', 28 | timeout=10 29 | ) 30 | result = translator.translate('This is an example.') 31 | print(result) 32 | 33 | #2: Don't specify default parameters. 34 | translator = GoogleTranslateRequest() 35 | result = translator.translate('This is an example.', target_language='tr') 36 | print(result) 37 | 38 | #2: Override default parameters. 39 | translator = GoogleTranslateRequest(target_language='tr') 40 | result = translator.translate('This is an example.', target_language='fr') 41 | print(result) 42 | 43 | #4: Translate a text in multiple languages at once via multi-threading. 44 | translator = GoogleTranslateRequest() 45 | result = translator.translate(text='This is an example.', target_language=['tr', 'fr', 'de']) 46 | print(result) 47 | 48 | #5: Translate a file in multiple languages at once via multi-threading. 49 | translator = GoogleTranslateRequest() 50 | result = translator.translate_file(file_path='text.txt', target_language=['tr', 'fr', 'de']) 51 | print(result) 52 | 53 | ''' 54 | 55 | def __init__(self, source_language='auto', target_language='tr', timeout=5): 56 | self.source_language = source_language 57 | self.target_language = target_language 58 | self.timeout = timeout 59 | self.pattern = r'(?s)class="(?:t0|result-container)">(.*?)<' 60 | 61 | def make_request(self, target_language, source_language, text, timeout): 62 | escaped_text = urllib.parse.quote(text.encode('utf8')) 63 | url = 'https://translate.google.com/m?tl=%s&sl=%s&q=%s'%(target_language, source_language, escaped_text) 64 | response = requests.get(url, timeout=timeout) 65 | result = response.text.encode('utf8').decode('utf8') 66 | result = re.findall(self.pattern, result) 67 | if not result: 68 | print('\nError: Unknown error.') 69 | f = open('error.txt') 70 | f.write(response.text) 71 | f.close() 72 | exit(0) 73 | return html.unescape(result[0]) 74 | 75 | def translate(self, text, target_language='', source_language='', timeout=''): 76 | if not target_language: 77 | target_language = self.target_language 78 | if not source_language: 79 | source_language = self.source_language 80 | if not timeout: 81 | timeout = self.timeout 82 | if len(text) > 5000: 83 | print('\nError: It can only detect 5000 characters at once. (%d characters found.)'%(len(text))) 84 | exit(0) 85 | if type(target_language) is list: 86 | with concurrent.futures.ThreadPoolExecutor() as executor: 87 | futures = [executor.submit(self.make_request, target, source_language, text, timeout) for target in target_language] 88 | return_value = [f.result() for f in futures] 89 | return return_value 90 | return self.make_request(target_language, source_language, text, timeout) 91 | 92 | def translate_file(self, file_path, target_language='', source_language='', timeout=''): 93 | if not os.path.isfile(file_path): 94 | print('\nError: The file or path is incorrect.') 95 | exit(0) 96 | f = open(file_path) 97 | text = self.translate(f.read(), target_language, source_language, timeout) 98 | f.close() 99 | return text --------------------------------------------------------------------------------