Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'

gemspec
gemspec
116 changes: 116 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
en:
ice_cube:
pieces_connector: ' / '
not: 'not %{target}'
not_on: 'not on %{target}'
date:
formats:
default: '%B %-d, %Y'
month_names:
-
- January
- February
- March
- April
- May
- June
- July
- August
- September
- October
- November
- December
day_names:
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
times:
other: '%{count} times'
one: '%{count} time'
until: 'until %{date}'
days_of_week: '%{segments} %{day}'
days_of_month:
other: '%{segments} days of the month'
one: '%{segments} day of the month'
days_of_year:
other: '%{segments} days of the year'
one: '%{segments} day of the year'
at_hours_of_the_day:
other: on the %{segments} hours of the day
one: on the %{segments} hour of the day
on_minutes_of_hour:
other: on the %{segments} minutes of the hour
one: on the %{segments} minute of the hour
at_seconds_of_minute:
other: at the %{segments} seconds
one: at the %{segments} second
on_seconds_of_minute:
other: on the %{segments} seconds of the minute
one: on the %{segments} second of the minute
each_second:
one: Secondly
other: Every %{count} seconds
each_minute:
one: Minutely
other: Every %{count} minutes
each_hour:
one: Hourly
other: Every %{count} hours
each_day:
one: Daily
other: Every %{count} days
each_week:
one: Weekly
other: Every %{count} weeks
each_month:
one: Monthly
other: Every %{count} months
each_year:
one: Yearly
other: Every %{count} years
'on': on the %{sentence}
in: 'in %{target}'
integer:
negative: '%{ordinal} to last'
literal_ordinals:
-1: last
-2: 2nd to last
ordinal: '%{number}%{ordinal}'
ordinals:
default: th
1: st
2: nd
3: rd
11: th
12: th
13: th
on_weekends: on Weekends
on_weekdays: on Weekdays
days_on:
- Sundays
- Mondays
- Tuesdays
- Wednesdays
- Thursdays
- Fridays
- Saturdays
on_days: on %{days}
array:
last_word_connector: ', and '
two_words_connector: ' and '
words_connector: ', '
string:
format:
day: '%{rest} %{current}'
day_of_week: '%{rest} %{current}'
day_of_month: '%{rest} %{current}'
day_of_year: '%{rest} %{current}'
hour_of_day: '%{rest} %{current}'
minute_of_hour: '%{rest} %{current}'
until: '%{rest} %{current}'
count: '%{rest} %{current}'
default: '%{rest} %{current}'
107 changes: 107 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
ja:
ice_cube:
pieces_connector: ' / '
not: '%{target}以外'
not_on: '%{target}以外'
date:
formats:
default: '%Y年%m月%d日'
month_names:
-
- 1月
- 2月
- 3月
- 4月
- 5月
- 6月
- 7月
- 8月
- 9月
- 10月
- 11月
- 12月
day_names:
- 日曜
- 月曜
- 火曜
- 水曜
- 木曜
- 金曜
- 土曜
times:
other: '%{count}回'
one: '%{count}回'
until: '%{date}まで'
days_of_week: '%{segments}%{day}'
days_of_month:
other: '%{segments}日'
one: '%{segments}日'
days_of_year:
other: '%{segments}日'
one: '%{segments}日'
at_hours_of_the_day:
other: '%{segments}時'
one: '%{segments}時'
on_minutes_of_hour:
other: '%{segments}分'
one: '%{segments}分'
on_seconds_of_minute:
other: '%{segments}秒'
one: '%{segments}秒'
each_second:
one: 毎秒
other: '%{count}秒ごと'
each_minute:
one: 毎分
other: '%{count}分ごと'
each_hour:
one: 毎時間
other: '%{count}時間ごと'
each_day:
one: 毎日
other: '%{count}日ごと'
each_week:
one: 毎週
other: '%{count}週間ごと'
each_month:
one: 毎月
other: '%{count}ヶ月ごと'
each_year:
one: 毎年
other: '%{count}年ごと'
'on': '%{sentence}'
in: '%{target}'
integer:
negative: '最終%{ordinal}'
literal_ordinals:
-1: 最終
-2: 最後から2番目の
ordinal: '%{ordinal}%{number}'
ordinals:
default: ''
on_weekends: 週末
on_weekdays: 平日
days_on:
- 日曜
- 月曜
- 火曜
- 水曜
- 木曜
- 金曜
- 土曜
on_days: '%{days}'
array:
last_word_connector: '、'
two_words_connector: '、'
words_connector: '、'
string:
format:
day: '%{rest}%{current}'
day_of_week: '%{rest}%{current}'
day_of_month: '%{rest}%{current}'
day_of_year: '%{rest}%{current}'
hour_of_day: '%{rest}%{current}'
minute_of_hour: '%{rest}%{current}'
until: '%{current}%{rest}'
count: '%{rest}%{current}'
default: '%{rest}%{current}'
1 change: 1 addition & 0 deletions ice_cube.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ Gem::Specification.new do |s|
s.add_development_dependency('rspec', '~> 2.12.0')
s.add_development_dependency('activesupport', '>= 3.0.0')
s.add_development_dependency('tzinfo')
s.add_runtime_dependency('i18n')
end
5 changes: 4 additions & 1 deletion lib/ice_cube.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'date'
require 'ice_cube/deprecated'
require 'i18n'

