Skip to content

Instantly share code, notes, and snippets.

@liangzan
Created September 7, 2010 03:39
Show Gist options
  • Save liangzan/567831 to your computer and use it in GitHub Desktop.
Save liangzan/567831 to your computer and use it in GitHub Desktop.
# lib/formtastic.rb
def localized_string(key, value, type, options = {}) #:nodoc:
key = value if value.is_a?(::Symbol)
if value.is_a?(::String)
value
else
use_i18n = value.nil? ? @@i18n_lookups_by_default : (value != false)
if use_i18n
model_name, nested_model_name = normalize_model_name(self.model_name.underscore)
action_name = template.params[:action].to_s rescue ''
attribute_name = key.to_s
defaults = ::Formtastic::I18n::SCOPES.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 << ''
i18n_value = ::Formtastic::I18n.t(defaults.shift,
options.merge(:default => defaults, :scope => type.to_s.pluralize.to_sym))
i18n_value.blank? ? nil : i18n_value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment