Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

API for Chinese to pinyin or Cantonese to jyutping

tshmak
New Member

I noticed that if we make a Chinese translation request via the official translate page, e.g.:

https://translate.google.com/?sl=zh-CN&tl=en&text=%E4%B8%80%E5%80%8B%E5%AF%A6%E9%A9%97&op=translate

in the Chinese textbox, the pinyin of the text will show up. Does anyone know if there's an API which I can call to get that pinyin from Chinese text? 

I've tried the translate API, and it doesn't seem to be part of the response. 

0 1 1,150
1 REPLY 1

Hi @tshmak,

Welcome to Google Cloud Community!

You're right, while Google Translate's website shows pinyin for Chinese input, it isn't directly available through their official Translate API. However, you can achieve this by using a combination of libraries and APIs.

Here are some different options you could explore:

  1. Open Chinese Convert: This library helps convert Chinese text between different scripts and can be adapted for pinyin extraction. You can find more information in the OpenCC GitHub repository.
  2. Combining with Google Translate API: To integrate with the Google Translate API, first translate your Chinese text using the API, then utilize a Pinyin library to convert the translated text (now in the target language) back into pinyin. Pinyin libraries analyze Chinese characters and apply specific rules to determine their corresponding pronunciations, allowing you to obtain pinyin for any language supported by Google Translate.

Additional considerations:

  • You can also check this official guide that explains how to use the API for language translation.
  • I came across an article/blog that addresses a method to convert Chinese characters to pinyin in Google Sheets, providing a workaround for those without access to a suitable API. 
  • The pypinyin library provides options to include tone marks in the output, while if you need to segment Chinese text into words, you can use libraries like jieba in Python before performing the pinyin conversion.

I hope the above information is helpful.