(May 2021) It's been about 8 years since the original question, and since then, several product changes have occurred, so new developers arriving here looking to use the Google Translate API on Python App Engine have a few changes to make:
Product info/costs: The Google Translate API is now available as the Google Cloud Translation API. It's not free (meaning you need to create a billing account backed by a financial instrument like a credit card), but you get a quota of translated characters per month. See its pricing page for more info. Similarly, while you used to be able to create an App Engine app without a credit card, you can no longer do so via the new policy as of Nov 2019. It still has a generous "Always Free" tier quota which you must exceed in order to incur charges. Also see the App Engine pricing page for more info.
Client libraries: Rather than using apiclient
or googleapiclient
which are part of the Google APIs client library which is a low-level, multi-product, platform-level client library, we recommend the Google Cloud client libraries which are higher-level and product-focused. That means there's a specific Cloud Translation client library (actually two: basic/v2/Python 2 or advanced/v3/Python 3) — these are higher-level and much easier to use:
- Add client lib:
pip install -U pip google-cloud-translate
(or pip3
)
- With it, your code sample can be as simple as:
'translate_demo.py - demo the Cloud Translation API'
from __future__ import print_function
import google.auth
from google.cloud import translate
TRANSLATE = translate.TranslationServiceClient()
_, PROJECT_ID = google.auth.default()
PARENT = 'projects/{}'.format(PROJECT_ID)
TARGET_LANG = 'es'
TEXT = 'Hello world'
DATA = {
'parent': PARENT,
'contents': [TEXT],
'target_language_code': TARGET_LANG,
}
try: # Python 3/advanced/v3
rsp = TRANSLATE.translate_text(request=DATA)
except TypeError: # Python 2/basic/v2
rsp = TRANSLATE.translate_text(**DATA)
print(TEXT, '=', rsp.translations[0].translated_text)
It also works on Python 2 and 3 without any modification:
$ python2 translate_demo.py
Hello world = Hola Mundo
$ python3 translate_demo.py
Hello world = Hola Mundo
This code snippet can be adapted for App Engine fairly easily (more below), especially if you're prototyping since you can take advantage of the default service account so you don't have to muck around with service accounts, like making a new one, creating a public/private key-pair, and having to download the JSON credentials file and pointing the GOOGLE_APPLICATION_CREDENTIALS
environment variable to it, etc. When you're ready to go into production and need to create your own service account, then check out this page in the docs.
Furthermore, there has been significant changes in App Engine itself: the original Python 2 App Engine service had a bunch of built-in proprietary APIs (Datastore, Memcache, Task Queues, etc.). Due to user feedback regarding "vendor lock-in," the next generation Python 3 App Engine service was made to free developers from those services. Instead, you'd leverage any equivalent productized services, i.e., Cloud Datastore, Cloud Memorystore, and Cloud Tasks instead. The Google Cloud team has created a migration guide and I've augmented that guide with hands-on tutorials, code samples, and videos to help folks migrate to these unbundled services as you port your app to Python 3.
If you're considering Google Cloud serverless compute platforms beyond App Engine, such as Cloud Functions (FaaS) or Cloud Run (containerized/managed CaaS), then check out this Translation API sample app I created (where I basically stole the above code snippet from) that can be deployed 8 different ways, Python 2 and 3, locally with Flask's development server, to App Engine, Cloud Functions, or Cloud Run, all with just minor config changes. It's meant to show flexibility in our platforms as well as to help users understand the differences between them better.
apiclient
code directory in your appengine project ? – Nijin Narayananpip show PyDrive
– deFreitas