Jump to content

Google translate and @CR @LF


corgano
 Share

Recommended Posts

I am useing the from Zinthose and i have an issue with @CRLF chars and the translation. The translation strips enter and I need a way to maintain having them. If you type:

Test

Test

Test

Test

It translates to (Russian)

Тест Тест Тест Тест

The problem si that it is all in one line. Here is example code to see what I mean

#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 = "ru"
$oslang = @oslang

If $oslang = "040a" or $oslang = "080a" or $oslang = "0c0a" or $oslang = "100a" or $oslang = "140a" or $oslang = "180a" or $oslang = "1c0a" or $oslang = "200a" or $oslang = "240a" or $oslang = "280a" or $oslang = "2c0a" or $oslang = "300a" or $oslang = "340a" or $oslang = "380a" or $oslang = "3c0a" or $oslang = "400a" or $oslang = "440a" or $oslang = "480a" or $oslang = "4c0a" or $oslang = "500a" Then
    $inlang = "Es"
    $titlea = _GoogleTranslate($titlea, "en", "es") & " "
    $titleb = " " & _GoogleTranslate($titleb, "en", "es") & " "
ElseIf $oslang = "0407" or $oslang = "0807" or $oslang = "0c07" or $oslang = "1007" or $oslang = "1407" then
    $inlang = "Ru"
    $titlea = _GoogleTranslate($titlea, "en", "Ru") & " "
    $titleb = " " & _GoogleTranslate($titleb, "en", "Ru") & " "
EndIf

$gui = GUICreate($titlea & $inlang & $titleb & $outlang,300,400)
$Editin = GUICtrlCreateEdit("",20,20,260,160, BitOR($ES_WANTRETURN,$WS_VSCROLL))
$From = GUICtrlCreateCombo("",20,190,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",$inlang)
$To = GUICtrlCreateCombo("",180,190,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)
$swap = GUICtrlCreateButton("swap",130,190,40,20)
$Editout = GUICtrlCreateEdit("",20,220,260,160, BitOR($ES_WANTRETURN,$WS_VSCROLL))
GUISetState()

$old = ""
$new = ""
$old2 = ""
$new2 = ""

While 1
    $msg = GUIGetMsg()
    $new = GUICtrlRead($Editin)
    $new2 = GUICtrlRead($Editout)
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $swap
            $outlang = GUICtrlRead($From)
            $inlang = GUICtrlRead($To)
            GUICtrlSetData($From,"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",$inlang)
            GUICtrlSetData($To,"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)
            WinSetTitle($gui,"",$titlea & $inlang & $titleb & $outlang)
            $old = "" ;this will trigger a translate
        Case $From
            $inlang = GUICtrlRead($From)
            WinSetTitle($gui,"",$titlea & $inlang & $titleb & $outlang)
            $old = "" ;this will trigger a translate
        Case $To
            $outlang = GUICtrlRead($To)
            WinSetTitle($gui,"",$titlea & $inlang & $titleb & $outlang)
            $old = "" ;this will trigger a translate
    EndSwitch

    If $new <> $old Then
        $old = $new
        $out = _GoogleTranslate($new,$inlang,$outlang)
        GUICtrlSetData($Editout,$out)
        $new2 = GUICtrlRead($Editout)
        $old2 = $new2

    ElseIf $new2 <> $old2 Then
        $old2 = $new2
        $in = _GoogleTranslate($new2,$outlang,$inlang)
        GUICtrlSetData($Editin,$in)
        $new = GUICtrlRead($Editin)
        $old = $new

    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)

    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

I hope someone knows a workaround, I have been trying for the last 6 days to get it on my own.

EDIT* Fixed error

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Is it that you are replacing the @CRLF with a space?

$new = StringReplace($new,@crlf," ")

I forgot I put those there. I fixed it in the example, and am trying different things right now. Hopefully what I was trying to do before will work now

*update* This works, and is 90% stable. The only side effect being that if someone types "(CRLF)" it will add an extra @crlf where in the string he typed it. This is acceptable for now as not many people useing a translator would think to type (CRLF).

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, $sEnter = "(CRLF)"
    $sText = stringreplace($sText, @CRLF, $sEnter)
    $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 = stringreplace($Translation[0], $sEnter, @CRLF)
        $Translation = stringreplace($Translation, @CRLF&" ", @CRLF)
        Return $Translation
    EndIf
EndFunc
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...