class Traducto::Base

*************************************************************************************

Base helper methods to add path to lazy lookup and allow formatting.

*************************************************************************************

Attributes

rails_helpers[R]

Public Class Methods

new(rails_helpers) click to toggle source
# File lib/traducto/base.rb, line 11
def initialize(rails_helpers)
  @rails_helpers = rails_helpers

  init_state

  set_base_actions
end

Public Instance Methods

add_action(action) click to toggle source
# File lib/traducto/base.rb, line 19
def add_action(action)
  @actions << action
end
translate(key, options={}) click to toggle source
# File lib/traducto/base.rb, line 23
def translate(key, options={})
  init_state key, options

  lazy_translate if lazy_lookup?

  i18n_translate @base_key if translation_missing?

  format

  if @text.is_a? String
    @text.html_safe
  else
    @text
  end
end

Private Instance Methods

controller() click to toggle source
# File lib/traducto/base.rb, line 42
def controller
  request[:controller].gsub('/', '.')
end
format() click to toggle source
# File lib/traducto/base.rb, line 46
def format
  case @options[:format]
    when :text then format_text
    when :list then format_list
    else format_base
  end
end
format_base() click to toggle source
# File lib/traducto/base.rb, line 54
def format_base
  @text = @text.join("\n") if @text.is_a? Array
end
format_list() click to toggle source
# File lib/traducto/base.rb, line 58
def format_list
  @text = [@text] if not @text.is_a? Array

  @text = @text.map { |x| content_tag(:li, x.html_safe) }.join.html_safe
  @text = content_tag(:ul, @text)
end
format_text() click to toggle source
# File lib/traducto/base.rb, line 65
def format_text
  @text = [@text] if not @text.is_a? Array

  @text = @text.map { |x| content_tag(:p, x.html_safe) }.join.html_safe
end
i18n_translate(key) click to toggle source
# File lib/traducto/base.rb, line 71
def i18n_translate(key)
  @text = I18n.translate(key, @options)
end
init_state(key='', options={}) click to toggle source
# File lib/traducto/base.rb, line 75
def init_state(key='', options={})
  @options = options.reverse_merge default: '', format: nil

  @base_key = key
  @text = nil
end
lazy_lookup?() click to toggle source
# File lib/traducto/base.rb, line 82
def lazy_lookup?
  @base_key[0,1] == '.'
end
lazy_translate() click to toggle source
# File lib/traducto/base.rb, line 86
def lazy_translate
  @actions.each do |action|
    i18n_translate "views.#{controller}.#{action}#{@base_key}" if translation_missing?
  end
  i18n_translate "views.#{controller}#{@base_key}" if translation_missing?
  i18n_translate "views#{@base_key}" if translation_missing?
end
set_base_actions() click to toggle source
# File lib/traducto/base.rb, line 94
def set_base_actions
  @actions = []

  if request
    @actions = [request[:action]]

    if request[:action] == 'create'
      @actions << 'new'
    elsif request[:action] == 'update'
      @actions << 'edit'
    end
  end
end
translation_missing?() click to toggle source
# File lib/traducto/base.rb, line 108
def translation_missing?
  @text.blank? or @text.include? 'translation missing'
end