├── .gitignore ├── README.md ├── demo.py ├── googletrans └── __init__.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/windows,linux,macos,visualstudiocode,python 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=windows,linux,macos,visualstudiocode,python 4 | 5 | ### Linux ### 6 | *~ 7 | 8 | # temporary files which can be created if a process still has a handle open of a deleted file 9 | .fuse_hidden* 10 | 11 | # KDE directory preferences 12 | .directory 13 | 14 | # Linux trash folder which might appear on any partition or disk 15 | .Trash-* 16 | 17 | # .nfs files are created when an open file is removed but is still being accessed 18 | .nfs* 19 | 20 | ### macOS ### 21 | # General 22 | .DS_Store 23 | .AppleDouble 24 | .LSOverride 25 | 26 | # Icon must end with two \r 27 | Icon 28 | 29 | 30 | # Thumbnails 31 | ._* 32 | 33 | # Files that might appear in the root of a volume 34 | .DocumentRevisions-V100 35 | .fseventsd 36 | .Spotlight-V100 37 | .TemporaryItems 38 | .Trashes 39 | .VolumeIcon.icns 40 | .com.apple.timemachine.donotpresent 41 | 42 | # Personal Data 43 | .jpg 44 | .png 45 | .wav 46 | 47 | # Directories potentially created on remote AFP share 48 | .AppleDB 49 | .AppleDesktop 50 | Network Trash Folder 51 | Temporary Items 52 | .apdisk 53 | 54 | ### Python ### 55 | # Byte-compiled / optimized / DLL files 56 | __pycache__/ 57 | *.py[cod] 58 | *$py.class 59 | 60 | # C extensions 61 | *.so 62 | 63 | # Distribution / packaging 64 | .Python 65 | build/ 66 | develop-eggs/ 67 | dist/ 68 | downloads/ 69 | eggs/ 70 | .eggs/ 71 | lib/ 72 | lib64/ 73 | parts/ 74 | sdist/ 75 | var/ 76 | wheels/ 77 | share/python-wheels/ 78 | *.egg-info/ 79 | .installed.cfg 80 | *.egg 81 | MANIFEST 82 | 83 | # PyInstaller 84 | # Usually these files are written by a python script from a template 85 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 86 | *.manifest 87 | *.spec 88 | 89 | # Installer logs 90 | pip-log.txt 91 | pip-delete-this-directory.txt 92 | 93 | # Unit test / coverage reports 94 | htmlcov/ 95 | .tox/ 96 | .nox/ 97 | .coverage 98 | .coverage.* 99 | .cache 100 | nosetests.xml 101 | coverage.xml 102 | *.cover 103 | *.py,cover 104 | .hypothesis/ 105 | .pytest_cache/ 106 | cover/ 107 | 108 | # Translations 109 | *.mo 110 | *.pot 111 | 112 | # Django stuff: 113 | *.log 114 | local_settings.py 115 | db.sqlite3 116 | db.sqlite3-journal 117 | 118 | # Flask stuff: 119 | instance/ 120 | .webassets-cache 121 | 122 | # Scrapy stuff: 123 | .scrapy 124 | 125 | # Sphinx documentation 126 | docs/_build/ 127 | 128 | # PyBuilder 129 | .pybuilder/ 130 | target/ 131 | 132 | # Jupyter Notebook 133 | .ipynb_checkpoints 134 | 135 | # IPython 136 | profile_default/ 137 | ipython_config.py 138 | 139 | # pyenv 140 | # For a library or package, you might want to ignore these files since the code is 141 | # intended to run in multiple environments; otherwise, check them in: 142 | # .python-version 143 | 144 | # pipenv 145 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 146 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 147 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 148 | # install all needed dependencies. 149 | #Pipfile.lock 150 | 151 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 152 | __pypackages__/ 153 | 154 | # Celery stuff 155 | celerybeat-schedule 156 | celerybeat.pid 157 | 158 | # SageMath parsed files 159 | *.sage.py 160 | 161 | # Environments 162 | .env 163 | .venv 164 | env/ 165 | venv/ 166 | ENV/ 167 | env.bak/ 168 | venv.bak/ 169 | 170 | # Spyder project settings 171 | .spyderproject 172 | .spyproject 173 | 174 | # Rope project settings 175 | .ropeproject 176 | 177 | # mkdocs documentation 178 | /site 179 | 180 | # mypy 181 | .mypy_cache/ 182 | .dmypy.json 183 | dmypy.json 184 | 185 | # Pyre type checker 186 | .pyre/ 187 | 188 | # pytype static type analyzer 189 | .pytype/ 190 | 191 | # Cython debug symbols 192 | cython_debug/ 193 | 194 | ### VisualStudioCode ### 195 | .vscode/* 196 | !.vscode/settings.json 197 | !.vscode/tasks.json 198 | !.vscode/launch.json 199 | !.vscode/extensions.json 200 | *.code-workspace 201 | 202 | # Local History for Visual Studio Code 203 | .history/ 204 | 205 | ### VisualStudioCode Patch ### 206 | # Ignore all local history of files 207 | .history 208 | .ionide 209 | 210 | ### Windows ### 211 | # Windows thumbnail cache files 212 | Thumbs.db 213 | Thumbs.db:encryptable 214 | ehthumbs.db 215 | ehthumbs_vista.db 216 | 217 | # Dump file 218 | *.stackdump 219 | 220 | # Folder config file 221 | [Dd]esktop.ini 222 | 223 | # Recycle Bin used on file shares 224 | $RECYCLE.BIN/ 225 | 226 | # Windows Installer files 227 | *.cab 228 | *.msi 229 | *.msix 230 | *.msm 231 | *.msp 232 | 233 | # Windows shortcuts 234 | *.lnk 235 | 236 | # End of https://www.toptal.com/developers/gitignore/api/windows,linux,macos,visualstudiocode,python 237 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Google Translate API for Python 2 | 3 | This is a Python library that provides an interface to the Google Translate API. 4 | 5 | ## Installation 6 | 7 | You can install this library using pip: 8 | 9 | ```bash 10 | pip3 install googletrans-python 11 | ``` 12 | 13 | ## Usage 14 | translate(TEXT, target_language_code) 15 | 16 | https://www.labnol.org/code/19899-google-translate-languages 17 | 18 | ```python 19 | import googletrans as gt 20 | 21 | # Translate text from English to Korean 22 | print(gt.translate("Hello, How are you?", "ko")) 23 | 24 | # Translate text from English to Japanese 25 | print(gt.translate("Hello, How are you?", "ja")) 26 | 27 | # Translate text from English to French 28 | print(gt.translate("Hello, How are you?", "fr")) 29 | ``` 30 | 31 | ```bash 32 | 안녕하세요. 어떻게 지내세요? 33 | こんにちは元気ですか? 34 | Bonjour comment allez-vous? 35 | ``` 36 | unofficial 37 | -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | import googletrans as gt 2 | 3 | # Translate text from English to Korean 4 | print(gt.translate("Hello, How are you?", "ko")) 5 | 6 | # Translate text from English to Japanese 7 | print(gt.translate("Hello, How are you?", "ja")) 8 | 9 | # Translate text from English to French 10 | print(gt.translate("Hello, How are you?", "fr")) 11 | -------------------------------------------------------------------------------- /googletrans/__init__.py: -------------------------------------------------------------------------------- 1 | import re 2 | import html 3 | import urllib.request 4 | import urllib.parse 5 | 6 | __version__ = "0.2.0" 7 | 8 | def translate(to_translate: str, to_language: str = "auto", from_language: str = "auto") -> str: 9 | """ 10 | Translates a text from one language to another using the Google Translate API. 11 | 12 | Args: 13 | to_translate (str): The text to translate. 14 | to_language (str, optional): The language to translate the text to. Defaults to "auto". 15 | from_language (str, optional): The language of the text to translate. Defaults to "auto". 16 | 17 | Returns: 18 | str: The translated text. 19 | """ 20 | link = f"http://translate.google.com/m?tl={to_language}&sl={from_language}&q={urllib.parse.quote(to_translate)}" 21 | request = urllib.request.Request(link, headers={ 22 | 'User-Agent': "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1;.NET CLR 1.1.4322;.NET CLR 2.0.50727;.NET CLR 3.0.04506.30)"}) 23 | re_result = re.findall( 24 | r'(?s)class="(?:t0|result-container)">(.*?)<', urllib.request.urlopen(request).read().decode("utf-8")) 25 | return "" if len(re_result) == 0 else html.unescape(re_result[0]) 26 | 27 | 28 | if __name__ == "__main__": 29 | print(translate("Hello, my name is Python.", "ko")) 30 | 31 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | from googletrans import __version__ as VERSION 3 | 4 | requirements = [ 5 | ] 6 | 7 | test_requirements = [ 8 | ] 9 | 10 | setup( 11 | name = 'googletrans-python', 12 | version = VERSION, 13 | description = 'googletrans-python package.', 14 | long_description = "googletrans", 15 | author = "leeyunjai", 16 | author_email = 'leeyunjai1982@gmail.com', 17 | url = 'https://github.com/leeyunjai/googletrans-python', 18 | packages = find_packages(), 19 | install_requires = requirements, 20 | keywords = 'googletrans', 21 | classifiers = [ 22 | 'Natural Language :: English', 23 | 'Programming Language :: Python :: 3.5', 24 | 'Programming Language :: Python :: 3.6', 25 | 'Programming Language :: Python :: 3.7', 26 | 'Programming Language :: Python :: 3.8', 27 | ], 28 | test_suite = 'tests', 29 | tests_require = test_requirements 30 | ) 31 | --------------------------------------------------------------------------------