Commit 3600768ceceed5b804b5d208b0df307f9e7b1408
Committed by
Arthur Esposte
1 parent
fa7cddb0
Exists in
master
and in
21 other branches
DatesHelper now uses i18n from Rails
Showing
3 changed files
with
25 additions
and
70 deletions
Show diff stats
app/helpers/dates_helper.rb
| @@ -2,24 +2,14 @@ require 'noosfero/i18n' | @@ -2,24 +2,14 @@ require 'noosfero/i18n' | ||
| 2 | 2 | ||
| 3 | module DatesHelper | 3 | module DatesHelper |
| 4 | 4 | ||
| 5 | - # FIXME Date#strftime should translate this for us !!!! | ||
| 6 | - MONTHS = [ | ||
| 7 | - N_('January'), | ||
| 8 | - N_('February'), | ||
| 9 | - N_('March'), | ||
| 10 | - N_('April'), | ||
| 11 | - N_('May'), | ||
| 12 | - N_('June'), | ||
| 13 | - N_('July'), | ||
| 14 | - N_('August'), | ||
| 15 | - N_('September'), | ||
| 16 | - N_('October'), | ||
| 17 | - N_('November'), | ||
| 18 | - N_('December') | ||
| 19 | - ] | ||
| 20 | - | ||
| 21 | - def month_name(n) | ||
| 22 | - _(MONTHS[n-1]) | 5 | + MONTHS = I18n.t('date.month_names') |
| 6 | + | ||
| 7 | + def month_name(n, abbreviated = false) | ||
| 8 | + if abbreviated | ||
| 9 | + I18n.t('date.abbr_month_names')[n] | ||
| 10 | + else | ||
| 11 | + MONTHS[n] | ||
| 12 | + end | ||
| 23 | end | 13 | end |
| 24 | 14 | ||
| 25 | # formats a date for displaying. | 15 | # formats a date for displaying. |
| @@ -91,15 +81,7 @@ module DatesHelper | @@ -91,15 +81,7 @@ module DatesHelper | ||
| 91 | _(date.strftime("%a")) | 81 | _(date.strftime("%a")) |
| 92 | else | 82 | else |
| 93 | # FIXME Date#strftime should translate this for us !!!! | 83 | # FIXME Date#strftime should translate this for us !!!! |
| 94 | - _([ | ||
| 95 | - N_('Sunday'), | ||
| 96 | - N_('Monday'), | ||
| 97 | - N_('Tuesday'), | ||
| 98 | - N_('Wednesday'), | ||
| 99 | - N_('Thursday'), | ||
| 100 | - N_('Friday'), | ||
| 101 | - N_('Saturday'), | ||
| 102 | - ][date.wday]) | 84 | + I18n.t('date.day_names')[date.wday] |
| 103 | end | 85 | end |
| 104 | end | 86 | end |
| 105 | 87 | ||
| @@ -111,7 +93,7 @@ module DatesHelper | @@ -111,7 +93,7 @@ module DatesHelper | ||
| 111 | date = date << 1 | 93 | date = date << 1 |
| 112 | end | 94 | end |
| 113 | if opts[:only_month] | 95 | if opts[:only_month] |
| 114 | - _('%{month}') % {:month => month_name(date.month.to_i) } | 96 | + _('%{month}') % { :month => month_name(date.month.to_i) } |
| 115 | else | 97 | else |
| 116 | _('%{month} %{year}') % { :year => date.year, :month => month_name(date.month.to_i) } | 98 | _('%{month} %{year}') % { :year => date.year, :month => month_name(date.month.to_i) } |
| 117 | end | 99 | end |
| @@ -156,7 +138,7 @@ module DatesHelper | @@ -156,7 +138,7 @@ module DatesHelper | ||
| 156 | else | 138 | else |
| 157 | order = [:day, :month, :year] | 139 | order = [:day, :month, :year] |
| 158 | end | 140 | end |
| 159 | - date_select(object, method, html_options.merge(options.merge(:include_blank => true, :order => order, :use_month_names => MONTHS.map {|item| gettext(item)}))) | 141 | + date_select(object, method, html_options.merge(options.merge(:include_blank => true, :order => order, :use_month_names => MONTHS))) |
| 160 | end | 142 | end |
| 161 | 143 | ||
| 162 | end | 144 | end |
app/models/blog_archives_block.rb
| @@ -36,8 +36,7 @@ class BlogArchivesBlock < Block | @@ -36,8 +36,7 @@ class BlogArchivesBlock < Block | ||
| 36 | results << content_tag('li', content_tag('strong', "#{year} (#{count})")) | 36 | results << content_tag('li', content_tag('strong', "#{year} (#{count})")) |
| 37 | results << "<ul class='#{year}-archive'>" | 37 | results << "<ul class='#{year}-archive'>" |
| 38 | posts.except(:order).count(:all, :conditions => ['EXTRACT(YEAR FROM published_at)=?', year], :group => 'EXTRACT(MONTH FROM published_at)').sort_by {|month, count| -month.to_i}.each do |month, count| | 38 | posts.except(:order).count(:all, :conditions => ['EXTRACT(YEAR FROM published_at)=?', year], :group => 'EXTRACT(MONTH FROM published_at)').sort_by {|month, count| -month.to_i}.each do |month, count| |
| 39 | - month_name = gettext(MONTHS[month.to_i - 1]) | ||
| 40 | - results << content_tag('li', link_to("#{month_name} (#{count})", owner_blog.url.merge(:year => year, :month => month))) | 39 | + results << content_tag('li', link_to("#{month_name(month.to_i)} (#{count})", owner_blog.url.merge(:year => year, :month => month))) |
| 41 | end | 40 | end |
| 42 | results << "</ul>" | 41 | results << "</ul>" |
| 43 | end | 42 | end |
test/unit/dates_helper_test.rb
| @@ -5,13 +5,16 @@ class DatesHelperTest < ActiveSupport::TestCase | @@ -5,13 +5,16 @@ class DatesHelperTest < ActiveSupport::TestCase | ||
| 5 | include DatesHelper | 5 | include DatesHelper |
| 6 | 6 | ||
| 7 | should 'translate month names' do | 7 | should 'translate month names' do |
| 8 | - expects(:_).with('January').returns('Janeiro') | ||
| 9 | - assert_equal "Janeiro", month_name(1) | 8 | + assert_equal "January", month_name(1) |
| 9 | + end | ||
| 10 | + | ||
| 11 | + should 'translate abbreviated month names' do | ||
| 12 | + assert_equal "Sep", month_name(9, true) | ||
| 10 | end | 13 | end |
| 11 | 14 | ||
| 12 | should 'display date with translation' do | 15 | should 'display date with translation' do |
| 16 | + expects(:month_name).with(1).returns('Janeiro') | ||
| 13 | expects(:_).with('%{month_name} %{day}, %{year}').returns('%{day} de %{month_name} de %{year}') | 17 | expects(:_).with('%{month_name} %{day}, %{year}').returns('%{day} de %{month_name} de %{year}') |
| 14 | - expects(:_).with('January').returns('Janeiro') | ||
| 15 | assert_equal '11 de Janeiro de 2008', show_date(Date.new(2008, 1, 11)) | 18 | assert_equal '11 de Janeiro de 2008', show_date(Date.new(2008, 1, 11)) |
| 16 | end | 19 | end |
| 17 | 20 | ||
| @@ -68,75 +71,48 @@ class DatesHelperTest < ActiveSupport::TestCase | @@ -68,75 +71,48 @@ class DatesHelperTest < ActiveSupport::TestCase | ||
| 68 | end | 71 | end |
| 69 | 72 | ||
| 70 | should 'show day of week' do | 73 | should 'show day of week' do |
| 71 | - expects(:_).with("Sunday").returns("Domingo") | ||
| 72 | - date = mock | ||
| 73 | - date.expects(:wday).returns(0) | ||
| 74 | - assert_equal "Domingo", show_day_of_week(date) | 74 | + assert_equal "Thursday", show_day_of_week(Date.new(2014,10,23)) |
| 75 | end | 75 | end |
| 76 | 76 | ||
| 77 | should 'show abbreviated day of week' do | 77 | should 'show abbreviated day of week' do |
| 78 | - expects(:_).with("Sun").returns("Dom") | ||
| 79 | date = Date.new(2009, 10, 25) | 78 | date = Date.new(2009, 10, 25) |
| 80 | - assert_equal "Dom", show_day_of_week(date, true) | 79 | + assert_equal "Sun", show_day_of_week(date, true) |
| 81 | end | 80 | end |
| 82 | 81 | ||
| 83 | should 'show month' do | 82 | should 'show month' do |
| 84 | - expects(:_).with('January').returns('January') | ||
| 85 | - expects(:_).with('%{month} %{year}').returns('%{month} %{year}') | ||
| 86 | assert_equal 'January 2008', show_month(2008, 1) | 83 | assert_equal 'January 2008', show_month(2008, 1) |
| 87 | end | 84 | end |
| 88 | 85 | ||
| 89 | should 'fallback to current year/month in show_month' do | 86 | should 'fallback to current year/month in show_month' do |
| 90 | Date.expects(:today).returns(Date.new(2008,11,1)).at_least_once | 87 | Date.expects(:today).returns(Date.new(2008,11,1)).at_least_once |
| 91 | - | ||
| 92 | - expects(:_).with('November').returns('November').at_least_once | ||
| 93 | - expects(:_).with('%{month} %{year}').returns('%{month} %{year}').at_least_once | ||
| 94 | assert_equal 'November 2008', show_month(nil, nil) | 88 | assert_equal 'November 2008', show_month(nil, nil) |
| 95 | assert_equal 'November 2008', show_month('', '') | 89 | assert_equal 'November 2008', show_month('', '') |
| 96 | end | 90 | end |
| 97 | 91 | ||
| 98 | should 'show next month' do | 92 | should 'show next month' do |
| 99 | - expects(:_).with('November').returns('November').at_least_once | ||
| 100 | - expects(:_).with('%{month} %{year}').returns('%{month} %{year}').at_least_once | ||
| 101 | assert_equal 'November 2009', show_month(2009, 10, :next => true) | 93 | assert_equal 'November 2009', show_month(2009, 10, :next => true) |
| 102 | end | 94 | end |
| 103 | 95 | ||
| 104 | should 'show previous month' do | 96 | should 'show previous month' do |
| 105 | - expects(:_).with('September').returns('September').at_least_once | ||
| 106 | - expects(:_).with('%{month} %{year}').returns('%{month} %{year}').at_least_once | ||
| 107 | assert_equal 'September 2009', show_month(2009, 10, :previous => true) | 97 | assert_equal 'September 2009', show_month(2009, 10, :previous => true) |
| 108 | end | 98 | end |
| 109 | 99 | ||
| 110 | should 'provide an intertionalized date selector pass month names' do | 100 | should 'provide an intertionalized date selector pass month names' do |
| 111 | - expects(:gettext).with('January').returns('January') | ||
| 112 | - expects(:gettext).with('February').returns('February') | ||
| 113 | - expects(:gettext).with('March').returns('March') | ||
| 114 | - expects(:gettext).with('April').returns('April') | ||
| 115 | - expects(:gettext).with('May').returns('May') | ||
| 116 | - expects(:gettext).with('June').returns('June') | ||
| 117 | - expects(:gettext).with('July').returns('July') | ||
| 118 | - expects(:gettext).with('August').returns('August') | ||
| 119 | - expects(:gettext).with('September').returns('September') | ||
| 120 | - expects(:gettext).with('October').returns('October') | ||
| 121 | - expects(:gettext).with('November').returns('November') | ||
| 122 | - expects(:gettext).with('December').returns('December') | ||
| 123 | expects(:language).returns('en') | 101 | expects(:language).returns('en') |
| 124 | - | ||
| 125 | - expects(:date_select).with(:object, :method, { :include_blank => true, :order => [:month, :day, :year], :use_month_names => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']}).returns("KKKKKKKK") | ||
| 126 | - | 102 | + expects(:date_select).with(:object, :method, { :include_blank => true, :order => [:month, :day, :year], :use_month_names => MONTHS }).returns("KKKKKKKK") |
| 127 | assert_equal 'KKKKKKKK', pick_date(:object, :method) | 103 | assert_equal 'KKKKKKKK', pick_date(:object, :method) |
| 128 | end | 104 | end |
| 129 | 105 | ||
| 130 | should 'order date in english like month day year' do | 106 | should 'order date in english like month day year' do |
| 131 | - expects(:language).returns("en") | ||
| 132 | - expects(:date_select).with(:object, :method, { :include_blank => true, :order => [:month, :day, :year], :use_month_names => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']}).returns("KKKKKKKK") | 107 | + expects(:language).returns('en') |
| 108 | + expects(:date_select).with(:object, :method, { :include_blank => true, :order => [:month, :day, :year], :use_month_names => MONTHS }).returns("KKKKKKKK") | ||
| 133 | 109 | ||
| 134 | assert_equal 'KKKKKKKK', pick_date(:object, :method) | 110 | assert_equal 'KKKKKKKK', pick_date(:object, :method) |
| 135 | end | 111 | end |
| 136 | 112 | ||
| 137 | should 'order date in other languages like day month year' do | 113 | should 'order date in other languages like day month year' do |
| 138 | expects(:language).returns('pt_BR') | 114 | expects(:language).returns('pt_BR') |
| 139 | - expects(:date_select).with(:object, :method, { :include_blank => true, :order => [:day, :month, :year], :use_month_names => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']}).returns("KKKKKKKK") | 115 | + expects(:date_select).with(:object, :method, { :include_blank => true, :order => [:day, :month, :year], :use_month_names => MONTHS }).returns("KKKKKKKK") |
| 140 | 116 | ||
| 141 | assert_equal 'KKKKKKKK', pick_date(:object, :method) | 117 | assert_equal 'KKKKKKKK', pick_date(:object, :method) |
| 142 | end | 118 | end |
| @@ -151,9 +127,7 @@ class DatesHelperTest < ActiveSupport::TestCase | @@ -151,9 +127,7 @@ class DatesHelperTest < ActiveSupport::TestCase | ||
| 151 | 127 | ||
| 152 | should 'translate time' do | 128 | should 'translate time' do |
| 153 | time = Time.parse('25 May 2009, 12:47') | 129 | time = Time.parse('25 May 2009, 12:47') |
| 154 | - expects(:_).with('%{day} %{month} %{year}, %{hour}:%{minutes}').returns('translated time') | ||
| 155 | - stubs(:_).with('May').returns("Maio") | ||
| 156 | - assert_equal 'translated time', show_time(time) | 130 | + assert_equal '25 May 2009, 12:47', show_time(time) |
| 157 | end | 131 | end |
| 158 | 132 | ||
| 159 | should 'handle nil time' do | 133 | should 'handle nil time' do |