Files
capsule/src/cgi-bin/translate

83 lines
1.7 KiB
Plaintext
Raw Normal View History

2020-11-27 00:11:26 +00:00
#!/usr/bin/ruby --disable-all
2020-11-27 00:11:26 +00:00
# A cgi-bin script that translates between languages. It relies on apertium
# being installed on the machine.
#
# CGI works with environment variables. Here are the ones that matter:
#
# PATH_INFO: /<from>/<to>
# QUERY_STRING: Some%20text%20to%20translate
#
# We expect to be called like: /cgi-bin/translate/eng/spa?Food
#
# If we don't have two languages, make it a Not Found error.
# If we don't have a query string to translate, ask for one.
require 'cgi'
2020-11-27 00:11:26 +00:00
LANGUAGE_PAIRS = %w[en-es es-en]
class Object
def blank?
nil? || self&.empty?
end
end
def respond!(code, meta, body = nil)
STDOUT.print("#{code} #{meta}\r\n")
STDOUT.print(body) unless body.blank?
exit 0
end
def ask!(prompt)
respond!(10, prompt)
end
def ok!(meta, body = nil)
respond!(20, meta, body)
end
def temp_fail!(meta = 'Temporary Failure')
respond!(40, meta)
end
def not_found!(meta = 'Not Found')
respond!(51, meta)
end
2020-11-27 00:11:26 +00:00
def extract_langpair(path)
return if path.blank? || !path.start_with?('/')
_, src, dst, *rest = path.split('/')
return unless rest.empty?
2020-11-27 00:11:26 +00:00
pair = [src, dst].join('-')
return unless LANGUAGE_PAIRS.include?(pair)
pair
end
def extract_text(query)
return "" if query.blank?
CGI.unescape(query)
end
2020-11-27 00:11:26 +00:00
LANG_PAIR = extract_langpair(ENV['PATH_INFO'])
not_found! if LANG_PAIR.blank?
TRANSLATE = extract_text(ENV['QUERY_STRING'])
# TODO: we could detect a URL and translate the whole page sometime, perhaps
ask!('Enter text to translate') if TRANSLATE.blank?
2020-11-27 00:11:26 +00:00
require 'open3'
2020-11-27 00:11:26 +00:00
translation, status = Open3.capture2("apertium", LANG_PAIR, stdin_data: TRANSLATE)
2020-11-27 00:11:26 +00:00
temp_fail!("Couldn't get translation") unless status.success?
2020-11-27 00:11:26 +00:00
ok!('text/plain; charset="utf-8"', translation)