Skip to content

Commit

Permalink
Add translate region tags [(#1488)](#1488)
Browse files Browse the repository at this point in the history
* Add region tags

* Added end region tags

* Linting errors fixed
  • Loading branch information
averikitsch authored and dandhlee committed Nov 18, 2022
1 parent d7aca80 commit 1f67d43
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions translation/samples/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@


def detect_language(text):
# [START translate_detect_language]
"""Detects the text's language."""
translate_client = translate.Client()

Expand All @@ -38,19 +39,23 @@ def detect_language(text):
print('Text: {}'.format(text))
print('Confidence: {}'.format(result['confidence']))
print('Language: {}'.format(result['language']))
# [END translate_detect_language]


def list_languages():
# [START translate_list_codes]
"""Lists all available languages."""
translate_client = translate.Client()

results = translate_client.get_languages()

for language in results:
print(u'{name} ({language})'.format(**language))
# [END translate_list_codes]


def list_languages_with_target(target):
# [START translate_list_language_names]
"""Lists all available languages and localizes them to the target language.
Target must be an ISO 639-1 language code.
Expand All @@ -62,9 +67,11 @@ def list_languages_with_target(target):

for language in results:
print(u'{name} ({language})'.format(**language))
# [END translate_list_language_names]


def translate_text_with_model(target, text, model=translate.NMT):
# [START translate_text_with_model]
"""Translates text into the target language.
Make sure your project is whitelisted.
Expand All @@ -86,9 +93,11 @@ def translate_text_with_model(target, text, model=translate.NMT):
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
result['detectedSourceLanguage']))
# [END translate_text_with_model]


def translate_text(target, text):
# [START translate_translate_text]
"""Translates text into the target language.
Target must be an ISO 639-1 language code.
Expand All @@ -108,6 +117,7 @@ def translate_text(target, text):
print(u'Translation: {}'.format(result['translatedText']))
print(u'Detected source language: {}'.format(
result['detectedSourceLanguage']))
# [END translate_translate_text]


if __name__ == '__main__':
Expand Down

0 comments on commit 1f67d43

Please sign in to comment.