1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs combinators fry grouping http.client io
4 io.encodings.binary io.files io.files.unique json.reader kernel
5 locals make namespaces sequences urls ;
8 CONSTANT: google-translate-url "http://ajax.googleapis.com/ajax/services/language/translate"
10 CONSTANT: maximum-translation-size 5120
12 : parameters>assoc ( text from to -- assoc )
14 [ "q" ,, ] [ "langpair" ,, ] bi*
18 : assoc>query-response ( assoc -- response )
19 google-translate-url http-post nip ;
21 TUPLE: response-error response error ;
23 : throw-response-error ( response -- * )
24 [ ] [ "responseDetails" of ] bi response-error boa throw ;
26 : check-response ( response -- response )
27 "responseStatus" over at {
29 { 400 [ response-error ] }
30 [ drop response-error ]
33 : query-response>text ( response -- text )
38 : (translate) ( text from to -- text' )
43 : translate ( text from to -- text' )
44 [ maximum-translation-size group ] 2dip
45 '[ _ _ (translate) ] map concat ;
47 :: translation-party ( text source target -- )
49 dup source target translate dup print
50 target source translate dup print
54 : translate-tts ( text -- file )
55 "http://translate.google.com/translate_tts?tl=en" >url
56 swap "q" set-query-param "" ".mp3" make-unique-file
57 [ download-to ] keep ;
60 ! "dog" "en" "de" translate .