class Smartdict::Drivers::AbstractDriver

Translation driver provides an ability to translate words using external data sources. It can be data from local disk, database or remote services like Google Translate. Every driver must inherit {Smartdict::Driver} and have implementation of {#translate} method. This method should sets translated and transcription properties. For examples you can see #{Smartdict::Driver::GoogleTranslateDriver}.

In your implementation of {#translate} you need to use methods:

Usage:

class HelloWorldDriver < Smartdict::Driver
  # Set name of driver, so DriverManager can identify it.
  name "hello_world"

  # This trivial example will always return the same translation.
  def translate
    self.translated = {"noun" => ["hello", "hi"], "verb" => ["greet"]}
    self.transcription = "he'leu"
  end
end

Attributes

from_lang[R]
to_lang[R]
transcription[RW]
translated[RW]
word[R]

Public Class Methods

new(word, from_lang, to_lang) click to toggle source
# File lib/smartdict/drivers/abstract_driver.rb, line 41
def initialize(word, from_lang, to_lang)
  @word      = word
  @from_lang = from_lang.to_s
  @to_lang   = to_lang.to_s
  translate
  raise Smartdict::TranslationNotFound if(translated.nil? || translated.empty?)
end
set_name(name) click to toggle source

Sets driver name

# File lib/smartdict/drivers/abstract_driver.rb, line 37
def self.set_name(name)
  self.name = name.to_s
end
translate(*args) click to toggle source
# File lib/smartdict/drivers/abstract_driver.rb, line 32
def self.translate(*args)
  self.new(*args).build_translation
end

Public Instance Methods

build_translation() click to toggle source
# File lib/smartdict/drivers/abstract_driver.rb, line 49
def build_translation
  translation = Smartdict::Translation.new(
    :word          => word,
    :from_lang     => from_lang.to_s,
    :to_lang       => to_lang.to_s,
    :transcription => transcription,
    :driver        => self.name
  )
  translated.each do |word_class, words|
    translation.translated[word_class] = words
  end
  translation
end