Created
July 29, 2013 18:58
-
-
Save sbaer/6106755 to your computer and use it in GitHub Desktop.
sample google translate python script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Use Google translate web service""" | |
| import rhinoscriptsyntax as rs | |
| import json, urllib | |
| import scriptcontext | |
| # Api key that I (Steve Baer) got from Google | |
| # http://code.google.com/apis/console-help/#UsingKeys | |
| # You might want to generate your own key, but I don't care | |
| # if you continue to use this one. | |
| KEY = "AIzaSyAZGoS-GjZGaSHZMZdoczfdUtWTjm_D-p4" | |
| def translate(text, source="en", target="fr"): | |
| """Translate text from one language to another. Returns the | |
| translated text on success or None on failure | |
| """ | |
| # see if we already have a cached answer | |
| cache_dict_name = "translate " + source + ":" + target | |
| if scriptcontext.sticky.has_key(cache_dict_name): | |
| cache = scriptcontext.sticky[cache_dict_name] | |
| if cache.has_key(text): return cache[text] | |
| url = "https://www.googleapis.com/language/translate/v2" | |
| url += "?key=" + KEY | |
| url += "&q="+urllib.quote(text.encode('utf-8')) | |
| url += "&source="+source | |
| url += "&target="+target | |
| f = urllib.urlopen(url) | |
| s = f.read().decode('UTF-8') | |
| # it seems that the google translate api return html encoded strings | |
| htmlcodes = ('&', '&'),('<', '<'),('>', '>'),('"', '"'),("'", ''') | |
| for c, code in htmlcodes: s = s.replace(code, c) | |
| f.close() | |
| rc = json.loads(s) | |
| if rc.has_key("data"): | |
| translated = rc["data"]["translations"][0]["translatedText"] | |
| if not scriptcontext.sticky.has_key(cache_dict_name): | |
| scriptcontext.sticky[cache_dict_name] = {} | |
| scriptcontext.sticky[cache_dict_name][text] = translated | |
| return translated | |
| # use __name__ test to determine if this script is directly being | |
| # executed or if it is being loaded as a library | |
| if( __name__=="__main__" ): | |
| if rs.ContextIsGrasshopper(): | |
| # if this script is running in grasshopper, translate the input | |
| # variable and set the output variables | |
| spanish = translate(english, "en", "es") | |
| german = translate(english, "en", "de") | |
| french = translate(english, "en", "fr") | |
| italian = translate(english, "en", "it") | |
| japanese = translate(english, "en", "ja") | |
| elif rs.ContextIsRhino(): | |
| # get text dots and translate their contents | |
| dots = rs.GetObjects("Select dots to translate", rs.filter.textdot) | |
| if dots: | |
| langs = { "English":"en", | |
| "ChineseSimplified":"zh-CN", | |
| "ChineseTraditional":"zh-TW", | |
| "Czech":"cs", | |
| "French":"fr", | |
| "German":"de", | |
| "Italian":"it", | |
| "Japanese":"ja", | |
| "Korean":"ko", | |
| "Polish":"pl", | |
| "Spanish":"es" | |
| } | |
| source_lang = rs.GetString("source", "English", langs.keys()) | |
| source_lang = langs[source_lang] | |
| target_lang = rs.GetString("target", "Spanish", langs.keys()) | |
| target_lang = langs[target_lang] | |
| for dot in dots: | |
| s = rs.TextDotText(dot) | |
| s = translate(s, source_lang, target_lang) | |
| rs.TextDotText(dot, s) |
Author
I think google discontinued supporting their translate service several years ago
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, Steve. Congratulations for your wordpress, has a lot of interesting information.
Well, I'm trying to use this code of yours, but I can't make it work, even using my own API key. There is no error, but the output is "null" for every language. See the gh file here https://drive.google.com/file/d/1-5MnDdxnfGPBH7dbM9ZxStq47xvlo-SR/view?usp=sharing.
Hope you can give me an idea of how to fix.
Thanks you in advance,
Kevin