Disclaimer: This is not an officially supported Google product. Written code can be used as a baseline, it's not meant for production usage.
Translate-SRT is a demonstration of using Google Cloud Functions. The demo leverages the Google Translate API to translate a subtitle text file in SubRip format from one language to another once it is placed in a Google Cloud Storage bucket. Current code specifies from en
(english) to nl
(Dutch), but this is easily altered.
The Cloud Function is triggered when a new subtitle is stored in a predfined Cloud Storage bucket. After finishing the translation, a new subtitle file with the same timings is stored in a second predefined Cloud Storage bucket.
Get the latest sample code from GitHub using Git or download the repository as a ZIP file. (Download)
git clone https://github.com/pbavinck/Translate-SRT.git
-
Download and install the Google Cloud SDK, which includes the gcloud command-line tool
-
Create a new Google Cloud Platform project from the Cloud Console or use an existing one
-
Initialize
gcloud
, set your project ID and authorize the Cloud SDKgcloud init
-
Enable the Google Translate API in Cloud Console
-
Create three buckets on Google Cloud Storage. One for staging the Cloud Function code (staging bucket). One for the to be translated subtitle files, which is the bucket that triggers the Cloud Function (source bucket). And one for storing the translated subtitle files (target bucket).
We assume you already have nodeJS
and npm
installed. Before you can run the code locally, which will still use the remote Google Cloud buckets and the remote Google Translate API, you need to do the following:
-
From within the Translate-SRT folder, install the required node dependencies:
npm install
-
Copy the file
.env_sample
to a new file with the name.env
-
Enter your source and target bucket names in the environment variables
TEST_BUCKET
andTARGET_BUCKET
as well -
Add the location of your service account credentials to the
GOOGLE_APPLICATION_CREDENTIALS
environment variable. This enables the client libraries to use the application default credentials. In VSCode this can be done by adding the followingenv
line to yourlaunch.json
.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/index.js",
"env": {
"GOOGLE_APPLICATION_CREDENTIALS": "/the-path-to-my/service-account-credentials.json"
}
}
]
}
-
Put a sample text file in SubRip format in your source bucket (with extension .txt as opposed to .srt)
-
Run the test using the command:
node test.js
If all goes well you should see output similar to:
Processing new text file: sample-en.txt (text/plain)
Lines read: 7954
2510 lines of text need translation
Batch size: 128
Performing 20 translation requests
Translations done.
20 segments returned
Writing translated file locally to /tmp/sample-nl.txt.
Uploading local file sample-nl.txt to bucket <<target bucket>
/tmp/sample-nl.txt uploaded to <<target bucket>
Deleting local file /tmp/sample-nl.txt
Finished.
-
Use the following command to deploy the code to Google Cloud Function.
gcloud functions deploy translateSRTFiles --runtime nodejs8 --region=<<region>> --stage-bucket <<staging bucket name>> --trigger-resource <<source bucket name>> --trigger-event google.storage.object.finalize
-
Congratulations! Your Cloud Function is now live and receives events from your source bucket.
- See LICENSE