Translation Stuff

Mac OSX specific forum
User avatar
Piero
Addict
Addict
Posts: 1214
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Translation Stuff

Post by Piero »

Code: Select all

(* 
AppleScript IDE Tool to translate selection (you must be online)
NEEDS:
https://raw.githubusercontent.com/soimort/translate-shell/gh-pages/trans (sudo chmod +x /usr/local/bin/trans)
https://formulae.brew.sh/formula/gawk
*)
activate
set a to (choose from list {"en", "fr", "de", "ru", "it"} cancel button name "Auto Detect" default items {"ru"}) as string
set a to a & ": "
if a is "false: " then set a to ""

tell application "System Events"
	try -- tools panel auto-hide (PB 6.30)
		set sel to value of attribute "AXSelectedText" of text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of window 1 of application process "PureBasic"
	on error -- tools panel always visible (PB 6.30)
		set sel to value of attribute "AXSelectedText" of text area 1 of scroll area 1 of group 1 of group 1 of splitter group 1 of splitter group 1 of window 1 of application process "PureBasic"
	end try
	try
		set sel to (do shell script "export PATH=\"/opt/homebrew/bin:$PATH\";/usr/local/bin/trans -b -no-auto " & a & quoted form of sel)
		set btn to button returned of (display dialog sel with title "Translation" with icon caution buttons {"Cancel", "Replace"} default button 2)
		tell application "PureBasic" to activate
		tell application process "PureBasic" to keystroke (sel as string)
	end try
end tell

Code: Select all

; Translates all comments of copied PB code (you must be online)
; NEEDS:
; https://github.com/soimort/translate-shell
; https://formulae.brew.sh/formula/gawk (https://www.gnu.org/software/gawk/)
; Example test (auto-detect fails):
;  Индефикация гаджета
EnableExplicit
Define pbsource$ = GetClipboardText() ; analyze copied PB code
#mynl = #LF$ ; new line char

Procedure.s QP(str$) ; quote shell paths etc.
   ProcedureReturn "'" + ReplaceString(str$, "'", "'\''") + "'"
EndProcedure

Procedure.s Trans(sh.s,s.s)
   Protected Err$, tmper$, Output$, shell, Exc = -1 ; -1 on failed launch
   shell = RunProgram(sh,"-l","",
      #PB_Program_Open|#PB_Program_Write|#PB_Program_Read|#PB_Program_Error )
   If shell
      WriteProgramStringN(shell,s)
      WriteProgramData(shell,#PB_Program_Eof,0)
      While ProgramRunning(shell)
         If AvailableProgramOutput(shell)
            Output$ + ReadProgramString(shell)
         EndIf
         tmper$ = ReadProgramError(shell)
         If tmper$ : Err$ + tmper$ + #mynl : EndIf
      Wend
      Exc = ProgramExitCode(shell) : CloseProgram(shell)
   EndIf
   If Err$ Or ExC
      ;Debug Err$
      ProcedureReturn s
   EndIf
   ProcedureReturn Output$
EndProcedure

Define MyAScr.s = ~"osascript\n"+
~"tell app \"finder\"\n"+
~"activate\n"+
~"set a to (choose from list {\"en\", \"fr\", \"de\", \"ru\", \"it\"} cancel button name \"Auto Detect\" default items {\"ru\"}) as string\n"+
~"set a to a & \": \"\n"+
~"if a is \"false: \" then set a to \"\"\n"+
~"a\n"+
~"end"

#_PBCOMMENT = ~"(\\\\\"|\"(?:\\\\\"|[^\"])*\"|(\\+))|;[\\s]*(?<pbcomment>.+)"
CreateRegularExpression(0, #_PBCOMMENT)

Define nl, k, lang$ = Trans("sh",MyAScr), sum$, pbcm_match$, pbsourceline$

nl = CountString(pbsource$, #mynl) + 1
For k = 1 To nl
   pbsourceline$ = StringField(pbsource$, k, #mynl)
   If pbsourceline$ And ExamineRegularExpression(0, pbsourceline$)
      While NextRegularExpressionMatch(0)
         pbcm_match$ = RegularExpressionNamedGroup(0, "pbcomment")
         If Trim(pbcm_match$) ; also handles strange unicode lookahead "empty match" cases…
            pbsourceline$ = ReplaceString(pbsourceline$, pbcm_match$, Trans("zsh","trans -b -no-auto "+ lang$ + QP(pbcm_match$)),
               #PB_String_CaseSensitive, RegularExpressionMatchPosition(0), 1)
         EndIf
      Wend
   EndIf
   sum$ + pbsourceline$ + #mynl
Next
sum$ = Left(sum$,Len(sum$)-Len(#mynl))
Debug sum$