Marked as: Advanced
Hello,
See example on how to fetch pages, parse the text to retrieve translations between the 6 languages available on InterGlot (
http://www.interglot.com )
You may use this example to try other translation services as well.
Note: InterGlot return text as CodePage 1252 (1004) while OS/2 and eComStation often use 850, 437 etc. The included script depend on Alex Taylors addon (see link in code below) to convert text between codepages so that you can get the correct translation. I've also tested other similar addons available and they work as well, you just have to change some params and function names to use those instead.
//Jan-Erik
Prerequisite: Save code to iGlot.cmd
Usage: iGlot English Deutsch redundant
/* Interglot translation */
'@echo off'
/* Load Rexx Utility functions */
CALL RxFuncAdd 'SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs'
CALL SysLoadFuncs
/* Load UniCode and CodePage functions */
/* Alex Taylors addon, http://hobbes.nmsu.edu/pub/os2/dev/rexx/rxuls_052.zip */
CALL RxFuncAdd 'ULSLoadFuncs', 'RXULS', 'ULSLoadFuncs'
CALL ULSLoadFuncs
lang.from = SUBWORD( ARG(1), 1, 1 )
lang.to = SUBWORD( ARG(1), 2, 1 )
cfg.from_word = ULSTransform( SUBWORD( ARG(1), 3 ), 'lower' )
GlotCP = 1004
if stream( 'in.tmp', 'c', 'close' ) = 'READY:' then
retval = SysFileDelete( 'in.tmp' )
if stream( 'in.tmp', 'c', 'query exists' ) \= '' then
retval = SysFileDelete( 'in.tmp' )
lang.0 = 6
lang.1 = 'Nederlands' /* Dutch */
lang.2 = 'English' /* English */
lang.3 = 'Deutsch' /* German */
lang.4 = 'Français' /* French */
lang.5 = 'Español' /* Spanish */
lang.6 = 'Svenska' /* Swedish */
do i = 1 to lang.0
if translate( lang.from ) = translate( lang.i ) then
do
cfg.from_lang = i
leave i
end
end
do i = 1 to lang.0
if translate( lang.to ) = translate( lang.i ) then
do
cfg.to_lang = i
leave i
end
end
call lineout 'out.tmp', "word="||ULSConvertCodepage( cfg.from_word,, GlotCP )||"&SrcLang="||cfg.from_lang||"&DstLang="||cfg.to_lang
if stream( 'out.tmp', 'c', 'close' ) = 'READY:' then
'@wget -O "in.tmp" --post-file=out.tmp -nv -t 2 -c -T 60 http://interglot.com/resultpage.php'
if stream( 'out.tmp', 'c', 'query exists' ) \= '' then
retval = SysFileDelete( 'out.tmp' )
a = 1
i = 1
j = 0
do while lines( 'in.tmp' ) > 0
retval = linein( 'in.tmp' )
parse value retval WITH .'scodes["'num'"] = "'Scodes.a'"'.
if length( Scodes.a ) > 0 then
do
Scodes.num = Scodes.a
a = a + 1
end
parse value retval WITH .'SrcStar['num'] = "'SrcStar.i'";'.
if length( SrcStar.i ) > 0 then
do
SrcStar.i = ULSConvertCodepage( SrcStar.i, GlotCP )
parse value linein( 'in.tmp' ) with .'SrcCodes['.'"'SrcCodes.i'";'.
if pos( 'v', SrcCodes.i ) > 0 then
SrcCodes.i.conj = 1
else
SrcCodes.i.conj = 0
i = i + 1
end
parse value retval WITH .'DestWords['chk']['num']="'Dest.Wrd'";'.
if length( Dest.Wrd ) > 0 then
do
parse value linein( 'in.tmp' ) WITH .'DestCodes['chk_2']['num_2']="'Dest.Cde'";'.
if chk = chk_2 & num = num_2 then
do
if SrcStar.j.0 > num then
j = j + 1
num = num + 1
SrcStar.j.0 = num
SrcStar.j.num = ULSConvertCodepage( Dest.Wrd, GlotCP )
SrcCodes.j.num = Dest.Cde
if pos( 'v', SrcCodes.j.num ) > 0 then
SrcCodes.j.num.conj = 1
else
SrcCodes.j.num.conj = 0
end
end
end
call stream 'in.tmp', 'c', 'close'
drop Dest.
Scodes.0 = a
SrcStar.0 = i - 1
do i = 1 to SrcStar.0
num = left( SrcCodes.i, 1 )
if DataType( num, 'N' ) then
if Scodes.num \= 'SCODES.'num then
say cfg.from_lang SrcCodes.i '['Scodes.num'] ('SrcStar.i')' SrcCodes.i.conj
else
say cfg.from_lang SrcCodes.i '('SrcStar.i')' SrcCodes.i.conj
else
say cfg.from_lang SrcCodes.i '('SrcStar.i')' SrcCodes.i.conj
do j = 1 to SrcStar.i.0
Say cfg.to_lang SrcCodes.i.j SrcStar.i.j SrcCodes.i.j.conj
end
end