Skip to content

Commit c325a00

Browse files
Tomo Matsumotogreysteil
authored andcommitted
use I18n
1 parent 6868b2c commit c325a00

File tree

14 files changed

+97
-29
lines changed

14 files changed

+97
-29
lines changed

config/locales/en.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
en:
2+
ice_cube:
3+
times:
4+
other: '%{count} times'
5+
one: '%{count} time'
6+
days_of_month:
7+
other: '%{segments} days of the month'
8+
one: '%{segments} day of the month'
9+
days_of_year:
10+
other: '%{segments} days of the year'
11+
one: '%{segments} day of the year'
12+
at_hours_of_the_day:
13+
other: on the %{segments} hours of the day
14+
one: on the %{segments} hour of the day
15+
on_minutes_of_hour:
16+
other: on the %{segments} minutes of the hour
17+
one: on the %{segments} minute of the hour
18+
at_seconds_of_minute:
19+
other: at the %{segments} seconds
20+
one: at the %{segments} second
21+
on_seconds_of_minute:
22+
other: on the %{segments} seconds of the minute
23+
one: on the %{segments} second of the minute
24+
'on': on the %{sentence}
25+
in: in
26+
integer:
27+
negative: '%{ordinal} to last'
28+
literal_ordinals:
29+
-1: last
30+
-2: 2nd to last
31+
ordinals:
32+
default: th
33+
1: st
34+
2: nd
35+
3: rd
36+
11: th
37+
12: th
38+
13: th
39+
on_weekends: on Weekends
40+
on_weekdays: on Weekdays
41+
days_on:
42+
- Sundays
43+
- Mondays
44+
- Tuesdays
45+
- Wednesdays
46+
- Thursdays
47+
- Fridays
48+
- Saturdays
49+
on_days: on %{days}
50+
support:
51+
array:
52+
two_words_connector: ' and '

ice_cube.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ Gem::Specification.new do |s|
2121
s.add_development_dependency('rspec', '~> 2.12.0')
2222
s.add_development_dependency('activesupport', '>= 3.0.0')
2323
s.add_development_dependency('tzinfo')
24+
s.add_development_dependency('i18n')
2425
end

lib/ice_cube.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
require 'date'
22
require 'ice_cube/deprecated'
3+
require 'i18n'
4+
5+
I18n.load_path += Dir[File.expand_path('../../config/locales/*{rb,yml}', __FILE__)]
36

47
module IceCube
58

lib/ice_cube/builders/string_builder.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ def self.register_formatter(type, &formatter)
3434

3535
module Helpers
3636

37-
NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
38-
SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }
39-
4037
# influenced by ActiveSupport's to_sentence
4138
def sentence(array)
4239
case array.length
@@ -48,13 +45,22 @@ def sentence(array)
4845
end
4946

5047
def nice_number(number)
51-
return 'last' if number == -1
52-
suffix = SPECIAL_SUFFIX[number] || NUMBER_SUFFIX[number.abs % 10]
53-
if number < -1
54-
number.abs.to_s << suffix << ' to last'
55-
else
56-
number.to_s << suffix
57-
end
48+
literal_ordinal(number) || ordinalize(number)
49+
end
50+
51+
def ordinalize(number)
52+
"#{number}#{ordinal(number)}"
53+
end
54+
55+
def literal_ordinal(number)
56+
I18n.t("ice_cube.integer.literal_ordinals")[number]
57+
end
58+
59+
def ordinal(number)
60+
ord = I18n.t("ice_cube.integer.ordinals")[number] ||
61+
I18n.t("ice_cube.integer.ordinals")[number % 10] ||
62+
I18n.t('ice_cube.integer.ordinals')[:default]
63+
number >= 0 ? ord : I18n.t("ice_cube.integer.negative", ordinal: ord)
5864
end
5965

6066
end

lib/ice_cube/validations/count.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def build_ical(builder)
5151

5252
StringBuilder.register_formatter(:count) do |segments|
5353
count = segments.first
54-
"#{count} #{count == 1 ? 'time' : 'times'}"
54+
I18n.t('ice_cube.times', count: count)
5555
end
5656

5757
end

lib/ice_cube/validations/day.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ def build_ical(builder)
5454
validation_days.sort!
5555
# pick the right shortening, if applicable
5656
if validation_days == [0, 6]
57-
'on Weekends'
57+
I18n.t('ice_cube.on_weekends')
5858
elsif validation_days == (1..5).to_a
59-
'on Weekdays'
59+
I18n.t('ice_cube.on_weekdays')
6060
else
61-
segments = validation_days.map { |d| "#{Date::DAYNAMES[d]}s" }
62-
"on #{StringBuilder.sentence(segments)}"
61+
day_names = ->(d){ "#{I18n.t("ice_cube.days_on")[d]}" }
62+
segments = validation_days.map(&day_names)
63+
I18n.t('ice_cube.on_days', days: StringBuilder.sentence(segments))
6364
end
6465
end
6566

lib/ice_cube/validations/day_of_month.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def build_ical(builder)
4343
end
4444

4545
StringBuilder.register_formatter(:day_of_month) do |entries|
46-
str = "on the #{StringBuilder.sentence(entries)} "
47-
str << (entries.size == 1 ? 'day of the month' : 'days of the month')
48-
str
46+
sentence = StringBuilder.sentence(entries)
47+
str = I18n.t('ice_cube.days_of_month', count: entries.size, segments: sentence)
48+
I18n.t('ice_cube.on', sentence: str)
4949
end
5050

5151
end

lib/ice_cube/validations/day_of_week.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def build_ical(builder)
6262
end
6363

6464
StringBuilder.register_formatter(:day_of_week) do |segments|
65-
'on the ' + segments.join(' and ')
65+
sentence = segments.join(I18n.t('support.array.two_words_connector'))
66+
I18n.t('ice_cube.on', sentence: sentence)
6667
end
6768

6869
end

lib/ice_cube/validations/day_of_year.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def build_ical(builder)
4949
end
5050

5151
StringBuilder.register_formatter(:day_of_year) do |entries|
52-
str = "on the #{StringBuilder.sentence(entries)} "
53-
str << (entries.size == 1 ? 'day of the year' : 'days of the year')
54-
str
52+
str = StringBuilder.sentence(entries)
53+
sentence = I18n.t('ice_cube.days_of_year', count: entries.size, segments: str)
54+
I18n.t('ice_cube.on', sentence: sentence)
5555
end
5656

5757
end

lib/ice_cube/validations/hour_of_day.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def build_ical(builder)
4444
end
4545

4646
StringBuilder.register_formatter(:hour_of_day) do |segments|
47-
str = "on the #{StringBuilder.sentence(segments)} "
48-
str << (segments.size == 1 ? 'hour of the day' : 'hours of the day')
47+
str = StringBuilder.sentence(segments)
48+
I18n.t('ice_cube.at_hours_of_the_day', count: segments.size, segments: str)
4949
end
5050

5151
end

0 commit comments

Comments
 (0)