Module: ToLang::Translatable

Defined in:
lib/to_lang/translatable.rb

Overview

ToLang’s core translation methods.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Overrides method_missing to catch and define dynamic translation methods.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/to_lang/translatable.rb', line 27

def method_missing(method, *args, &block)
  case method.to_s
  when /^to_(.*)_from_(.*)$/
    if CODEMAP[$1] && CODEMAP[$2]
      define_and_call_method(method) { translate(CODEMAP[$1], :from => CODEMAP[$2]) }
    else
      original_method_missing(method, *args, &block)
    end
  when /^from_(.*)_to_(.*)$/
    if CODEMAP[$1] && CODEMAP[$2]
      define_and_call_method(method) { translate(CODEMAP[$2], :from => CODEMAP[$1]) }
    else
      original_method_missing(method, *args, &block)
    end
  when /^to_(.*)$/
    if CODEMAP[$1]
      define_and_call_method(method) { translate(CODEMAP[$1]) }
    else
      original_method_missing(method, *args, &block)
    end
  else
    original_method_missing(method, *args, &block)
  end
end

Instance Method Details

#original_method_missingObject

Chain method_missing in case another library has used it.



21
# File 'lib/to_lang/translatable.rb', line 21

alias_method :original_method_missing, :method_missing

#original_respond_to?Object

Chain respond_to? in case another library has used it.



53
# File 'lib/to_lang/translatable.rb', line 53

alias_method :original_respond_to?, :respond_to?

#translate(target, *args) ⇒ String

Translates a string or array of strings to another language. All the magic methods use this internally. It, in turn, forwards everything on to Connector#request

Parameters:

  • target (String)

    The language code for the language to translate to.

  • args

    Any additional arguments, such as the source language.

Returns:

  • (String)

    The translated string.



15
16
17
# File 'lib/to_lang/translatable.rb', line 15

def translate(target, *args)
  ToLang.connector.request(self, target, *args)
end