1 Title: Translation of e-mails using Mutt
2 Tags: en, mutt, release, script, python, floss, translate, google translate, software, e-mail, gmail
6 New translation script here__, see the bottom of the post for a description
11 I don't like to trust my important data to big companies like Google__. That's
12 why even when I have a GMail__, I don't use it as my main account. I'm also
13 a little old fashion for some things, and I like to use Mutt__ to check my
16 __ https://www.google.com/
17 __ https://www.gmail.com/
18 __ http://www.mutt.org/
20 But GMail have a very useful feature, at least it became very useful since
21 I moved to a country which language I don't understand very well yet, that's not
22 available in Mutt: translation.
24 But that's the good thing about free software and console programs, they are
25 usually easy to hack to get whatever you're missing, so that's what I did.
27 The immediate solution in my mind was: download some program that uses `Google
28 Translate`__ to translate stuff, and pipe messages through it using a macro.
29 Simple, right? No. At least I couldn't find any script to do the translation,
30 because `Google Translate API`__ is now paid__.
32 __ https://translate.google.com/
33 __ https://developers.google.com/translate/
34 __ https://developers.google.com/translate/v2/pricing
36 So I tried to look for alternatives, first for some translation program that
37 worked locally, but at least in Ubuntu's repositories I couldn't find anything.
38 Then for online services alternatives, but nothing particularly useful either.
39 So I finally found a guy that, doing some Firebug__\ ing, found `how to use the
40 free Google translate service`__. Using that example, I put together a 100
41 SLOC__ nice general Python__ script that you can use to translate stuff, piping
42 them through it. Here is a trivial demonstration of the script (``gt``, short
43 for Google Translate... Brilliant!)::
45 $ echo hola mundo | gt
47 $ echo hallo Welt | gt --to fr
50 __ http://getfirebug.com/
51 __ http://rupeshpatel.wordpress.com/2012/06/23/usage-of-google-translator-api-for-free/
52 __ http://en.wikipedia.org/wiki/Source_lines_of_code
53 __ http://www.python.org/
55 And here is the output of ``gt --help`` to get a better impression on the
56 script's capabilities::
58 usage: gt [-h] [--from LANG] [--to LANG] [--input-file FILE]
59 [--output-file FILE] [--input-encoding ENC] [--output-encoding ENC]
61 Translate text using Google Translate.
64 -h, --help show this help message and exit
65 --from LANG, -f LANG Translate from LANG language (e.g. en, de, es,
67 --to LANG, -t LANG Translate to LANG language (e.g. en, de, es, default:
69 --input-file FILE, -i FILE
70 Get text to translate from FILE instead of stdin
71 --output-file FILE, -o FILE
72 Output translated text to FILE instead of stdout
73 --input-encoding ENC, -I ENC
74 Use ENC caracter encoding to read the input (default:
76 --output-encoding ENC, -O ENC
77 Use ENC caracter encoding to write the output
78 (default: get from locale)
80 You can `download the script here`__, but be warned, I only tested it with
81 Python **3.2**. It's almost certain that it won't work with Python < 3.0, and
82 there is a chance it won't work with Python 3.1 either. Please report success or
83 failure, and patches to make it work with older Python versions are always
88 Ideally you shouldn't abuse Google's service through this script, if you need to
89 translate massive texts every 50ms just pay for the service. For me it doesn't
90 make any sense to do so, because I'm not using the service differently, when
91 I didn't have the script I just *copy&pasted* the text to translate to the web.
92 Another drawback of using the script is I couldn't find any way to make it work
93 using HTTPS__, so you shouldn't translate sensitive data (you shouldn't do so
94 using the web either, because AFAIK it travels as plain text too).
96 __ http://en.wikipedia.org/wiki/HTTP_Secure
98 Anyway, the final step was just to connect Mutt with the script. The solution
99 I found is not ideal, but works most of the time. Just add these macros to your
102 macro index,pager <Esc>t "v/plain\n|gt|less\n" "Translate the first plain text part to English"
103 macro attach <Esc>t "|gt|less\n" "Translate to English"
105 Now using **Esc t** in the index or pager view, you'll see the first plain text
106 part of the message translated from an auto-detected language to English in the
107 default encoding. In the attachments view, **Esc t** will pipe the current part
108 instead. One thing I don't know how to do (or if it's even possible) is to get
109 the encoding of the part being piped to let ``gt`` know. For now I have to make
110 the pipe manually for parts that are not in UTF-8 to call ``gt`` with the right
111 encoding options. The results are piped through ``less`` for convenience. Of
112 course you can write your own macros to translate to another language other than
113 English or use a different default encoding. For example, to translate to
114 Spanish using ISO-8859-1 encoding, just replace the macro with this one::
116 macro index,pager <Esc>t "v/plain\n|gt -tes -Iiso-8859-1|less\n" "Translate the first plain text part to Spanish"
118 Well, that's it! I hope is as useful to you as is being to me ;-)
120 .. admonition:: Update
122 Since picking the right encoding for the e-mail started to be a real PITA,
123 I decided to improve the script to *auto-detect* the encoding, or to be more
124 specific, to try several popular encodings.
126 So, here is the help message for the `new version`__ of the script::
128 usage: gt [-h] [--from LANG] [--to LANG] [--input-file FILE]
129 [--output-file FILE] [--input-encoding ENC] [--output-encoding ENC]
131 Translate text using Google Translate.
134 -h, --help show this help message and exit
135 --from LANG, -f LANG Translate from LANG language (e.g. en, de, es,
137 --to LANG, -t LANG Translate to LANG language (e.g. en, de, es, default:
139 --input-file FILE, -i FILE
140 Get text to translate from FILE instead of stdin
141 --output-file FILE, -o FILE
142 Output translated text to FILE instead of stdout
143 --input-encoding ENC, -I ENC
144 Use ENC caracter encoding to read the input, can be a
145 comma separated list of encodings to try, LOCALE being
146 a special value for the user's locale-specified
147 preferred encoding (default: LOCALE,utf-8,iso-8859-15)
148 --output-encoding ENC, -O ENC
149 Use ENC caracter encoding to write the output
154 So now by default your locale's encoding, utf-8 and iso-8859-15 are tried by
155 default (in that order). These are the defaults that makes more sense to me,
156 you can change the default for the ones that makes sense to you by changing
157 the script or by using -I option in your macro definition, for example::
159 macro index,pager <Esc>t "v/plain\n|gt -IMS-GREEK,IBM-1148,UTF-16BE|less\n"
161 Weird choice of defaults indeed :P
163 .. vim: set et sw=3 sts=3 tw=80 fo+=lt1n: