Skip to content

Instantly share code, notes, and snippets.

@r3nya r3nya/translate.rb forked from Olefine/translate.rb
Created Feb 8, 2014

Embed
What would you like to do?
require 'yandex-translator'
class Translate
attr_accessor :words_for_translate
attr_reader :translated_word
def initialize(words)
set_api_key
@words_for_translate = words
end
def translate(options = nil)
raise ArgumentError unless options
p @words_for_translate
@translated_word = if @words_for_translate.size > 1
translated = @words_for_translate.map {|word| yandex_translate(word, options) }
translated.join(' ')
else
yandex_translate(@words_for_translate, options)
end
@translated_word
end
private
def set_api_key
Yandex::Translator.set_api_key('trnsl.1.1.20130901T135648Z.f9baa157528bd5a8.9f830c39c1e02e3cf0cf97d79932ddb245fb0684')
end
def yandex_translate(word, options = {})
Yandex::Translator.translate(word, options.values.first.to_s, options.keys.first.to_s)
end
end
tr = Translate.new(ARGV)
puts tr.translate(:en => :ru)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.