Marked as: Advanced
Hello,
here's some code for you to try out... it can help you translate between the languages available.
You may want to rewrite some of the reconition code to allow alternative ways of specifying the right language. You have to type each language right to make it work, with upper case of the first letter etc. as it look now.
Save the code to googletranslate.cmd and try it.
Usage: Googletranslate English German This is my first attemt to use Google for translations
Note that it may not be 100% accurate, but you should be able to get a fairly decent translation anyway that you just have to modify a bit.
Prerequisite: Unicode conversion library as specified in similar topic.
Self study: Ways to extract data from html code.
//Jan-Erik
/* Google 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 = SUBWORD( ARG(1), 3 )
CurrCP = ULSQueryLocaleItem( 'iCodePage' )
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 = 24
lang.1.code = 'auto'
lang.1 = 'Detect Automatically'
lang.2.code = 'ar'
lang.2 = 'Arabic'
lang.3.code = 'bg'
lang.3 = 'Bulgarian'
lang.4.code = 'zh-CN'
lang.4 = 'Chinese'
lang.5.code = 'hr'
lang.5 = 'Croatian'
lang.6.code = 'cs'
lang.6 = 'Czech'
lang.7.code = 'da'
lang.7 = 'Danish'
lang.8.code = 'nl'
lang.8 = 'Dutch'
lang.9.code = 'en'
lang.9 = 'English'
lang.10.code = 'fi'
lang.10 = 'Finnish'
lang.11.code = 'fr'
lang.11 = 'French'
lang.12.code = 'de'
lang.12 = 'German'
lang.13.code = 'el'
lang.13 = 'Greek'
lang.14.code = 'hi'
lang.14 = 'Hindi'
lang.15.code = 'it'
lang.15 = 'Italian'
lang.16.code = 'ja'
lang.16 = 'Japanese'
lang.17.code = 'ko'
lang.17 = 'Korean'
lang.18.code = 'no'
lang.18 = 'Norwegian'
lang.19.code = 'pl'
lang.19 = 'Polish'
lang.20.code = 'pt'
lang.20 = 'Portuguese'
lang.21.code = 'ro'
lang.21 = 'Romanian'
lang.22.code = 'ru'
lang.22 = 'Russian'
lang.23.code = 'es'
lang.23 = 'Spanish'
lang.24.code = 'sv'
lang.24 = 'Swedish'
do i = 1 to lang.0
if lang.from = lang.i then /* modify to allow similar ways of decribe the language */
do
cfg.from_lang = lang.i.code
leave i
end
end
do i = 1 to lang.0
if lang.to = lang.i then /* modify to allow similar ways of decribe the language */
do
cfg.to_lang = lang.i.code
leave i
end
end
call lineout 'out.tmp', "hl="||cfg.to_lang||"&ie=UTF8&text="||translate( ULSConvertCodepage( cfg.from_word, CurrCP, GlotCP ), '+', ' ' )||"&sl="||cfg.from_lang||"&tl="||cfg.to_lang
if stream( 'out.tmp', 'c', 'close' ) = 'READY:' then
'@wget -O "in.tmp" -U "Google Harvester" --post-file=out.tmp -nv -t 2 -c -T 60 http://translate.google.com/translate_t'
if stream( 'out.tmp', 'c', 'query exists' ) \= '' then
retval = SysFileDelete( 'out.tmp' )
tgt_size = stream( 'in.tmp', 'c', 'query size' )
retval = charin( 'in.tmp', 1, tgt_size )
parse value retval WITH .'dir="ltr" id=source>'sze'</textarea>'.'<div id=result_box dir="ltr">'tgt'</div>'.
call stream 'in.tmp', 'c', 'close'
Say ToNLS( sze, GlotCP, CurrCP )
Say 'has been translated to:'
Say ToNLS( tgt, GlotCP, CurrCP )
Return 0
ToNLS: procedure
retval = ARG(1)
do while pos( '&#', retval ) > 0
parse value retval with pre'&#'val';'post
retval = pre||d2c(val)||post
end
Return ULSConvertCodepage( retval, ARG(2), ARG(3) )