I am trying to use the google translator API for translating non-english words (transliterated words) to english word. For example, the word "Xingbie" is a Chinese transliterated word which is "Gender" in english word.
Now, when I tried to use the API using I do not get back the translated text.
from google.cloud import translate_v2 as translate
# Initialize the Translation client
translate_client = translate.Client()
# Function to detect the language of a given text
def detect_language(text):
result = translate_client.detect_language(text)
return result['language'], result['confidence']
# Function to translate text
def translate_text(text, target_language):
translation = translate_client.translate(text, target_language=target_language)
return translation['translatedText']
# Example usage
text_to_detect = "Xingbie" # This is a transliterated Chinese word
# Detect language
language, confidence = detect_language(text_to_detect)
print(f"Detected language: {language} (Confidence: {confidence:.2f})")
# Translate to English
translated_text = translate_text(text_to_detect, 'en')
print(f"Translated text: {translated_text}")
Output:
Detected language: zh-Latn (Confidence: 0.94)
Translated text: Xingbie
The translated text should be "Gender" which is exactly from the browser I can see. However, the API returns the same string. What am I missing here?
Currently, Google Cloud Translation API doesn’t support transliteration for Chinese (zh) words. The API might be recognizing it as Chinese (zh-Latn) but doesn’t inherently understand its meaning.
You can actually monitor the release notes to keep you updated of the recent fixes and announcements. Also, I figured these common issues might be helpful to you.