Class: Formtastic::Localizer

Inherits:
Object
  • Object
show all
Defined in:
lib/formtastic/localizer.rb

Overview

Implementation for looking up localized values within Formtastic using I18n, if no explicit value (like the :label option) is set and I18n-lookups are enabled in the configuration.

You can subclass this to implement your own Localizer, and configure Formtastic to use this localizer with:

Formtastic::FormBuilder.i18n_localizer

Enabled/disable i18n lookups completely with:

Formtastic::FormBuilder.i18n_lookups_by_default = true/false

Lookup priority:

'formtastic.%type.%model.%action.%attribute' 'formtastic.%type.%model.%attribute' 'formtastic.%type.%attribute'

Example:

'formtastic.labels.post.edit.title' 'formtastic.labels.post.title' 'formtastic.labels.title'

Defined Under Namespace

Classes: Cache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_builder) ⇒ Localizer

Returns a new instance of Localizer.



55
56
57
# File 'lib/formtastic/localizer.rb', line 55

def initialize(current_builder)
  self.builder = current_builder 
end

Instance Attribute Details

#builderObject

Returns the value of attribute builder.



49
50
51
# File 'lib/formtastic/localizer.rb', line 49

def builder
  @builder
end

Class Method Details

.cacheObject



51
52
53
# File 'lib/formtastic/localizer.rb', line 51

def self.cache
  @cache ||= Cache.new
end

Instance Method Details

#localize(key, value, type, options = {}) ⇒ Object

:nodoc:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/formtastic/localizer.rb', line 59

def localize(key, value, type, options = {}) #:nodoc:
  key = value if value.is_a?(::Symbol)
  
  if value.is_a?(::String)
    escape_html_entities(value)
  else
    use_i18n = value.nil? ? i18n_lookups_by_default : (value != false)
    use_cache = i18n_cache_lookups
    cache = self.class.cache
    
    if use_i18n
      model_name, nested_model_name  = normalize_model_name(builder.model_name.underscore)

      action_name = builder.template.params[:action].to_s rescue ''
      attribute_name = key.to_s
      
      # look in the cache first
      if use_cache
        cache_key = [::I18n.locale, action_name, model_name, nested_model_name, attribute_name, key, value, type, options]
        return cache.get(cache_key) if cache.has_key?(cache_key)
      end

      defaults = Formtastic::I18n::SCOPES.reject do |i18n_scope|
        nested_model_name.nil? && i18n_scope.match(/nested_model/)
      end.collect do |i18n_scope|
        i18n_path = i18n_scope.dup
        i18n_path.gsub!('%{action}', action_name)
        i18n_path.gsub!('%{model}', model_name)
        i18n_path.gsub!('%{nested_model}', nested_model_name) unless nested_model_name.nil?
        i18n_path.gsub!('%{attribute}', attribute_name)
        i18n_path.gsub!('..', '.')
        i18n_path.to_sym
      end
      defaults << ''

      defaults.uniq!

      default_key = defaults.shift
      i18n_value = Formtastic::I18n.t(default_key,
        options.merge(:default => defaults, :scope => type.to_s.pluralize.to_sym))
      i18n_value = i18n_value.is_a?(::String) ? i18n_value : nil
      if i18n_value.blank? && type == :label
        # This is effectively what Rails label helper does for i18n lookup
        options[:scope] = [:helpers, type]
        options[:default] = defaults
        i18n_value = ::I18n.t(default_key, options)
      end
      
      # save the result to the cache
      result = (i18n_value.is_a?(::String) && i18n_value.present?) ? escape_html_entities(i18n_value) : nil
      cache.set(cache_key, result) if use_cache
      result
    end
  end
end