lib/storefront/helpers/locale_helper.rb in storefront-0.2.1 vs lib/storefront/helpers/locale_helper.rb in storefront-0.2.7
- old
+ new
@@ -1,50 +1,120 @@
module Storefront
module LocaleHelper
- # t!(:"what.summary", :scope => :"reports.titles", :model => deal)
+ SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL = [
+ '%{namespace}.%{model}.%{nested_model}.%{action}.%{attribute}',
+ '%{model}.%{nested_model}.%{action}.%{attribute}',
+ '%{namespace}.%{model}.%{action}.%{attribute}',
+ '%{model}.%{action}.%{attribute}',
+ '%{namespace}.%{model}.%{nested_model}.%{attribute}',
+ '%{model}.%{nested_model}.%{attribute}',
+ '%{namespace}.%{model}.%{attribute}',
+ '%{model}.%{attribute}',
+ '%{namespace}.%{nested_model}.%{attribute}',
+ '%{nested_model}.%{attribute}',
+ '%{namespace}.%{attribute}',
+ '%{attribute}'
+ ]
+
+ SCOPES_WITH_NESTED_MODEL = [
+ '%{model}.%{nested_model}.%{action}.%{attribute}',
+ '%{model}.%{action}.%{attribute}',
+ '%{model}.%{nested_model}.%{attribute}',
+ '%{model}.%{attribute}',
+ '%{nested_model}.%{attribute}',
+ '%{attribute}'
+ ]
+
+ SCOPES_WITH_NAMESPACE = [
+ '%{namespace}.%{model}.%{action}.%{attribute}',
+ '%{namespace}.%{model}.%{attribute}',
+ '%{model}.%{action}.%{attribute}',
+ '%{model}.%{attribute}',
+ '%{namespace}.%{attribute}',
+ '%{attribute}'
+ ]
+
+ SCOPES = [
+ '%{model}.%{action}.%{attribute}',
+ '%{model}.%{attribute}',
+ '%{attribute}'
+ ]
+
+ # t?(:"what.summary", :scope => :"reports.titles", :model => deal)
def t?(key, options = {})
- return key if key.is_a?(::String)
- model_names = options[:model_names]
- model_names ||= options[:model].present? ? options[:model].class.ancestor_classes.map { |i| i.name.underscore } : []
- allow_blank = options.delete(:allow_blank) == true
+ return key if key.is_a?(::String) || key.blank?
- keys = [key.to_s]
- if options[:try].present?
- keys += Array(options[:default]).map do |extra_key|
- "#{key}.#{extra_key}"
- end
- end
-
+ models = Storefront::ModelProxy.model_names(options[:model] || options[:models] || options[:model_names] || storefront_config.current_scope[:resource] || [])
+ models << "" if models.blank?
+ namespaces = Array(options[:namespaces] || options[:namespace] || storefront_config.current_scope[:namespace])
+ namespaces << "" if namespaces.blank? && !!storefront_config.localize_with_namespace
+ action_name = options[:action] || storefront_config.current_scope[:action]
+ defaults = []
+ allow_blank = options.delete(:allow_blank) == true
count = options.delete(:count)
# none, one, many, other
count_key = count != 1 ? :other : :one
+ keys = [key.to_s]
+
if options.has_key?(:present) || options.has_key?(:past) || options.has_key?(:future)
if options[:future] == true
tense_key = :future
else
tense_key = (options.delete(:present) == true || options.delete(:past) == false) ? :present : :past
end
else
tense_key = nil
end
- defaults = []
+ if options.has_key?(:nested_model)
+ with_nested_model = !!options[:nested_model]
+ else
+ with_nested_model = !!storefront_config.localize_with_nested_model
+ end
- keys.each do |key|
- model_names.each do |model_name|
- path = '{{model}}.{{key}}'
- path.gsub!('{{model}}', model_name)
- path.gsub!('{{key}}', key)
- path.gsub!('..', '.')
- defaults << path
- end
+ if options.has_key?(:namespace)
+ with_namespace = !!options[:namespace]
+ else
+ with_namespace = !!storefront_config.localize_with_namespace
+ end
- path = '{{key}}'
- path.gsub!('{{key}}', key)
- path.gsub!('..', '.')
- defaults << path
+ if with_nested_model && with_namespace
+ # TODO, need to performance test before we do this many operations
+ SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL
+ elsif with_nested_model
+ # TODO
+ SCOPES_WITH_NESTED_MODEL
+ elsif with_namespace
+ namespaces.each do |namespace|
+ models.each do |model|
+ keys.each do |_key|
+ SCOPES_WITH_NAMESPACE.each do |path|
+ path = path.dup
+ path.gsub!('%{namespace}', namespace)
+ path.gsub!('%{model}', model)
+ path.gsub!('%{action}', action_name)
+ path.gsub!('%{attribute}', _key)
+ path.gsub!('..', '.')
+ defaults << path
+ end
+ end
+ end
+ end
+ else
+ models.each do |model|
+ keys.each do |_key|
+ SCOPES.each do |path|
+ path = path.dup
+ path.gsub!('%{model}', model)
+ path.gsub!('%{action}', action_name)
+ path.gsub!('%{attribute}', _key)
+ path.gsub!('..', '.')
+ defaults << path
+ end
+ end
+ end
end
defaults.map!(&:to_sym)
defaults << ''
@@ -144,20 +214,20 @@
return object unless object.is_a?(::Hash)
key = key.to_s
title = object[:title] || object[:label]
subtitle = object[:subtitle]
- title = t!(:"#{key}", options.merge(:try => :title))
+ title = t?(:"#{key}", options.merge(:try => :title))
title = key.to_s.split(".").last.titleize if title.blank? || title.is_a?(::Hash)
- subtitle = t!(:"#{key}.subtitle", options.merge(:allow_blank => true))
+ subtitle = t?(:"#{key}.subtitle", options.merge(:allow_blank => true))
# everything has to have a title
result[:title] = title
# not everything has to have a subtitle
result[:subtitle] = subtitle if subtitle.present?
- # it's a leaf if we're at this point!
+ # it's a leaf if we're at this point?
if object.has_key?(:value)
result[:value] = object[:value]
else
object.each do |child_key, child_value|
if child_value.is_a?(::Hash)