Class: LocalchI18n::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/localch_i18n/translator.rb,
lib/localch_i18n/translator/service.rb

Overview

Subclass this class in order to support another translation service

Defined Under Namespace

Classes: Service

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Translator

Returns a new instance of Translator.



8
9
10
# File 'lib/localch_i18n/translator.rb', line 8

def initialize options = {}
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/localch_i18n/translator.rb', line 6

def options
  @options
end

Instance Method Details

#auto_translate(flat_hash) ⇒ Object

TODO: Refactor to use inject



13
14
15
16
17
18
19
20
21
# File 'lib/localch_i18n/translator.rb', line 13

def auto_translate(flat_hash)      
	raise '#auto_translate method requires a #current_locale method in the same module' unless respond_to? :current_locale

	translated_hash = {}
	flat_hash.each do |key, text|
translated_hash[key] = translate_it text, current_locale
			end
			translated_hash
end

#service=(service) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/localch_i18n/translator.rb', line 27

def service= service
  raise ArgumentError, "Must be a subclass of LocalchI18n::TranslationService, was #{service}" unless service.kind_of?(LocalchI18n::TranslationService)
  @service = service
end

#translate_it(text, locale) ⇒ Object



23
24
25
# File 'lib/localch_i18n/translator.rb', line 23

def translate_it text, locale
  text_has_args? ? translate_with_args(text) : text.translate(current_locale)
end