CLC Translate Source Code updated

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

CLC Translate Source Code updated

Post by Zebuddi123 »

;
; Zebuddi. 16/1/2015

CLC helps in translation of text of strings in quotes and comments from one language to another via web translation.



1. Updated now has GetLastErrorAsText() from Droopys Lib. (Windows Only. Comment out for linux/mac) @ 1ine 198
2. Updated to Bing Translate which translates more accurately.
3. Compile with Unicode turn off in compiler options





Use:
1. In the FileRequester select the .pb/pb.i source file to translate.
2. Browser will open at bing translate.
3. Paste the copied quoted_strings/comments into bing translate.
4. Select Language to translate to
5. Copy the translated section. ( clc will recognized the correct clipboard clip & open clc with the data in place.)
6. Check translations of comments, quoted strings needs more attention when parsing.
7. Double Left Mouse Click in the translated (quoted strings/comented section of the the listview will open an editor window for the selected item. Closing the window modifies the selected entry.
8. Select checkbox for valid translations or all via select/de-select buttons to select unselect all at once.
9 Click send to ide button to open in the pbide.





;

Code: Select all


;  
;  Zebuddi. 17/01/2015
;

CompilerIf #PB_Compiler_Unicode
	Debug "This program needs to be CompiledF in Ascii mode"
CompilerEndIf

CompilerIf  #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS
ImportC ""
	errno_location() As "__errno_location"
	open_(s$, a.i, b.i) As "__open"
	strerror_(*address) As "__strerror"
EndImport
CompilerEndIf
    	


Structure tdata
   lineNbr.i                        ; line number for the part
   state.i                           ; qstring  or  a comment exists if #true
   orig.s                           ; original part
   trans.s                           ; translated part
EndStructure

Structure clc_data
   thisid.s                           ; id to show this is our translated data on the clipboard
   line_nbr.i                        ; current line postion number in the file  
   orig_codeline.s                  ; copy of original untouched  codeline
   translated_codeline.s            ; after translation   this holds the fully translated code 
   List comments.tdata( )          ; data relating to a  coment section if  in the current codeline
   List Qstrings.tdata( )             ; data relating to quotes string/s in the current codeline
EndStructure

Global Window_editor
Global CLCEditor_Text_Original
Global CLCEditor_Text_Translated
Global CLCEditor_Text_OriginalCodeLine
Global CLCEditor_String_Orig
Global CLCEditor_String_Trans
Global CLCEditor_String_Orig_codeline

Global CLCwindow                                 ; windows
Global StatusBar_CLCwindow

Global CLC_Window_ListIcon_Comments         ; listicons
Global CLC_Window_ListIcon_QuotedStrings

Global CLC_Window_Button_SellectAllComments; buttons
Global CLC_Window_Button_SelectAllQStrings
Global CLC_Window_Button_SendToIDE
Global CLC_Window_Button_Quit

Global CLCTranslateSourceCodeHelper_Frame   ; frames

Global ccbs.b                                       ;  state of the comments checkboxes
Global qscbs.b                                     ; state of the quoted strings checkboxes
Global hbp.b                                       ; counter  to show if  translation has been processed and sent to the ide on quitting the program  as a user warning
Global comment_count.i                           ; count of comments
Global qstring_count.I                            ; count of quoted strings

#MainWinX=393
#MainWinY=12
#MainWinWidth=1024
#MainWinHeight=768

Define.l Event

Global NewList clc_data.clc_data( )

Global collated_buffer_string.s                    ; collated comments and collated quoted strings to copy to clipboard
Global FileName.s                                 ; current file
Global FileName1.s                               ; current file  programparameter(0)   if used as an ide tool and not standalone tool via openfilerequester
Global FileName2.s                                 ;  current file  programparameter(0)   if used as an ide tool and not standalone tool via openfilerequester
Global qstring_count.i                              ; tally of all quoted strings found
Global  comment_count.i                           ; tallys all the comments in the file For display in the statusbar
Global  thisid.s="CLCDATAID4102"+Chr(13)         ; id so we know that this is our data

; ProcedureDLL.s GetLastErrorAsText(LastError.l) ; Used to get Last Win32 API Error
;   If LastError 
;     *ErrorBuffer = AllocateMemory(1024) 
;     FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, LastError, 0, *ErrorBuffer, 1024, 0) 
;     message.s=PeekS(*ErrorBuffer) 
;     FreeMemory(*ErrorBuffer) 
;   EndIf 
;   ProcedureReturn message
; EndProcedure

