Created
January 30, 2012 21:36
-
-
Save conanite/1706871 to your computer and use it in GitHub Desktop.
ActiveRecord, I18n, Date, and UX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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