用python写一个GH翻译成中文的插件
时间: 2023-04-09 14:04:44 浏览: 112
可以使用Google Translate API来实现这个功能,以下是示例代码:
```python
import rhinoscriptsyntax as rs
import urllib
import urllib.request
import json
def translate(text):
url = "https://translation.googleapis.com/language/translate/v2"
data = {
"q": text,
"source": "en",
"target": "zh-CN",
"format": "text"
}
data = urllib.parse.urlencode(data).encode("utf-8")
api_key = "YOUR_API_KEY_HERE"
request = urllib.request.Request(url, data)
request.add_header("Authorization", "Bearer " + api_key)
response = urllib.request.urlopen(request).read()
response = json.loads(response.decode("utf-8"))
return response["data"]["translations"][0]["translatedText"]
def translate_gh():
objs = rs.GetObjects("Select objects to translate")
if objs:
for obj in objs:
text = rs.TextObjectText(obj)
if text:
rs.TextObjectText(obj, translate(text))
translate_gh()
```
注意:需要替换 `YOUR_API_KEY_HERE` 为你自己的 Google Translate API key。