Procedure.b IsAlphaNumeric( string.s )             ; checks to see if the string contains alphanumeric chars  to be valid for processing  
   With clc_data()
      For i = 1 To Len( string ) 
         Select Asc( Mid( string, i, 1 )  ) 
            Case 48 To 57, 65 To 90, 97 To 122          ; range a-z A-Z  0-9
               ProcedureReturn #True
         EndSelect
      Next
      ProcedureReturn #False
   EndWith
EndProcedure

Procedure process_clc_data(FileName.s, List clc_data.clc_data( )  ) ; collates comment and qouted string sections form the source code file
   With clc_data()
      Protected regex_extract_quoted_strings.i    ; regular expression for exatrcting strings in " " quotes
      Protected a$                                    ; placeholder for the current codeline string read in from the file
      Protected c.i                                      ; counter for the current line number in the file
      Protected com.s                           ; collated comment`s  string after processing for the buffer to clipboard  for translating
      Protected coma_pos.i                     ; current position of the start of newly found comment  section
      Protected Nbr_quotes.i                     ; count the number of quotes in a given codeline to ensue that they are an even pair --  probablely redundant
      Protected q.s                              ; collated "quoted strings "string after processing for the buffer to clipboard for translating
      Protected comment_temp.s               ; temporary buffer to hold the extracted ( via Mid() ) comment section of the current codeine
      Protected i.i                                  ; for loop counter for processing the number of quoted strings in the current code line
      
      Protected Dim array_regextract.s( 0 )           ; array to collect regularepression extracted quoted strings
      
      regex_extract_quoted_strings.i = CreateRegularExpression( #PB_Any, Chr( 34 ) + ".*?" + Chr( 34 )  ) ; match anything in quotes + the quotes ie. "bla bla"
      
      If OpenFile( 0, FileName.s ) 
         While Not Eof( 0 ) 
            a$ = ReadString( 0, ReadStringFormat( 0 )  ) 
                        If FindString( a$, "; jaPBe Version", 1 ) Or FindString( a$, "; IDE Options = PureBasic", 1 ) ; bottom of the file not interested in any thing futher in  this file
                           Break 
                        EndIf
            AddElement( clc_data( )  ) 
            c + 1 : \line_nbr = c                                               ; sets up the line position in the file
            \orig_codeline = a$                                               ; copy of the the original untouched codeline
            coma_pos = FindString( a$, ";", 1 ) 
            If coma_pos
               comment_temp.s = Mid( a$, coma_pos ) 
               If IsAlphaNumeric( comment_temp ) = #True                ; make sure the comment is of interest and  not comment boxing & sectional isolation ie. ;//////////////////////////////////" etc
                  AddElement( \comments( )  ) 
                  comment_count + 1                                     ;  tallys all the comments in thefile
                  \comments( )\state = 1                                  ; comment scetion is valid
                  \comments()\lineNbr=c                                  ; comment at line number
                  \comments()\orig = comment_temp                       ; store an original copy of the comment
                  a$ = RemoveString( a$, \comments()\orig )                ;  remove the comment section from the processed string
                  comment_temp = ""
               EndIf
            Else
            EndIf
            
            Nbr_quotes = ExtractRegularExpression( regex_extract_quoted_strings, a$, array_regextract( )  ) ; extract all the qouted strings in the current codeline
            If Nbr_quotes
               For i = 0 To Nbr_quotes-1   
                  If IsAlphaNumeric( array_regextract( i )  ) = #True          ; make sure the quoted strinig is of interest and  not  paths directories etc " c:\" etc
                     AddElement( \Qstrings( )  ) 
                     qstring_count + 1                                        ; tally of all the qouted strings found
                     \Qstrings()\state = 1                                     ; quoted string is valid 
                     \Qstrings()\orig = array_regextract( i )                   ; extract quoted string from the array 
                  EndIf
               Next
               Nbr_quotes = 0 : a$ = "" : coma_pos = 0 
               FreeArray( array_regextract( )  )                               ; free the array for the current codeline 
               Dim array_regextract( 0 )                                     ; reinitialize  array for the next codeline
            EndIf   
        Wend
        
        CloseFile( 0 ) 
        
        ForEach clc_data( )                                                   ; collate all the quoted string/s ready for the buffer for the browser
        	ForEach \Qstrings( ) 
        		If \Qstrings()\state = 1
        			qs.s + \Qstrings()\orig + Chr( 13 ) 
        		EndIf
        	Next
        Next
        
        ForEach clc_data( )                                                  ; colates all the comments ready for the buffer for the browser  
        	ForEach \comments( ) 
        		If \comments()\state = 1
        			com.s + Chr( 34 ) + Mid( \comments()\orig, 2 ) + Chr( 34 ) + Chr( 13 ) 
        		EndIf
        	Next
        Next
        If com>""  Or qs>"" 
        	collated_buffer_string = thisid+com + qs                                      ; colate comments and quoted strings 
        	ClearClipboard( )
        	CallDebugger
        	If collated_buffer_string>""
        		SetClipboardText( collated_buffer_string )        ; clear and paste to the clipboard
        	Else
        		MessageRequester("Parsing Error","File "+FileName+Chr(13)+Chr(13)+"Has no parsable data.")
        		End
        	EndIf
        	; call the browser  at google translate page
        	CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        		RunProgram(  "http://www.bing.com/translator/")  ;"http://translate.google.com/" )                   ; win
        	CompilerElseIf   #PB_Compiler_OS = #PB_OS_MacOS		 ; Mac
        		RunProgram( "open",  "http://www.bing.com/translator/,"")  ;"http://translate.google.com/", "" ) 
        	CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
        		RunProgram( "xdg-open", "http://www.bing.com/translator/,"")  ;"http://translate.google.com/", "" )    ; - Linux?????
        	CompilerEndIf
        Else
        	MessageRequester("CLC Parse Error ","No Comments Or Quoted Strings Found"+Chr(10)+Chr(10)+"Program Ending")
        EndIf
    Else ; error messages for all OS`s
    	CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    		MessageRequester("File Error",GetLastErrorAsText(GetLastError_()) )
    		End
    	CompilerEndIf
    	
    	CompilerIf #PB_Compiler_OS = #PB_OS_Linux Or #PB_Compiler_OS = #PB_OS_MacOS	
    		MessageRequester("File Error", PeekS(strerror_(PeekL(errno_location()))))
    		End
    	CompilerEndIf	
    EndIf
