Commit 3600768ceceed5b804b5d208b0df307f9e7b1408
Committed by
Arthur Esposte
1 parent
fa7cddb0
Exists in
master
and in
27 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 | 2 | |
3 | 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 | 13 | end |
24 | 14 | |
25 | 15 | # formats a date for displaying. |
... | ... | @@ -91,15 +81,7 @@ module DatesHelper |
91 | 81 | _(date.strftime("%a")) |
92 | 82 | else |
93 | 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 | 85 | end |
104 | 86 | end |
105 | 87 | |
... | ... | @@ -111,7 +93,7 @@ module DatesHelper |
111 | 93 | date = date << 1 |
112 | 94 | end |
113 | 95 | if opts[:only_month] |
114 | - _('%{month}') % {:month => month_name(date.month.to_i) } | |
96 | + _('%{month}') % { :month => month_name(date.month.to_i) } | |
115 | 97 | else |
116 | 98 | _('%{month} %{year}') % { :year => date.year, :month => month_name(date.month.to_i) } |
117 | 99 | end |
... | ... | @@ -156,7 +138,7 @@ module DatesHelper |
156 | 138 | else |
157 | 139 | order = [:day, :month, :year] |
158 | 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 | 142 | end |
161 | 143 | |
162 | 144 | end | ... | ... |
app/models/blog_archives_block.rb
... | ... | @@ -36,8 +36,7 @@ class BlogArchivesBlock < Block |
36 | 36 | results << content_tag('li', content_tag('strong', "#{year} (#{count})")) |
37 | 37 | results << "<ul class='#{year}-archive'>" |
38 | 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 | 40 | end |
42 | 41 | results << "</ul>" |
43 | 42 | end | ... | ... |
test/unit/dates_helper_test.rb
... | ... | @@ -5,13 +5,16 @@ class DatesHelperTest < ActiveSupport::TestCase |
5 | 5 | include DatesHelper |
6 | 6 | |
7 | 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 | 13 | end |
11 | 14 | |
12 | 15 | should 'display date with translation' do |
16 | + expects(:month_name).with(1).returns('Janeiro') | |
13 | 17 | expects(:_).with('%{month_name} %{day}, %{year}').returns('%{day} de %{month_name} de %{year}') |
14 | - expects(:_).with('January').returns('Janeiro') | |
15 | 18 | assert_equal '11 de Janeiro de 2008', show_date(Date.new(2008, 1, 11)) |
16 | 19 | end |
17 | 20 | |
... | ... | @@ -68,75 +71,48 @@ class DatesHelperTest < ActiveSupport::TestCase |
68 | 71 | end |
69 | 72 | |
70 | 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 | 75 | end |
76 | 76 | |
77 | 77 | should 'show abbreviated day of week' do |
78 | - expects(:_).with("Sun").returns("Dom") | |
79 | 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 | 80 | end |
82 | 81 | |
83 | 82 | should 'show month' do |
84 | - expects(:_).with('January').returns('January') | |
85 | - expects(:_).with('%{month} %{year}').returns('%{month} %{year}') | |
86 | 83 | assert_equal 'January 2008', show_month(2008, 1) |
87 | 84 | end |
88 | 85 | |
89 | 86 | should 'fallback to current year/month in show_month' do |
90 | 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 | 88 | assert_equal 'November 2008', show_month(nil, nil) |
95 | 89 | assert_equal 'November 2008', show_month('', '') |
96 | 90 | end |
97 | 91 | |
98 | 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 | 93 | assert_equal 'November 2009', show_month(2009, 10, :next => true) |
102 | 94 | end |
103 | 95 | |
104 | 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 | 97 | assert_equal 'September 2009', show_month(2009, 10, :previous => true) |
108 | 98 | end |
109 | 99 | |
110 | 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 | 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 | 103 | assert_equal 'KKKKKKKK', pick_date(:object, :method) |
128 | 104 | end |
129 | 105 | |
130 | 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 | 110 | assert_equal 'KKKKKKKK', pick_date(:object, :method) |
135 | 111 | end |
136 | 112 | |
137 | 113 | should 'order date in other languages like day month year' do |
138 | 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 | 117 | assert_equal 'KKKKKKKK', pick_date(:object, :method) |
142 | 118 | end |
... | ... | @@ -151,9 +127,7 @@ class DatesHelperTest < ActiveSupport::TestCase |
151 | 127 | |
152 | 128 | should 'translate time' do |
153 | 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 | 131 | end |
158 | 132 | |
159 | 133 | should 'handle nil time' do | ... | ... |