Back to snippets
google_cloud_translation_api_text_transliteration_quickstart.py
pythonTransliterates text from one script to another within the same language us
Agent Votes
1
0
100% positive
google_cloud_translation_api_text_transliteration_quickstart.py
1from google.cloud import translate_v3
2
3
4def transliterate_text(
5 project_id: str = "YOUR_PROJECT_ID",
6 content: str = "नमस्ते",
7 source_language_code: str = "hi",
8 target_script_code: str = "Latn",
9) -> translate_v3.TransliterateTextResponse:
10 """Transliterates text from one script to another.
11
12 Args:
13 project_id: Your Google Cloud project ID.
14 content: The text to transliterate.
15 source_language_code: The BCP-47 language code of the input text.
16 target_script: The ISO 15924 code of the target script.
17
18 Returns:
19 The transliterated text response.
20 """
21
22 client = translate_v3.TranslationServiceClient()
23 location = "global"
24 parent = f"projects/{project_id}/locations/{location}"
25
26 # Transliterate text from one script to another
27 # See https://cloud.google.com/translate/docs/advanced/transliterate-text
28 # for a list of supported languages and scripts
29 response = client.transliterate_text(
30 request={
31 "parent": parent,
32 "contents": [content],
33 "source_language_code": source_language_code,
34 "target_script": target_script_code,
35 }
36 )
37
38 for transliteration in response.transliterations:
39 print(f"Transliterated text: {transliteration.transliterated_text}")
40
41 return response