Google Translate API

Source: Internet
Author: User

Recently, I am working on a task, but there is little information on the Internet. After several twists and turns, I finally got it done. The implementation process is very simple. Share it out so that you do not need to find it for a long time like me. Google Translate changed to a paid version after upgrading to 2.0. Therefore, you must first apply for a key from Google. The billing standard is $20 for every 1 million characters in text ). Google Translate work process: 1. Google Translate request URL: https://www.googleapis.com/language/translate/v2? Three parameters of the {parameters} Translation request: 1) API key: A key will be assigned to you after payment is made to Google; 2) Target language: the Target language you need to translate; 3) Source text string: the text to be translated (less than 2 kb ). 2. The following are examples of different parameters. You can pass parameters to the url as needed: 1) specify source and targethttps: // www.googleapis.com/language/translate/v2? Key = INSERT-YOUR-KEY & source = en & target = de & q = Hello % 20 worldJSON {"data": {"translations": [{"translatedText ": "Hallo Welt"}]} 2) when multiple q is passed in, the multi-segment text is translated into https://www.googleapis.com/language/translate/v2? Key = INSERT-YOUR-KEY & source = en & target = de & q = Hello % 20 world & q = My % 20 name % 20is % 20 JeffJSON {"data ": {"translations": [{"translatedText": "Hallo Welt" },{ "translatedText": "Mein Name ist Jeff"}] }3) do not specify source, translate directly into target language https://www.googleapis.com/language/translate/v2? Key = INSERT-YOUR-KEY & target = de & q = Hello % 20 worldJSON {"data": {"translations": [{"translatedText": "Hallo Welt ", "detectedSourceLanguage": "en"}]} 3. python-implemented Google Translate source code: # coding: utf8 import urllib2import jsonimport OS, sys reload (sys) sys. setdefaultencoding ("UTF-8") _ author _ = 'chenyu 'class GoogleTranslate: "Google translation" def google_translate (slef, text, targetlanguage ): text = urllib2.quote (text) www.2 Cto.com url = "https://www.googleapis.com/language/translate/v2? Key = YOUR_KEY & target = "+ targetlanguage +" & q = "+ text res = urllib2.urlopen (urllib2.Request (url) dirt = json. JSONDecoder (). decode (res. read () return dirt ["data"] ["translations"] [0] ["translatedText"]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.