This sample demonstrates how to use the Google Cloud Translate API on the App Engine Standard Environment.
This app allows users to input text, which is sent to the Google Cloud Translate API. The frontend of the application displays information retrieved from the Translate API including the French and Chinese translations of the text and the detected language for the input text.
- Create a project with the Google Cloud Platform Console Cloud Resource Manager.
- Make note of your project ID, which may be different than your project name.
- Make sure to Enable Billing for your project.
- Go to the Google Cloud Platform console.
- Click the button in the top left, select
APIs & Services
. - Click
Enable APIs and Services
at the top. - Search for 'Translate', click the first result, then click the
Enable
button.
Download the Google Cloud SDK command line tool, also known as gcloud
.
Initialize gcloud, selecting your Google account and project ID:
gcloud init
Run the following command to clone the Github repository:
git clone https://github.com/GoogleCloudPlatform/hackathon-toolkit.git
Change directory to the sample code location:
cd hackathon-toolkit/translate
Set up a service account via the following instructions:
- Visit the Google Cloud Platform console
- Search
Service Accounts
on the top search bar, click onService accounts
. - Click the
Create Service Account
button at the top.- Give your service account a name and click
CREATE
. - For service account permissions, click
Select a role
and chooseProject > Owner
. - Click
CONTINUE
.
- Give your service account a name and click
- Click
CREATE KEY
, and clickCREATE
to download a JSON service account key to your computer. - Click
DONE
to finish creating your service account. - Save the generated service account key JSON file to somewhere on your computer and rename it to
key.json
.
In your terminal, set the GOOGLE_APPLICATION_CREDENTIALS
variable to point to the service account key location with this command:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/service/account/key.json
Set up your virtual environment:
virtualenv env
Note: If you do not already have
virtualenv
installed, run 'sudo easy_install pip' and then 'pip install virtualenv'.
Enter your virtual environment:
source env/bin/activate
Install dependencies:
pip install -r requirements.txt
Test your application locally:
python main.py
Visit localhost:8080
to view your application running locally. Press Control-C
from command line when you are finished.
When you are ready to leave your virtual environment:
deactivate
Deploy your application to App Engine (should take <1 minute). When prompted to choose a region, choose the one that is closest to you geographically.
gcloud app deploy