Google Translate API

Source: Internet
Author: User
Recently in a task, but little information on the Internet, after a few twists and turns, finally got it. The implementation process is very simple, take out share, so that everyone like me for a long time.

Google translation to upgrade to 2.0 after the fee version, so first need to apply to Google a key. The pay standard is 20 dollars per 1 million text characters (feel small).

Google translation of the work process:
1. Google translation of the request URL: https://www.googleapis.com/language/translate/v2? {parameters} three parameters for translation requests:
1 API key: You need to pay Google, will give you a key;
2 target language: That is, you need to translate the goal language;
3 Source text string: Text that needs to be translated (less than 2k).

2. Here are some examples of different parameters that can be passed to the URL according to your own needs:
1 Specify source and target
HTTPS://Www.googleapis.com/language/translate/v2?key = insert-your-key & source = en & target = de &am P Q = Hello% 20world JSON
{
"Data": {
"Translations": [
{
"Translatedtext": "Hallo Welt"
}
]
}
2) Pass in multiple Q to translate multiple pieces of text
HTTPS://Www.googleapis.com/language/translate/v2?key = insert-your-key & source = en & target = de &am P Q = Hello% 20world & q = My% 20name% 20Jeff JSON
{
"Data": {
"Translations": [
{
"Translatedtext": "Hallo Welt"
},
{
"Translatedtext": "Mein Name ist Jeff"
}
]
}
} 3 does not specify source and translates directly into the target language
HTTPS://Www.googleapis.com/language/translate/v2?key = insert-your-key & target = de & q = Hello% 20w Orld JSON
{
"Data": {
"Translations": [
{
"Translatedtext": "Hallo Welt",
"Detectedsourcelanguage": "en"
}
]
}
3.python implementation of Google translation source:
# Coding:utf8


Import Urllib2
Import JSON
Import Os,sys

Reload (SYS)
Sys.setdefaultencoding ("Utf-8")

__author__ = ' Chenyu '

Class Googletranslate:
"""
Google Translation class
"""
def google_translate (slef,text,targetlanguage):
Text = urllib2.quote (text)
url = "https://www.googleapis.com/language/translate/v2/?key=YOUR_KEY&target=" + targetlanguage + "&q=" + t Ext
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.