Skip to content

Commit

Permalink
Added region tags
Browse files Browse the repository at this point in the history
  • Loading branch information
engelke committed Aug 21, 2019
1 parent 9645a45 commit 87c47b6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion background/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ $ gcloud app deploy

From the function directory, after creating the PubSub topic:
```
$ gcloud functions deploy --runtime=go111 --trigger-topic=translate Translate --set-env-vars GOOGLE_CLOUD_PROJECT=my-project
$ gcloud functions deploy --runtime=python37 --trigger-topic=translate Translate --set-env-vars GOOGLE_CLOUD_PROJECT=my-project
```
2 changes: 2 additions & 0 deletions background/app/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# [START getting_started_background_config]
runtime: python37
# [END getting_started_background_config]
6 changes: 6 additions & 0 deletions background/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
provides a form to request a new translation.
"""

# [START getting_started_background_app_main]
import json
import os

Expand All @@ -31,8 +32,10 @@

# Keep this list of supported languages up to date
ACCEPTABLE_LANGUAGES = ('de', 'en', 'es', 'fr', 'ja', 'sw')
# [END getting_started_background_app_main]


# [START getting_started_background_app_list]
@app.route('/', methods=['GET'])
def index():
""" The home page has a list of prior translations and a form to
Expand All @@ -45,8 +48,10 @@ def index():
doc_list.append(doc.to_dict())

return render_template('index.html', translations=doc_list)
# [END getting_started_background_app_list]


# [START getting_started_background_app_request]
@app.route('/request-translation', methods=['POST'])
def translate():
""" Handle a request to translate a string (form field 'v') to a given
Expand Down Expand Up @@ -75,3 +80,4 @@ def translate():
)
publisher.publish(topic_name, json.dumps(message).encode('utf8'))
return redirect('/')
# [END getting_started_background_app_request]
36 changes: 22 additions & 14 deletions background/function/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,39 @@
The dictionary may have other fields, which will be ignored.
"""

# [START getting_started_background_translate_setup]
import base64
import hashlib
import json

from google.cloud import firestore
from google.cloud import translate
# [END getting_started_background_translate_setup]

# [START getting_started_background_translate_init]
# Get client objects once to reuse over multiple invocations.
xlate = translate.Client()
db = firestore.Client()
# [END getting_started_background_translate_init]


# [START getting_started_background_translate_string]
def translate_string(from_string, to_language):
""" Translates a string to a specified language.
from_string - the original string before translation
to_language - the language to translate to, as a two-letter code (e.g.,
'en' for english, 'de' for german)
Returns the translated string and the code for original language
"""
result = xlate.translate(from_string, target_language=to_language)
return result['translatedText'], result['detectedSourceLanguage']
# [END getting_started_background_translate_string]


# [START getting_started_background_translate]
def document_name(message):
""" Messages are saved in a Firestore database with document IDs generated
from the original string and destination language. If the exact same
Expand Down Expand Up @@ -65,20 +86,6 @@ def update_database(transaction, message):
transaction.set(doc_ref, message)


def translate_string(from_string, to_language):
""" Translates a string to a specified language.
from_string - the original string before translation
to_language - the language to translate to, as a two-letter code (e.g.,
'en' for english, 'de' for german)
Returns the translated string and the code for original language
"""
result = xlate.translate(from_string, target_language=to_language)
return result['translatedText'], result['detectedSourceLanguage']


def translate_message(event, context):
""" Process a pubsub message requesting a translation
"""
Expand All @@ -95,3 +102,4 @@ def translate_message(event, context):

transaction = db.transaction()
update_database(transaction, message)
# [END getting_started_background_translate]

0 comments on commit 87c47b6

Please sign in to comment.