lydia_sp
15-11-2011, 13:16
ok, I am just starting this example. my idea is to translate words from input textbox into english, french, italian, spain, germany. my aim should looks like "I am an spanish girl" to translate this whole sentence into the language you like.
first I want only to translate one word like "hello" or "love" into various language. But I don't know what's best way. And what module I should prefer. I will open a text file with pre-defined vocabulary of special language and then translate this word into another language. perhaps here's at board an example to study?
my silly startpoint looks like this ;)
' Empty GUI script created on 11-15-2011 10:42:28 by lydia (ThinAIR) Uses "UI", "console", "tokenizer" ' -- ID numbers of controls Begin ControlID %ButtonClose = 1001 %tEnglish %List01 %tInput End ControlID Dim hDlg As DWord Dialog New 0, "translate_proggy",-1,-1, 480, 250, _ %WS_POPUP Or _ %WS_VISIBLE Or _ %WS_CLIPCHILDREN Or _ %WS_CAPTION Or _ %WS_SYSMENU Or _ %WS_MAXIMIZEBOX Or _ %WS_MINIMIZEBOX, _ 0 To hDlg Control Add Textbox hDlg, %tInput , "", 300, 25, 250, 280, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT, Call pickup() Control Add Button, hDlg, %ButtonClose, "Click to kill", 290, 5, 50, 18 Dialog Show Modal hDlg Call cbDialog CallBack Function cbDialog() As Long Local hDlg As Long, hText1 As Long Local lRslt As Long Dim vList(6) As String vList(1) = "french" vList(2) = "german" vList(3) = "italian" vList(4) = "spain" vList(5) = "english" vList(6) = "dutch" Dim vList2(100) As String vList2(1) = "Boy" Select Case CBMSG Case %WM_INITDIALOG Control Add COMBOBOX, CBHNDL, %List01 , vList(),150,5, 100, 200, %CBS_DROPDOWNLIST Or %CBS_HASSTRINGS Or %WS_TABSTOP Or %WS_VSCROLL COMBOBOX Select CBHNDL, %List01, 1 Control Add LISTBOX, CBHNDL,%tEnglish, vList2(), 5,25,250,280, %WS_VSCROLL Or %LBS_NOINTEGRALHEIGHT,%WS_EX_CLIENTEDGE, Call pickup() Case %WM_COMMAND If CBWPARAM = %ButtonClose Then Dialog End CBHNDL Case %WM_DESTROY MsgBox 0, "Window is to be destroyed." End Select End Function CallBack Function pickup() Local hdlg As Long Local str As String Dim vList2(100) As String vList2(1) = "Girl" If CBCTLMSG = %LBN_SELCHANGE Or CBCTLMSG=%LBN_DBLCLK Then LISTBOX Get Text hDlg, %tEnglish To str 'LISTBOX Get Text hWnd, ctrlID To Txt MsgBox 0, "hello english" LISTBOX Reset cbhndl,%tEnglish,vList2() 'LISTBOX RESET hWnd, ctrlID [, NewStringArray()] End If End Function
help or ideas are welcome. maybe the code tags don't working. I am working with linux and openoffice.org "writer" and this app copied whole text as block, don't know why, sorry. example in attachement too.
greetings, lydia
my idea is to use the web control to display the site:
http://translate.google.com/
as an example if you have translated the "i am a spanish girl" to spanish you will notice that the address like this:
http://translate.google.com/#en|es|I%20am%20an%20spanish%20girl
1- look at #en|es|
to investigate about abbreviations to other languages try to translate to different languages and look at the address how it changed.
2- also it replaces the space character with "%20"
4- so simply attach the content of the thinbasic textbox to a string variable and add it to "http://translate.google.com/#en|es|"
then supply it to the web browser control.
5- the text from the thinbasic textbox ,we must change every space between words to "%20", i have used Replace$.
6- to get back the translated text from google translator to a second thinbasic textbox , this is i don't know.
attached the source adapted from webBrowser_01.tbasic from the examples:
C:\thinBasic\SampleScripts\WebBrowser
7623
'------------------------------------------------------------------
' Pre-processor prerequisites
'------------------------------------------------------------------
#MINVERSION 1.7.8.0
'------------------------------------------------------------------
' Declare needed thinBasic modules
'------------------------------------------------------------------
USES "UI"
USES "UIAdv"
uses "console"
'------------------------------------------------------------------
' Global declarations
'------------------------------------------------------------------
begin const
%ID_WEB2 = 200
%ID_OK
%TM_WEB2_Busy
%translate
%tInput
end const
'------------------------------------------------------------------
' Main script entry point
'------------------------------------------------------------------
function TBMain() as long
Global hDlg As Long
local Count as long
Global txt As String
'---Create a new window
'DIALOG NEW pixels, 0, "Web Browser control & google translator",-1,-1,800,600,%WS_SYSMENU Or %WS_MINIMIZEBOX Or %WS_CAPTION To hDlg -1, -1, 800, 600, _
Dialog New Pixels, 0, "Web Browser control test", _
-1, -1, 740, 480, _
%WS_CLIPCHILDREN Or _
%WS_CLIPSIBLINGS Or _
%WS_DLGFRAME Or _
%DS_CENTER Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_OVERLAPPEDWINDOW , _
0 _
To hDlg
Control Add Textbox hDlg, %tInput , "", 10, 400, 300, 300, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT
Control Add Button, hDlg, %translate, "translate eng to spain", 350, 400, 140, 50, Call translate()
'---Show new window and assign it callback function
DIALOG SHOW MODELESS hDlg, call cbDialog_Proc
Do
DIALOG DOEVENTS 0 To Count
Loop While Count
end function
'------------------------------------------------------------------
' Window callback handle. Use to handle message pump of main window
'------------------------------------------------------------------
callback function cbDialog_Proc() as long
local sStr as string
local lValue as long
local IsBusy as long
SELECT CASE cbMsg
CASE %WM_INITDIALOG
'Control Add WEBBROWSER, cbhndl, %ID_WEB, "", 100, 10, 300, 240 'call cbWeb
DIALOG SET TIMER cbhndl, %TM_WEB2_Busy, 500
WebBrowser_Create CBHNDL, %ID_WEB2, "", 0, 0, 750, 400, %WS_CHILD Or %WS_VISIBLE
'WebBrowser_Navigate2(CBHNDL, %ID_WEB2, "http://www.google.com")
Control Set Resize CBHNDL, %ID_WEB2, 1, 1, 1, 1
'control add button cbhndl, %ID_OK, "OK", 10, 10, 40, 20
case %WM_TIMER
select case cbctl
case %TM_WEB2_Busy
if WebBrowser_Busy(cbhndl, %ID_WEB2) then
dialog set text cbhndl, "Busy ..."
else
dialog set text cbhndl, "Done!"
DIALOG kill TIMER cbhndl, %TM_WEB2_Busy
end if
end select
case %WM_DESTROY
'DIALOG kill TIMER cbhndl, %TM_WEB2_Busy
'case %WM_COMMAND
' select case cbctl
' case %ID_OK
' sStr = WebBRowser_Doc_GetElementValueById(cbhndl, %ID_WEB, "Input_text")
' msgbox 0, sStr
' sStr = WebBRowser_Doc_GetElementInnerHtmlById(cbhndl, %ID_WEB, "Output")
' msgbox 0, acode$(sStr)
'
' end select
END SELECT
end function
'------------------------------------------------------------------
callback function cb_Web2_Busy() as long
'------------------------------------------------------------------
end function
CallBack Function translate()
If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
Control Get Text hDlg, %tInput To txt
txt = "http://translate.google.com/#en|es|" + txt +
'txt = "http://translate.google.com/#en|es|I%20am%20an%20spanish%20girl "
txt = Replace$(txt, " ", "%20")
WebBrowser_Navigate2(CBHNDL, %ID_WEB2, txt)
'MsgBox 0,txt
End If
End If
End Function
largo_winch
16-11-2011, 12:20
hello, here's another approach for translation of words, but it's not perfect as I didn't know how to open and scan the vocabeldata file (fictive, it's full of language vocabel letters and words)
only test version for improving:
' Empty GUI script created on 11-15-2011 10:42:28 by largo_winch (ThinAIR)
Uses "UI", "console", "tokenizer"
' -- ID numbers of controls
Begin ControlID
%ButtonClose = 1001
%tEnglish
%tfrance
%tgerman
%tspain
%List01
%tInput
%tLoad
%tSave
%typein
%typeinlabel
End ControlID
Dim hDlg As DWord
Dim sFile As String
Dim sInput As String
Global vocab_engl() As String, item_english As Long
Global vocab_french() As String, item_french As Long
Global vocab_italian() As String, item_italian As Long
Global vocab_spain() As String, item_spain As Long
'------------------------------------------------------------>
Dialog New 0, "translate_proggy by lw",-1,-1, 580, 360, _
%WS_POPUP Or _
%WS_VISIBLE Or _
%WS_CLIPCHILDREN Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_MAXIMIZEBOX Or _
%WS_MINIMIZEBOX, _
0 To hDlg
Control Add Textbox hDlg, %tInput , "", 300, 25, 250, 280, %ES_AUTOHSCROLL Or %ES_LEFT Or %WS_BORDER Or %WS_TABSTOP Or %ES_MULTILINE Or %ES_WANTRETURN Or %WS_VSCROLL, %WS_EX_CLIENTEDGE Or %WS_EX_LEFT, Call pickup()
Control Add Button, hDlg, %ButtonClose, "Click to kill", 290, 5, 50, 18
Control Add Button, hDlg, %tLoad, "Load", 5, 5, 60, 14
Control Add Button, hDlg, %tSave, "SaveFile", 505, 5, 70, 14
Control Add Textbox, hDlg, %typein, "type in word for translation", 10, 330, 245, 20
Control Set Color hDlg, %typein, %BLUE, %YELLOW
Control Add Label, hDlg, %typeinlabel, "Type some language words and press <ENTER> key",12,310,180,12
Control Add Button, hDlg, %IDOK, "", 1000, 1000, 60, 14, Call Translate_Button
Dialog Show Modal hDlg Call cbDialog
CallBack Function cbDialog() As Long
Local hDlg As Long, mytext As Long
Local lRslt As Long
Dim vList(6) As String
vList(1) = "french"
vList(2) = "german"
vList(3) = "italian"
vList(4) = "spain"
vList(5) = "english"
vList(6) = "dutch"
Dim vList2(100) As String
vList2(1) = "Boy"
Select Case CBMSG
Case %WM_INITDIALOG
Control Add COMBOBOX, CBHNDL, %List01 , vList(),150,5, 100, 200, %CBS_DROPDOWNLIST Or %CBS_HASSTRINGS Or %WS_TABSTOP Or %WS_VSCROLL
COMBOBOX Select CBHNDL, %List01, 1
Control Add LISTBOX, CBHNDL,%tEnglish, vList2(), 5,25,250,280, %WS_VSCROLL Or %LBS_NOINTEGRALHEIGHT,%WS_EX_CLIENTEDGE, Call pickup()
'----------------------------------> largos part --------------------------------------------->
' readVocabulary ("ien.vcb",vocab_engl(),item_english,%id_iteminglese,"Vocaboli in english:")
' readVocabulary ("ifr.vcb",vocab_french(),item_french,%id_itemfrancese,"Vocaboli in french:")
' readVocabulary ("ita.vcb",vocab_italian(),item_italian,%id_itemtedesco,"Vocaboli in german:")
' readVocabulary ("isp.vcb",vocab_spain(),item_spain,%id_itemspain,"Vocaboli in spanish:")
Control Handle hDlg, %tInput To myText
Control Set Focus hDlg,%tInput
'----------------------------------> largos part --------------------------------------------->
Case %WM_COMMAND
If CBWPARAM = %ButtonClose Then Dialog End CBHNDL
If CBWPARAM = %tLoad Then
'If CBCTLMSG = %BN_CLICKED Then
sFile = OpenFile(CBHNDL)
If Len(sFile) Then
Control Set Text CBHNDL, %tInput,FILE_Load(sFile)
End If
End If
If CBWPARAM = %tSave Then
MsgBox 0, "here!"
If CBCTLMSG = %BN_CLICKED Then
sFile = SaveFile(CBHNDL)
If Len(sFile) Then
FILE_Save(sFile, Control_GetText(CBHNDL, %tInput))
End If
End If
End If
Case %WM_DESTROY
End Select
End Function
CallBack Function pickup()
Local hdlg As Long
Local str As String
Dim vList2(100) As String
vList2(1) = "Girl"
If CBCTLMSG = %LBN_SELCHANGE Or CBCTLMSG=%LBN_DBLCLK Then
LISTBOX Get Text hDlg, %tEnglish To str
MsgBox 0, "hello english"
LISTBOX Reset cbhndl,%tEnglish,vList2()
'LISTBOX RESET hWnd, ctrlID [, NewStringArray()]
End If
End Function
Function SaveFile(hDlg As DWord) As String
Dim sFile As String
Dim sFilter As String
sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"
sFile = Dialog_SaveFile(hDlg, _
"Save a file", _
APP_SourcePath, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST Or %OFN_HIDEREADONLY Or %OFN_ENABLESIZING)
Function = sFile
End Function
CallBack Function Translate_Button()
If CBCTLMSG = %BN_CLICKED Then
DoTheTranslation(CBHNDL, %typein, %tInput, "this is translated text.. " )
End If
End Function
Function DoTheTranslation(ByVal hDlg As DWord, ByVal ID_In As Long, ByVal ID_Out As Long, AdditionalText As String) As Long
Local counter As Long, sBuffer As String
Control Get Text hDlg, ID_In To sInput
If Trim$(sInput) = "" Then
sInput = "more.."
End If
Counter += 1
sInput = AdditionalText & Format$(Counter, "000") & " " & sInput
Control Get Text hDlg, ID_Out To sBuffer
Control AppendToTop Text hDlg, ID_Out, sInput & $CRLF
Control Set Text hDlg, ID_In, ""
Control Set Focus hDlg, ID_In
Control Set Text hDlg, %tInput, sInput
End Function
Function OpenFile(hDlg As DWord) As String
Dim sFile As String
Dim sFilter As String
sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"
sFile = Dialog_OpenFile(hDlg, _
"Open a file", _
APP_SourcePath, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST Or %OFN_HIDEREADONLY Or %OFN_ENABLESIZING)
Function = sFile
End Function
Sub proofLanguage(id As DWord,vocabeldata() As String,item As Long,words As String,idtexto As Long,texto As String)
Local j As Long,finding As Long
LISTBOX Reset hDlg,id
If words="!" Then Exit Sub
For j=1 To item
If LEFT$(words,1)="!" Then
If Ucase$(LEFT$(vocabeldata(j),Len(words)-1))=Ucase$(RIGHT$(words,Len(words)-1)) Then LISTBOX Add hDlg,id,vocabeldata(j):Incr finding
Else
If InStr(Ucase$(vocabeldata(j)),Ucase$(words)) Then LISTBOX Add hDlg,id,vocabeldata(j):Incr finding
End If
If finding>10 And Len(words)<3 Then Control Set Text hDlg,idtexto,"too few letters, looking more special..":Exit Sub
Next
Control Set Text hDlg,idtexto,texto+Str$(finding)+"."
End Sub
'-----------------------> SEARCHING CLASS IN DATA LANGUAGE FILE -------------->
Function text_searching_class (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Local lzStr As Asciiz * 128, lRet As Long
Static lString As String
Select Case wMsg
Case %WM_KEYUP
Case %WM_KEYDOWN
Case %WM_CHAR
Select Case wParam
Case %VK_BACK
lString=LEFT$(lString,Len(lString)-1)
proofLanguage %tenglish ,vocab_engl(),item_english,lString,%id_iteminglese,"In english"
proofLanguage %tfrance,vocab_french(),item_french,lString,%id_itemfrancese,"In french"
proofLanguage %tgerman ,vocab_italian(),item_italian,lString,%id_itemtedesco,"In german"
proofLanguage %tspain ,voc_spain(),item_spain,lString,%id_itemspain,"In spanish"
Case &h20 To &hff
lString = lString + Chr$(wParam)
proofLanguage %tenglish ,vocab_engl(),item_english,lString,%id_iteminglese,"In inglese"
proofLanguage %tfrance,vocab_french(),item_french,lString,%id_itemfrancese,"In francese"
proofLanguage %tgerman ,vocab_italian(),item_italian,lString,%id_itemtedesco,"In tedesco"
proofLanguage %tspain ,vocab_spain(),item_spain,lString,%id_itemspain,"In spanish"
Case &h0D
Select Case Ucase$(lString)
Case "DIR","TIME","BEEP","KILL" : lString = "*" + lString + "*"
End Select
lString = ""
End Select
End Select
End Function
'------------> not perfect but important part to scan file for vocabeldata ------------------------------------->
Sub readVocabulary(nameof_file As String,vocaboli() As String,item As Long,idtexto As Long,texto As String)
Local tmp As String,j As Long,tabulatore As Long
' tabulatore=Max%(tabulatore,InStr(tmp,"¦"))
Dim vocabeldata(1 To item)
For j=1 To item
vocaboli(j)=Extract$(vocabeldata(j),"¦")+$SPC(tabulatore-Len(Extract$(vocabeldata(j),"¦")))+Remain$(vocabeldata(j),"¦")
Next
Control Set Text hDlg,idtexto,texto+Str$(item)+"."
End Sub
hope this could help also for building data file with different language words (italian, english, spanish, french, german, dutch etcpp). my idea is not using internet and google translation.
bye, largo