Skip to main content

Googletrans google translation flask extension

Project description

flask_googletrans

Coverage Status

A Flask extension to add Googletrans google translation to the template with ability to cache translation to external pretty .json file.

Install:

- With pip

  • pip install Flask-Googletrans

- From the source:

  • git clone https://github.com/mrf345/flask_googletrans.git
  • cd flask_googletrans
  • python setup.py install

Setup:

- Inside Flask app:

from flask import Flask, render_template
from flask_googletrans import translator

app = Flask(__name__)
ts = translator(app)

- Inside jinja template:

<h1>{{ translate(text='translation !', src='en', dest=['fr']) }}</h1>

Settings:

- Options:

The accepted arguments to be passed to the translator.translate() function are as follow:

def translate(
        self,
        text='translation !', # Text to be translated
        src='en', # Language to be translated from
        dest=['fr']): # Languages to translate to

If more than one language is used, the returning value will be a dictionary instead of a text string

# assuming
translate(text='something', src='en', dest=['fr', 'it', 'es'])
# this will return 
{"en": "nothing", "fr": "rien", "it": "Niente", "es": "nada"}

- Caching:

Caching stores all the translated text in one big dictionary with the translated text as a key in a separate .json file

ts = translator(
    app=app,
    cache=True, # To enable caching by default is disabled
    fail_safe=False, # returns original text if fetching translation failed
    skip_app=False, # to skip checking app for .init_app()
    file_name='gt_cache.json', # To change the default name of the cache file
    route=False # opens up a route on /gtran/<fromL>/<toL>/<text> to fetch translation as json response {translation: 'text ...'}
)

- Useful functions:

Function Does
ts.translate() To translate as shown in template example
ts.loadCache() To load the cache file
ts.cacheIt() To store the current saved translation to the cache file

- List of supported languages:

{ 'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu', 'fil': 'Filipino', 'he': 'Hebrew' }

Credit:

  • Googletrans: Awesome free and unlimited python library that implements Google Translate API

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Googletrans-0.11.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

Flask_Googletrans-0.11-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Googletrans-0.11.tar.gz.

File metadata

  • Download URL: Flask-Googletrans-0.11.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.9

File hashes

Hashes for Flask-Googletrans-0.11.tar.gz
Algorithm Hash digest
SHA256 8cc0509f63647a47f15532201530701f226be7dacfbbb038217f465eeb7b21b9
MD5 214f33a04568d46ab09855477e5be8f8
BLAKE2b-256 d1162af7bfdf069249471a13625604b421dcb39f7470c1f6ceb842b99f9bc53b

See more details on using hashes here.

File details

Details for the file Flask_Googletrans-0.11-py3-none-any.whl.

File metadata

  • Download URL: Flask_Googletrans-0.11-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.9

File hashes

Hashes for Flask_Googletrans-0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 ea897189edc498942a47061bfe71cd5e2c049e0106050ad32f057580454cd613
MD5 23814e2919b7e7dafbeec0d0bacbdaca
BLAKE2b-256 56af781156f5c6c1c8e517d98a6a467ebf6491547ba88815b435bada7e921415

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page