lib/traco/class_methods.rb in traco-3.1.2 vs lib/traco/class_methods.rb in traco-3.1.3
- old
+ new
@@ -1,61 +1,70 @@
module Traco
module ClassMethods
- LOCALE_RE = /[a-zA-Z]{2}(?:_[a-zA-Z]{2})?/
-
def locales_for_attribute(attribute)
- re = /\A#{attribute}_(#{LOCALE_RE})\z/
+ traco_cache(attribute, LocaleFallbacks::ANY_FALLBACK).keys
+ end
- column_names.
- grep(re) { $1.to_sym }.
- sort_by(&locale_sort_value)
+ # Consider this method internal.
+ def _locale_columns_for_attribute(attribute, fallback)
+ traco_cache(attribute, fallback).values
end
def locale_columns(*attributes)
- attributes.inject([]) { |memo, attribute|
- memo += locales_for_attribute(attribute).map { |locale|
- :"#{attribute}_#{locale}"
- }
- }
+ attributes.each_with_object([]) do |attribute, columns|
+ columns.concat(_locale_columns_for_attribute(attribute, LocaleFallbacks::ANY_FALLBACK))
+ end
end
def current_locale_column(attribute)
- :"#{attribute}_#{I18n.locale.to_s.downcase.sub("-", "_")}"
+ Traco.column(attribute, I18n.locale)
end
- def human_attribute_name(attribute, options = {})
- default = super(attribute, options.merge(default: ""))
+ def human_attribute_name(column, options = {})
+ attribute, locale = Traco.split_localized_column(column)
- if default.blank? && attribute.to_s.match(/\A(\w+?)_(#{LOCALE_RE})\z/)
- column, locale = $1, $2.to_sym
- if translates?(column)
- return "#{super(column, options)} (#{locale_name(locale)})"
- end
+ if translates?(attribute)
+ default = super(column, options.merge(default: ""))
+ default.presence || "#{super(attribute, options)} (#{Traco.locale_name(locale)})"
+ else
+ super
end
-
- super
end
private
- def locale_sort_value
- lambda { |locale|
- if locale == Traco.locale_suffix(I18n.default_locale)
- # Sort the default locale first.
- "0"
- else
- # Sort the rest alphabetically.
- locale.to_s
- end
- }
- end
-
def translates?(attribute)
- translatable_attributes.include?(attribute.to_sym)
+ translatable_attributes.include?(attribute)
end
- def locale_name(locale)
- default = locale.to_s.upcase.sub("_", "-")
- I18n.t(locale, scope: :"i18n.languages", default: default)
+ # Structured by (current) locale => attribute => fallback option => {locale_to_try => column}.
+ # @example
+ # {
+ # :"pt-BR" => {
+ # title: {
+ # default: { :"pt-BR" => :title_pt_br, :en => :title_en },
+ # any: { :"pt-BR" => :title_pt_br, :en => :title_en, :sv => :title_sv }
+ # },
+ # },
+ # :sv => {
+ # title: {
+ # default: { :sv => :title_sv, :en => :title_en },
+ # any: { :sv => :title_sv, :en => :title_en, :"pt-BR" => :title_pt_br }
+ # }
+ # }
+ # }
+ def traco_cache(attribute, fallback)
+ cache = @traco_cache ||= {}
+ per_locale_cache = cache[I18n.locale] ||= {}
+ per_attribute_cache = per_locale_cache[attribute] ||= {}
+
+ per_attribute_cache[fallback] ||= begin
+ locales_to_try = Traco.locale_with_fallbacks(I18n.locale, fallback)
+
+ locales_to_try.each_with_object({}) do |locale, columns|
+ column = Traco.column(attribute, locale)
+ columns[locale] = column if instance_methods.include?(column)
+ end
+ end
end
end
end