Skip to content

Commit

Permalink
Decompose I18n::Backend::Base#localize
Browse files Browse the repository at this point in the history
  • Loading branch information
corlinus committed Nov 7, 2016
1 parent 4b3725d commit 8ac0252
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,7 @@ def localize(locale, object, format = :default, options = {})
format = I18n.t(:"#{type}.formats.#{key}", options)
end

# format = resolve(locale, object, format, options)
format = format.to_s.gsub(/%[aAbBpP]/) do |match|
case match
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
when '%P' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
end
end

format = translate_format(format)
object.strftime(format)
end

Expand Down Expand Up @@ -184,6 +173,19 @@ def load_yml(filename)
raise InvalidLocaleData.new(filename, e.inspect)
end
end

def translate_format(format)
format.to_s.gsub(/%[aAbBpP]/) do |match|
case match
when '%a' then I18n.t(:"date.abbr_day_names", :locale => locale, :format => format)[object.wday]
when '%A' then I18n.t(:"date.day_names", :locale => locale, :format => format)[object.wday]
when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon]
when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).upcase if object.respond_to? :hour
when '%P' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format).downcase if object.respond_to? :hour
end
end
end
end
end
end

0 comments on commit 8ac0252

Please sign in to comment.