|
21 | 21 | https://cloud.google.com/translate/docs.
|
22 | 22 | """
|
23 | 23 |
|
| 24 | +from __future__ import annotations |
| 25 | + |
24 | 26 | import argparse
|
25 | 27 |
|
26 | 28 |
|
@@ -116,30 +118,60 @@ def translate_text_with_model(target: str, text: str, model: str = "nmt") -> dic
|
116 | 118 |
|
117 | 119 |
|
118 | 120 | # [START translate_translate_text]
|
119 |
| -def translate_text(target: str, text: str) -> dict: |
120 |
| - """Translates text into the target language. |
121 |
| -
|
122 |
| - Target must be an ISO 639-1 language code. |
123 |
| - See https://g.co/cloud/translate/v2/translate-reference#supported_languages |
| 121 | +def translate_text( |
| 122 | + text: str | bytes | list[str] = "¡Hola amigos y amigas!", |
| 123 | + target_language: str = "en", |
| 124 | + source_language: str | None = None, |
| 125 | +) -> dict: |
| 126 | + """Translates a given text into the specified target language. |
| 127 | +
|
| 128 | + Find a list of supported languages and codes here: |
| 129 | + https://cloud.google.com/translate/docs/languages#nmt |
| 130 | +
|
| 131 | + Args: |
| 132 | + text: The text to translate. Can be a string, bytes or a list of strings. |
| 133 | + If bytes, it will be decoded as UTF-8. |
| 134 | + target_language: The ISO 639 language code to translate the text into |
| 135 | + (e.g., 'en' for English, 'es' for Spanish). |
| 136 | + source_language: Optional. The ISO 639 language code of the input text |
| 137 | + (e.g., 'fr' for French). If None, the API will attempt |
| 138 | + to detect the source language automatically. |
| 139 | +
|
| 140 | + Returns: |
| 141 | + A dictionary containing the translation results. |
124 | 142 | """
|
| 143 | + |
125 | 144 | from google.cloud import translate_v2 as translate
|
126 | 145 |
|
127 | 146 | translate_client = translate.Client()
|
128 | 147 |
|
129 | 148 | if isinstance(text, bytes):
|
130 |
| - text = text.decode("utf-8") |
| 149 | + text = [text.decode("utf-8")] |
131 | 150 |
|
132 |
| - # Text can also be a sequence of strings, in which case this method |
133 |
| - # will return a sequence of results for each text. |
134 |
| - result = translate_client.translate(text, target_language=target) |
| 151 | + if isinstance(text, str): |
| 152 | + text = [text] |
135 | 153 |
|
136 |
| - print("Text: {}".format(result["input"])) |
137 |
| - print("Translation: {}".format(result["translatedText"])) |
138 |
| - print("Detected source language: {}".format(result["detectedSourceLanguage"])) |
| 154 | + # If a string is supplied, a single dictionary will be returned. |
| 155 | + # In case a list of strings is supplied, this method |
| 156 | + # will return a list of dictionaries. |
139 | 157 |
|
140 |
| - return result |
| 158 | + # Find more information about translate function here: |
| 159 | + # https://cloud.google.com/python/docs/reference/translate/latest/google.cloud.translate_v2.client.Client#google_cloud_translate_v2_client_Client_translate |
| 160 | + results = translate_client.translate( |
| 161 | + values=text, |
| 162 | + target_language=target_language, |
| 163 | + source_language=source_language |
| 164 | + ) |
141 | 165 |
|
| 166 | + for result in results: |
| 167 | + if "detectedSourceLanguage" in result: |
| 168 | + print(f"Detected source language: {result['detectedSourceLanguage']}") |
142 | 169 |
|
| 170 | + print(f"Input text: {result['input']}") |
| 171 | + print(f"Translated text: {result['translatedText']}") |
| 172 | + print() |
| 173 | + |
| 174 | + return results |
143 | 175 | # [END translate_translate_text]
|
144 | 176 |
|
145 | 177 |
|
@@ -178,4 +210,4 @@ def translate_text(target: str, text: str) -> dict:
|
178 | 210 | elif args.command == "list-languages-with-target":
|
179 | 211 | list_languages_with_target(args.target)
|
180 | 212 | elif args.command == "translate-text":
|
181 |
| - translate_text(args.target, args.text) |
| 213 | + translate_text(text=args.text, target_language=args.target) |
0 commit comments