I18n.load_path += Dir[File.expand_path('../../config/locales/*{rb,yml}', __FILE__)]

module IceCube

Expand Down Expand Up @@ -68,7 +71,7 @@ module Validations
# Defines the format used by IceCube when printing out Schedule#to_s.
# Defaults to '%B %e, %Y'
def self.to_s_time_format
@to_s_time_format ||= '%B %e, %Y'
@to_s_time_format ||= I18n.t("ice_cube.date.formats.default")
end

# Sets the format used by IceCube when printing out Schedule#to_s.
Expand Down
40 changes: 25 additions & 15 deletions lib/ice_cube/builders/string_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ def piece(type, prefix = nil, suffix = nil)
end

def to_s
@types.each_with_object(@base || '') do |(type, segments), str|
string = @base || ''
@types.each do |type, segments|
if f = self.class.formatter(type)
str << ' ' << f.call(segments)
current = f.call(segments)
else
next if segments.empty?
str << ' ' << self.class.sentence(segments)
current = self.class.sentence(segments)
end
f = I18n.t('ice_cube.string.format')[type] ? type : 'default'
string = I18n.t("ice_cube.string.format.#{f}", rest: string, current: current)
end
string
end

def self.formatter(type)
Expand All @@ -34,27 +38,33 @@ def self.register_formatter(type, &formatter)

module Helpers

NUMBER_SUFFIX = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']
SPECIAL_SUFFIX = { 11 => 'th', 12 => 'th', 13 => 'th', 14 => 'th' }

# influenced by ActiveSupport's to_sentence
def sentence(array)
case array.length
when 0 ; ''
when 1 ; array[0].to_s
when 2 ; "#{array[0]} and #{array[1]}"
else ; "#{array[0...-1].join(', ')}, and #{array[-1]}"
when 2 ; "#{array[0]}#{I18n.t('ice_cube.array.two_words_connector')}#{array[1]}"
else ; "#{array[0...-1].join(I18n.t('ice_cube.array.words_connector'))}#{I18n.t('ice_cube.array.last_word_connector')}#{array[-1]}"
end
end

def nice_number(number)
return 'last' if number == -1
suffix = SPECIAL_SUFFIX[number] || NUMBER_SUFFIX[number.abs % 10]
if number < -1
number.abs.to_s << suffix << ' to last'
else
number.to_s << suffix
end
literal_ordinal(number) || ordinalize(number)
end

def ordinalize(number)
I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number))
end

def literal_ordinal(number)
I18n.t("ice_cube.integer.literal_ordinals")[number]
end

def ordinal(number)
ord = I18n.t("ice_cube.integer.ordinals")[number] ||
I18n.t("ice_cube.integer.ordinals")[number % 10] ||
I18n.t('ice_cube.integer.ordinals')[:default]
number >= 0 ? ord : I18n.t("ice_cube.integer.negative", ordinal: ord)
end

end
Expand Down
8 changes: 5 additions & 3 deletions lib/ice_cube/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,11 @@ def to_s
rd = recurrence_times_with_start_time - extimes
pieces.concat rd.sort.map { |t| t.strftime(IceCube.to_s_time_format) }
pieces.concat rrules.map { |t| t.to_s }
pieces.concat exrules.map { |t| "not #{t.to_s}" }
pieces.concat extimes.sort.map { |t| "not on #{t.strftime(IceCube.to_s_time_format)}" }
pieces.join(' / ')
pieces.concat exrules.map { |t| I18n.t('ice_cube.not', target: t.to_s) }
pieces.concat extimes.sort.map { |t|
I18n.t('ice_cube.not_on', target: t.strftime(IceCube.to_s_time_format))
}
pieces.join(I18n.t('ice_cube.pieces_connector'))
end

# Serialize this schedule to_ical
Expand Down
2 changes: 1 addition & 1 deletion lib/ice_cube/validations/count.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def build_ical(builder)

StringBuilder.register_formatter(:count) do |segments|
count = segments.first
"#{count} #{count == 1 ? 'time' : 'times'}"
I18n.t('ice_cube.times', count: count)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/ice_cube/validations/daily_interval.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def validate(step_time, schedule)
end

def build_s(builder)
builder.base = interval == 1 ? 'Daily' : "Every #{interval} days"
builder.base = I18n.t('ice_cube.each_day', count: interval)
end

def build_hash(builder)
Expand Down
9 changes: 5 additions & 4 deletions lib/ice_cube/validations/day.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ def build_ical(builder)
validation_days.sort!
# pick the right shortening, if applicable
if validation_days == [0, 6]
'on Weekends'
I18n.t('ice_cube.on_weekends')
elsif validation_days == (1..5).to_a
'on Weekdays'
I18n.t('ice_cube.on_weekdays')
else
segments = validation_days.map { |d| "#{Date::DAYNAMES[d]}s" }
"on #{StringBuilder.sentence(segments)}"
day_names = ->(d){ "#{I18n.t("ice_cube.days_on")[d]}" }
segments = validation_days.map(&day_names)
I18n.t('ice_cube.on_days', days: StringBuilder.sentence(segments))
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/ice_cube/validations/day_of_month.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def build_ical(builder)
end

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

end
Expand Down
Loading