2424import argparse
2525
2626
27+ # [START translate_detect_language]
2728def detect_language (text ):
28- # [START translate_detect_language]
2929 """Detects the text's language."""
3030 from google .cloud import translate_v2 as translate
3131
@@ -38,11 +38,12 @@ def detect_language(text):
3838 print ("Text: {}" .format (text ))
3939 print ("Confidence: {}" .format (result ["confidence" ]))
4040 print ("Language: {}" .format (result ["language" ]))
41- # [END translate_detect_language]
4241
4342
43+ # [END translate_detect_language]
44+
45+ # [START translate_list_codes]
4446def list_languages ():
45- # [START translate_list_codes]
4647 """Lists all available languages."""
4748 from google .cloud import translate_v2 as translate
4849
@@ -52,11 +53,12 @@ def list_languages():
5253
5354 for language in results :
5455 print (u"{name} ({language})" .format (** language ))
55- # [END translate_list_codes]
5656
5757
58+ # [END translate_list_codes]
59+
60+ # [START translate_list_language_names]
5861def list_languages_with_target (target ):
59- # [START translate_list_language_names]
6062 """Lists all available languages and localizes them to the target language.
6163
6264 Target must be an ISO 639-1 language code.
@@ -70,11 +72,12 @@ def list_languages_with_target(target):
7072
7173 for language in results :
7274 print (u"{name} ({language})" .format (** language ))
73- # [END translate_list_language_names]
7475
7576
77+ # [END translate_list_language_names]
78+
79+ # [START translate_text_with_model]
7680def translate_text_with_model (target , text , model = "nmt" ):
77- # [START translate_text_with_model]
7881 """Translates text into the target language.
7982
8083 Make sure your project is allowlisted.
@@ -97,11 +100,12 @@ def translate_text_with_model(target, text, model="nmt"):
97100 print (u"Text: {}" .format (result ["input" ]))
98101 print (u"Translation: {}" .format (result ["translatedText" ]))
99102 print (u"Detected source language: {}" .format (result ["detectedSourceLanguage" ]))
100- # [END translate_text_with_model]
101103
102104
105+ # [END translate_text_with_model]
106+
107+ # [START translate_translate_text]
103108def translate_text (target , text ):
104- # [START translate_translate_text]
105109 """Translates text into the target language.
106110
107111 Target must be an ISO 639-1 language code.
@@ -122,7 +126,9 @@ def translate_text(target, text):
122126 print (u"Text: {}" .format (result ["input" ]))
123127 print (u"Translation: {}" .format (result ["translatedText" ]))
124128 print (u"Detected source language: {}" .format (result ["detectedSourceLanguage" ]))
125- # [END translate_translate_text]
129+
130+
131+ # [END translate_translate_text]
126132
127133
128134if __name__ == "__main__" :
0 commit comments