Skip to main content

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

Metadata

Release files for Flask-Googletrans 0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Googletrans 0.11
File Size Uploaded
Flask-Googletrans-0.11.tar.gz 6.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Googletrans 0.11
File Interpreter ABI Platform
Flask_Googletrans-0.11-py3-none-any.whl Python 3 none any Details

Total release size: 13.2 kB

Release files / Flask-Googletrans-0.11.tar.gz

Download URL Flask-Googletrans-0.11.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8cc0509f63647a47f15532201530701f226be7dacfbbb038217f465eeb7b21b9
BLAKE2b-256 checksum
How to use checksums
d1162af7bfdf069249471a13625604b421dcb39f7470c1f6ceb842b99f9bc53b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / Flask_Googletrans-0.11-py3-none-any.whl

Download URL Flask_Googletrans-0.11-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea897189edc498942a47061bfe71cd5e2c049e0106050ad32f057580454cd613
BLAKE2b-256 checksum
How to use checksums
56af781156f5c6c1c8e517d98a6a467ebf6491547ba88815b435bada7e921415
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.11 This release

2 release files

0.10

2 release files

0.9

1 release file

0.8

1 release file

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page