Jump to content

Redundant translator


corgano
 Share

Recommended Posts

Redundant translator: WTF?

I noticed that when one translates a selection of text to another language and back, sometimes it comes out slightly different. I created this for the repeated translation to and from a language. Why? sometimes it gives funny results. Try typing in "boy do i like cake".

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
Global $CBS_DROPDOWNLIST= 3
Global $LBS_STANDARD= 10485763


$titlea = "Translate from "
$titleb = " to "


$inlang = "En"
$outlang = "Hr"

$gui = GUICreate($titlea & $inlang & $titleb & $outlang,300,400)
$Edit1 = GUICtrlCreateEdit("",20,50,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$Edit2 = GUICtrlCreateEdit("",20,95,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$Edit3 = GUICtrlCreateEdit("",20,140,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$Edit4 = GUICtrlCreateEdit("",20,185,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$Edit5 = GUICtrlCreateEdit("",20,230,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$Edit6 = GUICtrlCreateEdit("",20,275,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$Edit7 = GUICtrlCreateEdit("",20,320,260,35, BitOR($ES_WANTRETURN,$WS_VSCROLL))

$to = GUICtrlCreateCombo("",20,20,100, 20, BitOR($ES_READONLY, $CBS_DROPDOWNLIST, $LBS_STANDARD))
GUICtrlSetData(-1,"Af|Ar|Be|Bg|Ca|Cy|Da|De|En|El|Es|Fa|Fi|Fr|Ga|Gl|He|Hi|Hr|Hu|In|Is|It|Ja|Ko|Lt|Lv|Mk|Ms|Mt|Nl|No|Pl|Pt|Ro|Ru|Sk|Sr|Sl|Sq|Sw|Th|Tl|Tr|Uk|Vi|Yi|zh-CN|zh-TW",$outlang)

GUISetState()

$old = ""
$new = ""

While 1
    $msg = GUIGetMsg()
    $new = GUICtrlRead($Edit1)
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $To
            $outlang = GUICtrlRead($To)
            WinSetTitle($gui,"",$titlea & $inlang & $titleb & $outlang)
            $old = "" ;this will trigger a translate
    EndSwitch
    
    If $new <> $old Then
        $old = $new
        $new = StringReplace($new,@crlf," ")
        $T2 = _GoogleTranslate($new,$inlang,$outlang)
        GUICtrlSetData($Edit2,$T2)      
        $T3 = _GoogleTranslate($T2,$outlang,$inlang)
        GUICtrlSetData($Edit3,$T3)      
        $T4 = _GoogleTranslate($T3,$inlang,$outlang)
        GUICtrlSetData($Edit4,$T4)      
        $T5 = _GoogleTranslate($T4,$outlang,$inlang)
        GUICtrlSetData($Edit5,$T5)      
        $T6 = _GoogleTranslate($T5,$inlang,$outlang)
        GUICtrlSetData($Edit6,$T6)      
        $T7 = _GoogleTranslate($T6,$outlang,$inlang)
        GUICtrlSetData($Edit7,$T7)
        sleep(100)
    EndIf
WEnd

Func _GoogleTranslate($sText, $sFrom = "en", $sTo = "ru")
    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)
;~  ConsoleWrite($GoogleURL & @crlf)
    
    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

Func foo()
    Exit
EndFunc

The from Zinthose

compiled version: download

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...