2013-12-15 18:41:13 +00:00
|
|
|
require 'i18n'
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locale', '*.yml')]
|
|
|
|
|
|
|
|
module Ransack
|
|
|
|
module Translate
|
|
|
|
def self.word(key, options = {})
|
2014-05-01 13:55:39 +00:00
|
|
|
I18n.translate(:"ransack.#{key}", :default => key.to_s)
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.predicate(key, options = {})
|
2014-05-01 13:55:39 +00:00
|
|
|
I18n.translate(:"ransack.predicates.#{key}", :default => key.to_s)
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.attribute(key, options = {})
|
|
|
|
unless context = options.delete(:context)
|
2011-08-13 20:36:40 +00:00
|
|
|
raise ArgumentError, "A context is required to translate attributes"
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
original_name = key.to_s
|
|
|
|
base_class = context.klass
|
2014-01-08 00:45:42 +00:00
|
|
|
base_ancestors = base_class.ancestors.select {
|
|
|
|
|x| x.respond_to?(:model_name)
|
|
|
|
}
|
2011-06-07 01:32:03 +00:00
|
|
|
predicate = Predicate.detect_from_string(original_name)
|
2011-03-31 00:31:39 +00:00
|
|
|
attributes_str = original_name.sub(/_#{predicate}$/, '')
|
|
|
|
attribute_names = attributes_str.split(/_and_|_or_/)
|
|
|
|
combinator = attributes_str.match(/_and_/) ? :and : :or
|
|
|
|
defaults = base_ancestors.map do |klass|
|
2014-01-01 17:53:51 +00:00
|
|
|
:"ransack.attributes.#{i18n_key(klass)}.#{original_name}"
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
translated_names = attribute_names.map do |attr|
|
|
|
|
attribute_name(context, attr, options[:include_associations])
|
|
|
|
end
|
|
|
|
|
|
|
|
interpolations = {}
|
2014-01-08 00:45:42 +00:00
|
|
|
interpolations[:attributes] = translated_names.join(
|
|
|
|
" #{Translate.word(combinator)} "
|
|
|
|
)
|
2011-03-31 00:31:39 +00:00
|
|
|
|
|
|
|
if predicate
|
|
|
|
defaults << "%{attributes} %{predicate}"
|
|
|
|
interpolations[:predicate] = Translate.predicate(predicate)
|
|
|
|
else
|
|
|
|
defaults << "%{attributes}"
|
|
|
|
end
|
|
|
|
|
|
|
|
defaults << options.delete(:default) if options[:default]
|
2014-05-01 13:55:39 +00:00
|
|
|
options.reverse_merge! :count => 1, :default => defaults
|
2011-03-31 00:31:39 +00:00
|
|
|
I18n.translate(defaults.shift, options.merge(interpolations))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.association(key, options = {})
|
|
|
|
unless context = options.delete(:context)
|
|
|
|
raise ArgumentError, "A context is required to translate associations"
|
|
|
|
end
|
|
|
|
|
2014-01-01 17:53:51 +00:00
|
|
|
defaults = key.blank? ?
|
|
|
|
[:"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"] :
|
|
|
|
[:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
|
2011-03-31 00:31:39 +00:00
|
|
|
defaults << context.traverse(key).model_name.human
|
2014-05-01 13:55:39 +00:00
|
|
|
options = { :count => 1, :default => defaults }
|
2011-03-31 00:31:39 +00:00
|
|
|
I18n.translate(defaults.shift, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.attribute_name(context, name, include_associations = nil)
|
|
|
|
assoc_path = context.association_path(name)
|
|
|
|
associated_class = context.traverse(assoc_path) if assoc_path.present?
|
|
|
|
attr_name = name.sub(/^#{assoc_path}_/, '')
|
|
|
|
interpolations = {}
|
|
|
|
interpolations[:attr_fallback_name] = I18n.translate(
|
2014-01-01 17:53:51 +00:00
|
|
|
:"ransack.attributes.#{
|
|
|
|
i18n_key(associated_class || context.klass)
|
|
|
|
}.#{attr_name}",
|
2014-05-01 13:55:39 +00:00
|
|
|
:default => [
|
2014-01-08 00:45:42 +00:00
|
|
|
(
|
|
|
|
if associated_class
|
|
|
|
:"#{associated_class.i18n_scope}.attributes.#{
|
|
|
|
i18n_key(associated_class)}.#{attr_name}"
|
|
|
|
else
|
2014-09-12 14:59:11 +00:00
|
|
|
context.klass.ancestors.select do |k|
|
|
|
|
k.respond_to?(:model_name)
|
|
|
|
end.collect do |k|
|
|
|
|
:"#{k.i18n_scope}.attributes.#{i18n_key(k)}.#{attr_name}"
|
|
|
|
end
|
2014-01-08 00:45:42 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
),
|
2013-05-23 12:56:48 +00:00
|
|
|
:".attributes.#{attr_name}",
|
2011-03-31 00:31:39 +00:00
|
|
|
attr_name.humanize
|
2014-09-12 14:59:11 +00:00
|
|
|
].flatten
|
2011-03-31 00:31:39 +00:00
|
|
|
)
|
2014-01-01 17:53:51 +00:00
|
|
|
defaults = [:"ransack.attributes.#{i18n_key(context.klass)}.#{name}"]
|
2011-03-31 00:31:39 +00:00
|
|
|
if include_associations && associated_class
|
|
|
|
defaults << '%{association_name} %{attr_fallback_name}'
|
2013-08-04 13:13:41 +00:00
|
|
|
interpolations[:association_name] = association(
|
2014-05-01 13:55:39 +00:00
|
|
|
assoc_path, :context => context
|
2013-08-04 13:13:41 +00:00
|
|
|
)
|
2011-03-31 00:31:39 +00:00
|
|
|
else
|
|
|
|
defaults << '%{attr_fallback_name}'
|
|
|
|
end
|
2014-05-01 13:55:39 +00:00
|
|
|
options = { :count => 1, :default => defaults }
|
2011-03-31 00:31:39 +00:00
|
|
|
I18n.translate(defaults.shift, options.merge(interpolations))
|
|
|
|
end
|
2014-01-01 17:53:51 +00:00
|
|
|
|
|
|
|
def self.i18n_key(klass)
|
2013-09-05 09:13:03 +00:00
|
|
|
if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
|
|
|
|
klass.model_name.i18n_key.to_s.tr('.', '/')
|
|
|
|
else
|
|
|
|
klass.model_name.i18n_key.to_s
|
|
|
|
end
|
2014-01-01 17:53:51 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
2013-03-01 20:29:40 +00:00
|
|
|
end
|