#Region - Demo
_GoogleToMsgBox("Hello World", "en", "en")
_GoogleToMsgBox("Hello World", "en", "ja")
_GoogleToMsgBox("Hello World", "en", "es")
_GoogleToMsgBox("Hello World", "en", "it")
_GoogleToMsgBox("Hello World", "en", "zh-cn")
_GoogleToMsgBox("Hello World", "en", "ar")
_GoogleToMsgBox("Hello World", "en", "bg")
_GoogleToMsgBox("Hello World", "en", "fr")
_GoogleToMsgBox("Hello World", "en", "de")
_GoogleToMsgBox(InputBox("在线快速翻译", "请输入您要翻译的内容", ""), "en", "zh-cn")
Func _GoogleToMsgBox($sText, $sFrom, $sTo)
Msgbox(4096, "在线快速翻译 [" & $sFrom & ":" & $sTo & "]", $sText & @TAB & @TAB & @TAB & @CRLF & @CRLF & _GoogleTranslate($sText, $sFrom, $sTo))
EndFunc
#EndRegion
Func _GoogleTranslate($sText, $sFrom = "en", $sTo = "zh-cn")
Local Const $FileName = "Translation.jsn"
Local Const $Pattern = '"translatedText":"([^"]+)"'
Local $GoogleURL = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=%s&langpair=%s%%7C%s"
Local $File
$GoogleURL = StringFormat($GoogleURL, $sText, $sFrom, $sTo)
If Not InetGet($GoogleURL, $FileName, 1) Then Return SetError(1, 0, 0)
$File = FileOpen($FileName, 4)
FileGetSize($FileName)
$Translation = FileRead($File, FileGetSize($FileName))
FileClose($File)
FileDelete($FileName)
$Translation = BinaryToString($Translation, 4)
If StringRegExp($Translation , $Pattern) Then
$Translation = StringRegExp($Translation , $Pattern, 1)
$Translation = $Translation[0]
Return $Translation
EndIf
EndFunc |