class Pincerna::Translation

Translates text using Google Translate.

Constants

ICON

The icon to show for each feedback item.

MATCHER

The expression to match.

RELEVANT_MATCHES

Relevant groups in the match.

URL

The URL of the webservice.

Public Instance Methods

perform_filtering(from, to, value) click to toggle source

Translates text using Google Translate.

@param from [String] The code of the source language. @param to [String] The code of the target language. @param value [String] The text to translate. @return [Hash|NilClass] The translation data or `nil` if the translation failed.

# 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
process_results(results) click to toggle source

Processes items to obtain feedback items.

@param results [Array] The items to process. @return [Array] The feedback items.

# 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