Created
November 20, 2009 09:07
-
-
Save bovi/239373 to your computer and use it in GitHub Desktop.
Translation plugin for Termtter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# this plugin is detecting the language of a tweet and | |
# translate it to your language, if you can't understand | |
# the specific language | |
# | |
# configuration: | |
# add to your .termtter/config some additional stuff | |
# which language you prefer to read? | |
# - config.mylang = 'en' | |
# which languages should not be translated? | |
# - config.readable_langs = ['en', 'de'] | |
# | |
# author: Daniel Bovensiepen [daniel@bovensiepen.net] | |
# | |
# this code is using which ever licence Termtter itself is using | |
require 'json' | |
require 'uri' | |
require 'open-uri' | |
GOOGLE_URL = "http://ajax.googleapis.com/ajax/services/language/" | |
GOOGLE_TRANSLATE_URL = "#{GOOGLE_URL}translate" | |
GOOGLE_DETECT_URL = "#{GOOGLE_URL}detect" | |
def ask_ggogle url, return_index, default | |
jsondoc = open(URI.escape("#{url}&v=1.0")).read | |
response = JSON.parse(jsondoc) | |
if response["responseStatus"] == 200 | |
return response["responseData"][return_index], | |
else | |
return default | |
end | |
end | |
def guess_lang(text); ask_ggogle "#{GOOGLE_DETECT_URL}?q=#{text}", 'language', 'en'; end | |
def trans_lang(text) | |
input_lang = guess_lang(text) | |
unless config.readable_langs.include? input_lang | |
langpair = "#{input_lang}|#{config.my_lang}" | |
ask_ggogle "#{GOOGLE_TRANSLATE_URL}?q=#{text}&langpair=#{langpair}", "translatedText", text | |
else | |
text | |
end | |
end | |
module Termtter::Client | |
register_command( | |
:name => :translatelist, :aliases => [:tl], | |
:exec_proc => lambda {|arg| | |
if arg =~ /\-([\d]+)/ | |
options = {:count => $1} | |
arg = arg.gsub(/\-([\d]+)/, '') | |
else | |
options = {} | |
end | |
if arg.empty? | |
event = :list_friends_timeline | |
statuses = Termtter::API.twitter.friends_timeline(options) | |
else | |
event = :list_user_timeline | |
statuses = [] | |
Array(arg.split).each do |user| | |
user_name = normalize_as_user_name(user) | |
statuses += Termtter::API.twitter.user_timeline(user_name, options) | |
end | |
end | |
statuses.each do |s| | |
s[:text] = trans_lang s[:text] | |
end | |
output(statuses, event) | |
}, | |
:help => ["translatelist,tl [USERNAME] [-COUNT]", "List and translate the posts"] | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment