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