Skip to content

Instantly share code, notes, and snippets.

@conanite
Created January 30, 2012 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conanite/1706871 to your computer and use it in GitHub Desktop.
Save conanite/1706871 to your computer and use it in GitHub Desktop.
ActiveRecord, I18n, Date, and UX
class Date
class << self
alias :_ruby_parse :_parse
def _parse(str, comp=true)
if I18n.locale.to_sym == :en
_ruby_parse str, comp
else
map = month_map I18n.locale
str = str.gsub /[^\s\/\d-]+/ do |month|
map[month.normalize] || month
end
_ruby_parse str, comp
end
end
private
def month_map locale
@@month_map ||= { }
@@month_map[locale] ||= make_month_map(locale)
end
def make_month_map locale
abbrev_months_en = I18n.t "date.abbr_month_names", :locale => :en
abbrev_months_local = I18n.t "date.abbr_month_names", :locale => locale
full_months_local = I18n.t "date.month_names", :locale => locale
result = { }
abbrev_months_en.each_with_index do |mon, i|
if mon
result[abbrev_months_local[i].normalize] = mon
result[full_months_local[i].normalize] = mon
end
end
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment