1 ! Copyright (C) 2010 Doug Coleman.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: assocs combinators grouping http.client io io.files.temp
4 io.files.unique json.reader kernel make sequences urls ;
7 CONSTANT: google-translate-url "https://ajax.googleapis.com/ajax/services/language/translate"
9 CONSTANT: maximum-translation-size 5120
11 : parameters>assoc ( text from to -- assoc )
13 [ "q" ,, ] [ "langpair" ,, ] bi*
17 : assoc>query-response ( assoc -- response )
18 google-translate-url http-post nip ;
20 TUPLE: response-error response error ;
22 : throw-response-error ( response -- * )
23 [ ] [ "responseDetails" of ] bi response-error boa throw ;
25 : check-response ( response -- response )
26 "responseStatus" over at {
28 { 400 [ throw-response-error ] }
29 [ drop throw-response-error ]
32 : query-response>text ( response -- text )
37 : (translate) ( text from to -- text' )
42 : translate ( text from to -- text' )
43 [ maximum-translation-size group ] 2dip
44 '[ _ _ (translate) ] map concat ;
46 :: translation-party ( text source target -- )
48 dup source target translate dup print
49 target source translate dup print
53 : translate-tts ( text -- file )
54 "https://translate.google.com/translate_tts?tl=en" >url
55 swap "q" set-query-param [
56 "" ".mp3" unique-file [ download-to ] keep
57 ] with-temp-directory ;
60 ! "dog" "en" "de" translate .