Last active
April 6, 2018 19:32
-
-
Save juancarlospaco/abef209ae8028087dce3 to your computer and use it in GitHub Desktop.
Translate from internet via API from mymemory.translated.net, with Fall-back, DoNotTrack HTTPS, Time-Out and Legally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
"""Tinyslations, smallest possible Translations from Internet with fallback.""" | |
from urllib import parse, request | |
from locale import getdefaultlocale | |
from json import loads | |
def tinyslation(s: str, to: str=getdefaultlocale()[0][:2], fm="en") -> str: | |
"""Translate from internet via API from mymemory.translated.net,legally.""" | |
api = "https://mymemory.translated.net/api/get?q={st}&langpair={fm}|{to}" | |
req = request.Request(url=api.format(st=parse.quote(s), fm=fm, to=to), | |
headers={'User-Agent': '', 'DNT': 1}) # DoNotTrack | |
try: | |
responze = request.urlopen(req, timeout=3).read().decode("utf-8") | |
return loads(responze)['responseData']['translatedText'] | |
except: | |
return str(s).strip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alias
_( )
using a simple Variable to aliastinyslation( )
.NOTE:
gettext
with manual translation and professional translators.🌏 🌍 🌎