Class: Pincerna::Translation
Overview
Translates text using Google Translate.
Constant Summary
- MATCHER =
The expression to match.
/^ (from\s+)?(?<from>[a-z_-]{2,5}) \s+ (to\s+)?((?<to>[a-z_-]{2,5})\s+)? (?<text>.+) $/mix
- RELEVANT_MATCHES =
Relevant groups in the match.
{ "from" => ->(_, value) { value.downcase }, "to" => ->(_, value) { value && value.downcase }, "text" => ->(_, value) { value.strip }, }
- ICON =
The icon to show for each feedback item.
Pincerna::Base::ROOT + "/images/translate.png"
- URL =
The URL of the webservice.
"http://translate.google.com.br/translate_a/t"
Constants inherited from Base
Base::CACHE_ROOT, Base::FULL_NAME, Base::ROOT, Base::TYPES, Base::WORKFLOW_ROOT
Instance Attribute Summary
Attributes inherited from Base
#format, #format_content_type, #output
Instance Method Summary (collapse)
-
- (Hash|NilClass) perform_filtering(from, to, value)
Translates text using Google Translate.
-
- (Array) process_results(results)
Processes items to obtain feedback items.
Methods inherited from Base
#add_feedback_item, execute!, #filter, #format_float, #initialize, #output_feedback, #round_float
Constructor Details
This class inherits a constructor from Pincerna::Base
Instance Method Details
- (Hash|NilClass) perform_filtering(from, to, value)
Translates text using Google Translate.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/pincerna/translation.rb', line 37 def perform_filtering(from, to, value) # By default we translate from English if !to then to = from from = "en" end response = Pincerna::Cache.instance.use("translation:#{from}:#{to}:#{value}", Pincerna::Cache::EXPIRATIONS[:short]) { fetch_remote_resource(URL, {client: "p", text: value, sl: from, tl: to, multires: 1, ssel: 0, tsel: 0, sc: 1, ie: "UTF-8", oe: "UTF-8"}) } # Parse results if response["dict"] then translations = response["dict"][0]["entry"].map {|t| t["word"] } {main: translations.shift, alternatives: translations} else translation = response["sentences"][0]["trans"] {main: translation} if translation != value end end |
- (Array) process_results(results)
Processes items to obtain feedback items.
62 63 64 65 |
# File 'lib/pincerna/translation.rb', line 62 def process_results(results) alternatives = results[:alternatives] ? "Alternatives: #{results[:alternatives].join(", ")}" : "Action this item to copy the translation on the clipboard." [{title: results[:main], arg: results[:main], subtitle: alternatives, icon: ICON}] end |