This repo contains the minimal code for a Google Translate micro-service using Python3 Functions Framework.
Before anything, generate an API Key for Google Translate API.
open https://console.cloud.google.com/apis/credentials
+ CREATE CREDENTIALS
> API Key
Restrict key to Google Translation API
only.
Copy the config.sample.yaml
file to a config.yaml
and fill the key.
The '/
' path expects a 'q
' query containing the word or sentence to send to Translate API.
?q=Hello+World
# > {"Hello World": "en"}
Requirements:
VIRTUALENV="venv"
virtualenv ${VIRTUALENV} -p python3
source ${VIRTUALENV}/bin/activate
pip install requirements.txt
Run development server
# after running requirements
functions-framework --target=entrypoint
# > open localhost:8080
Run tests
# after running requirements
python3 main.py
Build container
CONTAINER_NAME="translate"
GCR_LOCATION="eu."
GCP_PROJECT_ID="my-project-id"
# Choose to build locally or remotely
# locally
docker build -t ${CONTAINER_NAME} .
# remotely
gcloud builds submit \
--tag ${GCR_LOCATION}gcr.io/${GCP_PROJECT_ID}/${CONTAINER_NAME} .
docker run \
-d \
--restart always \
-p 0.0.0.0:8080:8080 \
-e PORT=8080 \
${CONTAINER_NAME}
.
├── config.sample.yaml # Copy this file as 'config.yaml' and fill it
├── Dockerfile
├── main.py
├── requirements.txt # pip requirements
├── services
│ ├── __init__.py
│ ├── http.py # Implem HTTP calls and authentication
│ └── translate.py # Implem Translate "detect language" feature
├── utils
│ └── __init__.py # Utils such as config
├── tests
└── __init__.py # Few tests