Commits

Victor Gavro committed 6a4b761

added CLI interface

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+
 import urllib
 import urllib2
 import urlparse
     if flat:
         return u' '.join(result)
     return tuple(result), source_language
+
+
+if __name__ == '__main__':
+    import optparse, locale
+    parser = optparse.OptionParser(usage='usage: %prog -t target_language '
+                                   '[-s source_language] text to translate')
+    parser.add_option('-t', dest='tl', help='Language of translation')
+    parser.add_option('-s', dest='sl', help='Language of source text')
+    opts, args = parser.parse_args()
+    encoding = locale.getpreferredencoding()
+    text = u' '.join([arg.decode(encoding) for arg in args]).strip()
+
+    if not opts.tl or not text:
+        parser.error('target language or text to translate not specified')
+
+    translation = translate(text, opts.tl, opts.sl, html=False, flat=True)
+    print translation.encode(encoding)