EndWith
EndProcedure

Procedure Monitor_Clipboard( collated_buffer_string.s )       ; monitor the clipboard for changes and get conformation user has copied translated section from google translated else remonitor for next change
   With clc_data()
      Protected  new_translated_string.s                        ; get the current contents of the clipboard
      Protected  mr_true.b                                       ; if contents of the clipboard has changed and the contents is the translation  copied by the user
      Protected done.b                                          ; we have finished monitoring and gotten the users translated data
      Protected mr_data.s                                        ; sample of the translated data from the clipboard to show to the user for confirmation  data is correct 

      Repeat 
      	new_translated_string.s = GetClipboardText( ) 
      	Delay(1000)
      	If new_translated_string.s <> collated_buffer_string.s    ; clipboard buffer changed
      		cc+1
      		Debug Str(cc)+"."
      	Debug new_translated_string
         	CallDebugger
            yy.s= Left(new_translated_string,13)
            new_translated_string=Mid(new_translated_string,16)

            If RemoveString(Left(yy,13),Chr(13))=RemoveString(thisid,Chr(13))
               done=1
            Else
               done=0                                             ; keep monitoring the clipboard
               Delay( 10 )                                           ; check if buffer change ever 1/100th second
            EndIf
         EndIf
      Until done.b = 1                                              ; quit monitoring
      
      If CreateFile( 0, GetTemporaryDirectory( ) + "clctemp" )    ; creat a temporary file  to process 
         WriteString( 0, new_translated_string, #PB_UTF8) 
         CloseFile( 0 ) 
      EndIf
   EndWith
EndProcedure

Procedure.s rect_trans_strings( rts.s )                         ; rts =  translated string
   ; rectify some of google persistant errors in translation that effect  quoted string 
   ; leaving the strings encapsulated in the quotes " bla bla "  improve gooles translation
   rts = ReplaceString( rts, " \ ", "\" ) 
   rts = ReplaceString( rts, "% ", "%" ) 
   rts = ReplaceString( rts, " : ", ":" ) 
   rts = ReplaceString( rts, " ) ", " ) " ) 
   rts = ReplaceString( rts, "( ", "( " ) 
   rts = ReplaceString( rts, " " + Chr( 34 ), Chr( 34 )  ) 
   rts = ReplaceString( rts, Chr( 34 ) + " ", Chr( 34 )  ) 
   rts = ReplaceString( rts, " $", "$" ) 
   rts = ReplaceString( rts, "$ ", "$" ) 
   rts = ReplaceString(rts, " / ", "/" )
   
   ProcedureReturn  rts
EndProcedure

Procedure  process_translated_data( )                         ; collect and process data for browseer translation
   With clc_data()
      Protected a$                                               ; current line buffer 
      
      If ReadFile( 0, GetTemporaryDirectory( ) + "clctemp" )  
         While Not Eof( 0 ) 
            
            ForEach clc_data( )                               ; comments section translation
               ForEach \comments( ) 
                  If \comments()\state = 1
                     a$ = ReadString( 0, #PB_UTF8 ) 
                     a$ = Mid( a$, 2 ) 
                     a$ = Left( a$, Len( a$ ) -1 ) 
                     \comments()\trans = ";  " + Trim( rect_trans_strings( a$ )  ) 
                  EndIf
               Next
            Next
            
            ForEach clc_data( )                               ; qstrings section translation
               ForEach \Qstrings( ) 
                  If \Qstrings()\state = 1
                     a$ = ReadString( 0, #PB_UTF8 ) 
                     \Qstrings()\trans = rect_trans_strings( a$ ) 
                  EndIf
               Next
            Next
            
         Wend
         CloseFile( 0 ) 
      EndIf
   EndWith
EndProcedure

Procedure OpenWindow_CLCwindow( )                      ; Main GUI
   CLCwindow = OpenWindow( #PB_Any, #MainWinX , #MainWinY, #MainWinWidth, #MainWinHeight, "CLC Translate( Source Code ) Helper", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_WindowCentered | #PB_Window_ScreenCentered ) 
   If CLCwindow
      StatusBar_CLCwindow = CreateStatusBar( #PB_Any, WindowID( CLCwindow )  ) 
      If StatusBar_CLCwindow
         AddStatusBarField( 512 ) 
         AddStatusBarField(512)
      EndIf
      CLC_Window_ListIcon_Comments = ListIconGadget( #PB_Any, 0, 55, 1024, 345, "Original Comment", 475, #PB_ListIcon_AlwaysShowSelection | #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines ) 
      AddGadgetColumn( CLC_Window_ListIcon_Comments, 1, "Translated  Comment", 475 )
      AddGadgetColumn( CLC_Window_ListIcon_Comments , 2, "@Line",50)
      CLC_Window_ListIcon_QuotedStrings = ListIconGadget( #PB_Any, 0, 402, 1024, 345, "Original Quoted Strings", 475, #PB_ListIcon_AlwaysShowSelection | #PB_ListIcon_CheckBoxes | #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines ) 
      AddGadgetColumn( CLC_Window_ListIcon_QuotedStrings, 1, "Translated Quoted String", 475 ) 
      AddGadgetColumn( CLC_Window_ListIcon_QuotedStrings , 2, "@Line",50)
      CLC_Window_Button_SellectAllComments = ButtonGadget( #PB_Any, 170, 15, 150, 25, "DE/SELECT All Comments" ) 
      CLC_Window_Button_SelectAllQStrings = ButtonGadget( #PB_Any,320, 15, 150, 25, "DE/SELECT All QStrings" ) 
      CLC_Window_Button_SendToIDE = ButtonGadget( #PB_Any, 570, 15, 100, 25, "Send To IDE" ) 
      CLC_Window_Button_Quit = ButtonGadget( #PB_Any, 670, 15, 100, 25, "Quit" ) 
      CLCTranslateSourceCodeHelper_Frame3D_14 = FrameGadget(#PB_Any, 2, 5, 1019, 47, "")
   EndIf
EndProcedure

Procedure OpenWindow_Window_editor(winpos.i)         ; editor window  for fine tuning translation 
   Window_editor = OpenWindow(#PB_Any, #PB_Ignore, winpos, 1024, 79, "CLC_Editor", #PB_Window_TitleBar|#PB_Window_SystemMenu|#PB_Window_WindowCentered)
   If Window_editor
      
      CLCEditor_Text_OriginalCodeLine  = TextGadget(#PB_Any, 0, 5, 90, 20, "Original CodeLine", #PB_Text_Center)
      CLCEditor_Text_Original = TextGadget(#PB_Any, 0, 30, 80, 20, "Original", #PB_Text_Center)
      CLCEditor_Text_Translated = TextGadget(#PB_Any, 0, 55, 80, 20, "Translated",#PB_Text_Center)
      
      CLCEditor_String_Orig_codeline = StringGadget(#PB_Any, 90, 5, 933, 21, "",#PB_String_ReadOnly)
      CLCEditor_String_Orig = StringGadget(#PB_Any, 90, 30, 933, 20, "",#PB_String_ReadOnly)
      CLCEditor_String_Trans = StringGadget(#PB_Any, 90, 55, 933, 20, "")
      
   EndIf
   ;    ResizeWindow(Window_editor,#PB_Ignore,winpos,1024,79)
      StickyWindow(Window_editor, #True)
EndProcedure

Procedure Fill_ListVeiwGadget( List clc_data.clc_data( )  )                   ; fills the listview with data after translation copied from google translate
   With clc_data()
      ResetList( clc_data( )  ) : FirstElement( clc_data( )  ) 
      ForEach clc_data( ) 
         ForEach \comments( ) 
            If \comments()\state = 1
               AddGadgetItem( CLC_Window_ListIcon_Comments, -1, \comments()\orig + Chr( 10 ) + \comments()\trans+Chr(10)+\line_nbr ) 
            EndIf
         Next
      Next
      
      ForEach clc_data( ) 
         ForEach \Qstrings( ) 
            If \Qstrings()\state = 1
               AddGadgetItem( CLC_Window_ListIcon_QuotedStrings, -1, \Qstrings()\orig + Chr( 10 ) + \Qstrings()\trans+Chr(10)+\line_nbr ) 
            EndIf
         Next
      Next
      StatusBarText( StatusBar_CLCwindow, 0, "Comments: "+Str(comment_count) ) 
         StatusBarText( StatusBar_CLCwindow, 1, "QStrings: "+Str(qstring_count) ) 
      
   EndWith
EndProcedure

Procedure disable_listicons()
   DisableGadget(CLC_Window_ListIcon_QuotedStrings,#True)
   DisableGadget(CLC_Window_ListIcon_Comments,#True)
EndProcedure

Procedure enable_listicons()
   DisableGadget(CLC_Window_ListIcon_QuotedStrings,#False)
   DisableGadget(CLC_Window_ListIcon_Comments,#False)
EndProcedure

Procedure quoted_strings_editor(pos.i)                      ;event double LMB clicks  activates the editor  which you can then manually  edit the cotents for further precision   
   With clc_data()
      Protected gad_qstring_orig.s                              ; original quoted string  in the listicon gadget 
      Protected gad_qstring_trans.s                            ; translated quoted string in the listicon gadget
      disable_listicons()
      OpenWindow_Window_editor(pos.i)
      SetWindowTitle(Window_editor,"CLC_Editor  Quoted String Section    @Line "+Str(clc_data()\line_nbr))
      
      gad_qstring_orig = GetGadgetItemText(CLC_Window_ListIcon_QuotedStrings , GetGadgetState(CLC_Window_ListIcon_QuotedStrings),0) 
      gad_qstring_trans = GetGadgetItemText(CLC_Window_ListIcon_QuotedStrings,GetGadgetState(CLC_Window_ListIcon_QuotedStrings),1)
      
      SetGadgetText(CLCEditor_String_Orig,gad_qstring_orig)   
      SetGadgetText(CLCEditor_String_Trans,gad_qstring_trans)
      
      ; search through list to find original codeline
      ForEach clc_data() 
         ForEach clc_data()\Qstrings()
            If clc_data()\Qstrings()\state=1 And clc_data()\Qstrings()\orig=GetGadgetItemText(CLC_Window_ListIcon_QuotedStrings,GetGadgetState(CLC_Window_ListIcon_QuotedStrings))
               SetGadgetText(CLCEditor_String_Orig_codeline,Trim(clc_data()\orig_codeline))
               SetWindowTitle(Window_editor,"CLC_Editor  Quoted String Section    @Line "+Str(clc_data()\line_nbr))
            EndIf
         Next
      Next
      
      Repeat ; on close editor window check to see it the translated quoted string has been  user updated  if true replace in linkedlink \ comment()\trans and refresh listiconview
         event = WaitWindowEvent()
         Select event
            Case #PB_Event_CloseWindow
               Select EventWindow()
                  Case Window_editor
                     
                     currline=Val(GetGadgetItemText(CLC_Window_ListIcon_QuotedStrings,GetGadgetState(CLC_Window_ListIcon_QuotedStrings),2))  ; get current line number
                     newtrans.s=GetGadgetText(CLCEditor_String_Trans) ; get the new updated  trans qstring
                     ForEach clc_data()
                        If \line_nbr=currline
                           ForEach \Qstrings()
                              If FindString(\Qstrings()\orig,GetGadgetItemText(CLC_Window_ListIcon_QuotedStrings,GetGadgetState(CLC_Window_ListIcon_QuotedStrings),0),1)
                                 If \Qstrings()\trans<>newtrans
                                    \Qstrings()\trans=newtrans
                                    j.s= \Qstrings()\orig+Chr(10)+\Qstrings()\trans+Chr(10)+Str(\line_nbr)
                                    ClearGadgetItems(CLC_Window_ListIcon_QuotedStrings)
                                    ClearGadgetItems(CLC_Window_ListIcon_Comments)
                                    Fill_ListVeiwGadget(clc_data())
                                 EndIf
                              EndIf
                           Next
                        EndIf
                     Next
                     
                     CloseWindow( Window_editor ) 
                     Break
               EndSelect
         EndSelect
      ForEver
      enable_listicons()
EndWith
EndProcedure

Procedure comment_strings_editor(pos.i)                     ;event double LMB clicks  activates the editor  which you can then manually  edit the cotents for further presision  
   Protected currline.i         ; currentline in the CLC_Window_LIstIcon_Comments
   Protected newtrans.s     ; user updataed translated string
   With clc_data()
      disable_listicons()
      OpenWindow_Window_editor(pos.i)
      SetWindowTitle(Window_editor,"CLC_Editor Comment Section")
      gad_comment_orig.s=GetGadgetItemText(CLC_Window_ListIcon_Comments,GetGadgetState(CLC_Window_ListIcon_Comments),0)
      SetGadgetText(CLCEditor_String_Orig , gad_comment_orig)   
      gad_comment_trans.s=GetGadgetItemText(CLC_Window_ListIcon_Comments,GetGadgetState(CLC_Window_ListIcon_Comments),1)
      SetGadgetText(CLCEditor_String_Trans , gad_comment_trans)
      ; search through list to find original codeline
      ForEach clc_data() 
         ForEach clc_data()\comments()
            If clc_data()\comments()\state=1  And clc_data()\comments()\orig=GetGadgetItemText(CLC_Window_ListIcon_Comments,GetGadgetState(CLC_Window_ListIcon_Comments))
               SetWindowTitle(Window_editor,"CLC_Editor  Comment Section    @Line "+Str(clc_data()\line_nbr))
               SetGadgetText(CLCEditor_String_Orig_codeline,Trim(clc_data()\orig_codeline))
            EndIf
         Next
      Next
      
      Repeat   ; on close editor window check to see it the translated comment has been  user updated  if true replace in linkedlink \ comment()\trans and refresh listiconview
         event = WaitWindowEvent()
         Select event
            Case #PB_Event_CloseWindow
               Select EventWindow()
                  Case Window_editor
                  currline=Val(GetGadgetItemText(CLC_Window_ListIcon_Comments,GetGadgetState(CLC_Window_ListIcon_Comments),2))  ; get current line number
                     newtrans.s=GetGadgetText(CLCEditor_String_Trans) ; get the new updated  trans qstring
                     ForEach clc_data()
                        If \line_nbr=currline
                           ForEach \comments()
                              If FindString(\comments()\orig,GetGadgetItemText(CLC_Window_ListIcon_Comments,GetGadgetState(CLC_Window_ListIcon_Comments),0),1)
                                 If \comments()\trans<>newtrans
                                    \comments()\trans=newtrans
                                    j.s= \comments()\orig+Chr(10)+\comments()\trans+Chr(10)+Str(\line_nbr)
                                    ClearGadgetItems(CLC_Window_ListIcon_QuotedStrings)
                                    ClearGadgetItems(CLC_Window_ListIcon_Comments)
                                    Fill_ListVeiwGadget(clc_data())
                                 EndIf
                              EndIf
                           Next
                        EndIf
                     Next
                     CloseWindow( Window_editor ) 
                     Break
               EndSelect
         EndSelect
      ForEver
   enable_listicons()
   EndWith
EndProcedure

Procedure Process_CheckBoxes(GadID.i,mode)                ; sets all checkboxes checked/unchecked   
   
   Protected i.i
   Select mode
      Case 0
         For i = 0 To CountGadgetItems(GadID)
            SetGadgetItemState(GadID, i ,#PB_ListIcon_Checked)
         Next
         
      Case 1
         For i = 0 To CountGadgetItems(GadID)
            SetGadgetItemState(GadID, i ,1)
         Next
   EndSelect
EndProcedure

Procedure process_to_IDE()
   With clc_data()
      ForEach clc_data( )                                     ; process through temp translation file and extract  relative quoted string or comment section 
         \translated_codeline = \orig_codeline
         ForEach \Qstrings( ) 
            If \Qstrings()\state = 1 And GetGadgetItemState(CLC_Window_ListIcon_QuotedStrings, i )=#PB_ListIcon_Checked
               \translated_codeline = ReplaceString( \translated_codeline, \Qstrings()\orig, \Qstrings()\trans ) ; place translated quoted string to linkedlist
            EndIf
         Next
         
         If ListSize( \comments( )  ) >0                         ; if we have a valid comment section
            If \comments()\state = 1  And GetGadgetItemState(CLC_Window_ListIcon_Comments, i )=#PB_ListIcon_Checked                            ; probably redundant 
               \translated_codeline = ReplaceString( \translated_codeline, \comments()\orig, \comments()\trans ) ; place translated comment to linkedlist 
            EndIf
         EndIf
      Next
      
      
      
      If CreateFile(0,GetPathPart(FileName)+"Trans_"+GetFilePart(FileName))
         ForEach clc_data()
            WriteStringN(0, clc_data()\translated_codeline)
         Next
         CloseFile(0)
         RunProgram(GetPathPart(FileName)+"Trans_"+GetFilePart(FileName))
      Else
         MessageRequester("File Error","Failed to create file "+Chr(13)+GetPathPart(FileName)+"Trans_"+GetFilePart(FileName))
      EndIf
   EndWith
EndProcedure

Procedure quitprogram()
   With clc_data()
      If hbp > 0                                                     ; translation has been processed so we can quit 
         FreeList(clc_data())
         CloseWindow(CLCwindow)
         End
      Else
         qresult=MessageRequester("Translation Notice","TRANSLATION has not been PROCESSED and sent to the IDE !"+Chr(13)+"DO YOU WANT TO QUIT THE PROGRAM ?", #PB_MessageRequester_YesNo)
         If qresult=#PB_MessageRequester_Yes
            FreeList(clc_data())
            CloseWindow(CLCwindow)
            End
         EndIf
      EndIf
   EndWith
EndProcedure


;-  ---   Main
With clc_data()
   

   ;{ -   can be used as IDE Tool  or  standalone  program     
   ;       If   IDE Tool   set BOTH tool arguments  "%FILENAME""%TEMPFILE"
   FileName1=ProgramParameter(0) ; %FILENAME
   FileName2=ProgramParameter(1) ; %TEMPFILE
   
   If FileName1=""
      If FileName2>""
         FileName=FileName2
      Else
         FileName = OpenFileRequester( "Select a PureBasic Source File", "*.pb","PureBasic (*.pb)|*.pb|IncludeFiles (*.pbi)|*.pbi", 0 ) ; AS A STANDALONE TOOL
      EndIf
   Else
      FileName=FileName1
   EndIf
   ;}
   
   
   If FileName
      
      process_clc_data( FileName , clc_data( )  ) 
      Monitor_Clipboard( collated_buffer_string ) 
      process_translated_data( ) 
      OpenWindow_CLCwindow( ) 
      SetWindowTitle(CLCwindow, "CLC Translate( Source Code ) Helper    File:- "+GetFilePart(FileName.s))
      Fill_ListVeiwGadget( clc_data( )  ) 
      
      Repeat
         Event = WaitWindowEvent( ) 
         Select Event
            Case #PB_Event_Gadget
               Select EventGadget( ) 
                     
                  Case CLC_Window_Button_SellectAllComments
                     Process_CheckBoxes(CLC_Window_ListIcon_Comments,ccbs)                     ; select/de-select all check boxes for comments
                     If ccbs=0 : ccbs=1 : Else: ccbs=0 : EndIf                                              ; global state of the checkboxes
                  Case CLC_Window_Button_SelectAllQStrings
                     Process_CheckBoxes(CLC_Window_ListIcon_QuotedStrings,qscbs)               ; select/de-select all check boxes for quoted strings
                     If qscbs=0 : qscbs=1 : Else : qscbs=0 : EndIf                                           ; global state of the checkboxes
                     
                  Case CLC_Window_Button_SendToIDE                                                   
                     process_to_IDE()                                                                      ; process all user selected translations   and send to the IDE
                     hbp+1                                                                               ; counter - translation has been processed and sent to the ide +1 times         
                     
                  Case CLC_Window_Button_Quit
                     quitprogram()
               EndSelect
               
               Select  EventType()
                     
                  Case #PB_EventType_LeftDoubleClick
                     
                     Select EventGadget()
                           
                        Case CLC_Window_ListIcon_Comments
                           comment_strings_editor((#MainWinHeight/2)+120)
                           
                        Case CLC_Window_ListIcon_QuotedStrings
                           quoted_strings_editor((#MainWinHeight/2)-120)
                           
                     EndSelect
                     
               EndSelect
               ;=====================================================
            Case #PB_Event_CloseWindow
               Select EventWindow( ) 
                  Case CLCwindow
                     quitprogram()
               EndSelect
         EndSelect
      ForEver
      
      
      FreeList( clc_data( )  ) 
   Else
      MessageRequester("Closing Program","User Cancelled")
      End
   EndIf
EndWith
Last edited by Zebuddi123 on Sat Jan 17, 2015 6:44 pm, edited 11 times in total.
malleo, caput, bang. Ego, comprehendunt in tempore
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: CLC Translate Source Code

Post by IdeasVacuum »

Hi Zebuddi, thanks for persevering with CLC, it's very handy.
Line 166: GetLastErrorAsText() is not a function, array, list, map or macro.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: CLC Translate Source Code

Post by Zebuddi123 »

@IdeasVacuum GetLastErrorAsText() from droopy`s lib. I have re-uploaded the code with this procedure included, so re-copy the source code from that post.

Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: CLC Translate Source Code

Post by IdeasVacuum »

ah thank you :D

Folks, line 176 should read:

Code: Select all

MessageRequester("File Error",GetLastErrorAsText(GetLastError_()))
GetLastError_() being an API function.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: CLC Translate Source Code

Post by Zebuddi123 »

@IdeasVacuum whoops :oops: forgot that one updated the source.

Zebuddi. :lol:
malleo, caput, bang. Ego, comprehendunt in tempore
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: CLC Translate Source Code

Post by Tenaja »

IdeasVacuum wrote:Hi Zebuddi, thanks for persevering with CLC, it's very handy.
Line 166: GetLastErrorAsText() is not a function, array, list, map or macro.
I found the procedure here:
http://forums.purebasic.fr/french/viewtopic.php?p=44570
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 796
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: CLC Translate Source Code updated

Post by Zebuddi123 »

Code updated and re-posted linux and mac ( GetLastErrorAsText() equivalent not tested ) @ lines 197-205

Zebuddi. :)
malleo, caput, bang. Ego, comprehendunt in tempore
Post Reply