|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 | 15 | # [START translate_v3_translate_text]
|
16 |
| -# Imports the Google Cloud Translation library |
17 | 16 | import os
|
18 | 17 |
|
| 18 | +# Import the Google Cloud Translation library. |
| 19 | +# [START translate_v3_import_client_library] |
19 | 20 | from google.cloud import translate_v3
|
| 21 | +# [END translate_v3_import_client_library] |
20 | 22 |
|
21 | 23 | PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
|
22 | 24 |
|
23 | 25 |
|
24 |
| -# Initialize Translation client |
25 | 26 | def translate_text(
|
26 | 27 | text: str = "YOUR_TEXT_TO_TRANSLATE",
|
27 |
| - language_code: str = "fr", |
| 28 | + source_language_code: str = "en-US", |
| 29 | + target_language_code: str = "fr", |
28 | 30 | ) -> translate_v3.TranslationServiceClient:
|
29 |
| - """Translating Text from English. |
| 31 | + """Translate Text from a Source language to a Target language. |
30 | 32 | Args:
|
31 | 33 | text: The content to translate.
|
32 |
| - language_code: The language code for the translation. |
33 |
| - E.g. "fr" for French, "es" for Spanish, etc. |
34 |
| - Available languages: https://cloud.google.com/translate/docs/languages#neural_machine_translation_model |
| 34 | + source_language_code: The code of the source language. |
| 35 | + target_language_code: The code of the target language. |
| 36 | + For example: "fr" for French, "es" for Spanish, etc. |
| 37 | + Find available languages and codes here: |
| 38 | + https://cloud.google.com/translate/docs/languages#neural_machine_translation_model |
35 | 39 | """
|
36 | 40 |
|
| 41 | + # Initialize Translation client. |
37 | 42 | client = translate_v3.TranslationServiceClient()
|
38 | 43 | parent = f"projects/{PROJECT_ID}/locations/global"
|
39 |
| - # Translate text from English to chosen language |
40 |
| - # Supported mime types: # https://cloud.google.com/translate/docs/supported-formats |
| 44 | + |
| 45 | + # MIME type of the content to translate. |
| 46 | + # Supported MIME types: |
| 47 | + # https://cloud.google.com/translate/docs/supported-formats |
| 48 | + mime_type = "text/plain" |
| 49 | + |
| 50 | + # Translate text from the source to the target language. |
41 | 51 | response = client.translate_text(
|
42 | 52 | contents=[text],
|
43 |
| - target_language_code=language_code, |
44 | 53 | parent=parent,
|
45 |
| - mime_type="text/plain", |
46 |
| - source_language_code="en-US", |
| 54 | + mime_type=mime_type, |
| 55 | + source_language_code=source_language_code, |
| 56 | + target_language_code=target_language_code, |
47 | 57 | )
|
48 | 58 |
|
49 |
| - # Display the translation for each input text provided |
| 59 | + # Display the translation for the text. |
| 60 | + # For example, for "Hello! How are you doing today?": |
| 61 | + # Translated text: Bonjour comment vas-tu aujourd'hui? |
50 | 62 | for translation in response.translations:
|
51 | 63 | print(f"Translated text: {translation.translated_text}")
|
52 |
| - # Example response: |
53 |
| - # Translated text: Bonjour comment vas-tu aujourd'hui? |
54 | 64 |
|
55 | 65 | return response
|
56 |
| - |
57 |
| - |
58 | 66 | # [END translate_v3_translate_text]
|
59 | 67 |
|
| 68 | + |
60 | 69 | if __name__ == "__main__":
|
61 |
| - translate_text(text="Hello! How are you doing today?", language_code="fr") |
| 70 | + translate_text( |
| 71 | + text="Hello! How are you doing today?", |
| 72 | + source_language_code="en-US", |
| 73 | + target_language_code="fr" |
| 74 | + ) |
0 commit comments