diff --git a/Gemfile b/Gemfile index fa75df15..cd8aa9e0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,3 @@ source 'https://rubygems.org' -gemspec +gemspec \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 00000000..b1ad9023 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,178 @@ +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}' + + date: + abbr_day_names: + - Sun + - Mon + - Tue + - Wed + - Thu + - Fri + - Sat + abbr_month_names: + - + - Jan + - Feb + - Mar + - Apr + - May + - Jun + - Jul + - Aug + - Sep + - Oct + - Nov + - Dec + day_names: + - Sunday + - Monday + - Tuesday + - Wednesday + - Thursday + - Friday + - Saturday + formats: + default: "%Y-%m-%d" + long: "%B %d, %Y" + short: "%b %d" + month_names: + - + - January + - February + - March + - April + - May + - June + - July + - August + - September + - October + - November + - December + order: + - :year + - :month + - :day + + time: + am: am + formats: + default: "%a, %d %b %Y %H:%M:%S %z" + long: "%B %d, %Y %H:%M" + short: "%d %b %H:%M" + pm: pm diff --git a/config/locales/es.yml b/config/locales/es.yml new file mode 100644 index 00000000..aebd3f01 --- /dev/null +++ b/config/locales/es.yml @@ -0,0 +1,176 @@ +es: + ice_cube: + pieces_connector: ', ' + not: 'excepto %{target}' + not_on: 'excepto el %{target}' + date: + formats: + default: '%-d de %B de %Y' + month_names: + - + - Enero + - Febrero + - Marzo + - Abril + - Mayo + - Junio + - Julio + - Agosto + - Septiembre + - Octubre + - Noviembre + - Diciembre + day_names: + - Domingo + - Lunes + - Martes + - Miércoles + - Jueves + - Viernes + - Sábado + times: + other: '%{count} veces' + one: '%{count} vez' + until: 'hasta el %{date}' + days_of_week: '%{segments} %{day}' + days_of_month: + other: 'los días %{segments} del mes' + one: 'el día %{segments} del mes' + days_of_year: + one: 'el día %{segments}' + other: 'los días %{segments}' + at_hours_of_the_day: + one: 'en la hora %{segments}' + other: 'en las horas %{segments}' + on_minutes_of_hour: + one: 'en el minuto %{segments}' + other: 'en los minutos %{segments}' + at_seconds_of_minute: + one: 'en el segundo %{segments}' + other: 'en los segundos %{segments}' + on_seconds_of_minute: + one: 'en el segundo %{segments} del minuto' + other: 'en los segundos %{segments} del minuto' + on_days: '%{days}' + each_second: + one: Cada segundo + other: 'Cada %{count} segundos' + each_minute: + one: Cada minuto + other: 'Cada %{count} minutos' + each_hour: + one: Cada hora + other: 'Cada %{count} horas' + each_day: + one: Diariamente + other: 'Cada %{count} días' + each_week: + one: Semanalmente + other: 'Cada %{count} semanas' + each_month: + one: Mensualmente + other: 'Cada %{count} meses' + each_year: + one: Anualmente + other: 'Cada %{count} años' + 'on': 'en %{sentence}' + in: 'en %{target}' + integer: + negative: '%{ordinal} por la cola' + literal_ordinals: + -1: el último + -2: el penúltimo + ordinal: '%{number}%{ordinal}' + ordinals: + default: º + 1: º + 2: º + 3: º + 11: º + 12: º + 13: º + on_weekends: en fin de semana + on_weekdays: en días laborables + days_on: + - los domingos + - los lunes + - los martes + - los miércoles + - los jueves + - los viernes + - los sábados + on_days: '%{days}' + array: + last_word_connector: ' y ' + two_words_connector: ' y ' + 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}' + + date: + day_names: + - Domingo + - Lunes + - Martes + - Miércoles + - Jueves + - Viernes + - Sábado + abbr_day_names: + - Dom + - Lun + - Mar + - Mie + - Jue + - Vie + - Sab + # Don't forget the nil at the beginning; there's no such thing as a 0th month + month_names: + - + - Enero + - Febrero + - Marzo + - Abril + - Mayo + - Junio + - Julio + - Agosto + - Septiembre + - Octubre + - Noviembre + - Diciembre + abbr_month_names: + - + - Ene + - Feb + - Mar + - Abr + - May + - Jun + - Jul + - Ago + - Sep + - Oct + - Nov + - Dic + formats: + default: '%d/%m/%Y' + long: '%d de %B de %Y' + short: '%d de %B' + + time: + formats: + default: "%A, %d de %B de %Y %H:%M:%S %z" + short: "%d de %b %H:%M" + long: "%d de %B de %Y a las %H:%M" + am: "am" + pm: "pm" diff --git a/config/locales/ja.yml b/config/locales/ja.yml new file mode 100644 index 00000000..a113af1a --- /dev/null +++ b/config/locales/ja.yml @@ -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}' diff --git a/ice_cube.gemspec b/ice_cube.gemspec index 9854163d..6f89238d 100644 --- a/ice_cube.gemspec +++ b/ice_cube.gemspec @@ -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_development_dependency('i18n') end diff --git a/lib/ice_cube.rb b/lib/ice_cube.rb index 4375ee53..fe03d9d5 100644 --- a/lib/ice_cube.rb +++ b/lib/ice_cube.rb @@ -1,5 +1,8 @@ require 'date' require 'ice_cube/deprecated' +require 'ice_cube/i18n' + +IceCube::I18n.detect_backend! module IceCube @@ -69,7 +72,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' + IceCube::I18n.t("ice_cube.date.formats.default") end # Sets the format used by IceCube when printing out Schedule#to_s. diff --git a/lib/ice_cube/builders/ical_builder.rb b/lib/ice_cube/builders/ical_builder.rb index d6592a6a..2ba4e646 100644 --- a/lib/ice_cube/builders/ical_builder.rb +++ b/lib/ice_cube/builders/ical_builder.rb @@ -32,15 +32,15 @@ def to_s def self.ical_utc_format(time) time = time.dup.utc - "#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time + IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ') # utc time end def self.ical_format(time, force_utc) time = time.dup.utc if force_utc if time.utc? - ":#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time + ":#{IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ')}" # utc time else - ";TZID=#{time.strftime('%Z:%Y%m%dT%H%M%S')}" # local time specified + ";TZID=#{IceCube::I18n.l(time, format: '%Z:%Y%m%dT%H%M%S')}" # local time specified end end diff --git a/lib/ice_cube/builders/string_builder.rb b/lib/ice_cube/builders/string_builder.rb index de4585d7..dda38dcc 100644 --- a/lib/ice_cube/builders/string_builder.rb +++ b/lib/ice_cube/builders/string_builder.rb @@ -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 = IceCube::I18n.t('ice_cube.string.format')[type] ? type : 'default' + string = IceCube::I18n.t("ice_cube.string.format.#{f}", rest: string, current: current) end + string end def self.formatter(type) @@ -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]}#{IceCube::I18n.t('ice_cube.array.two_words_connector')}#{array[1]}" + else ; "#{array[0...-1].join(IceCube::I18n.t('ice_cube.array.words_connector'))}#{IceCube::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) + IceCube::I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number)) + end + + def literal_ordinal(number) + IceCube::I18n.t("ice_cube.integer.literal_ordinals")[number] + end + + def ordinal(number) + ord = IceCube::I18n.t("ice_cube.integer.ordinals")[number] || + IceCube::I18n.t("ice_cube.integer.ordinals")[number % 10] || + IceCube::I18n.t('ice_cube.integer.ordinals')[:default] + number >= 0 ? ord : IceCube::I18n.t("ice_cube.integer.negative", ordinal: ord) end end diff --git a/lib/ice_cube/i18n.rb b/lib/ice_cube/i18n.rb new file mode 100644 index 00000000..a9c090d9 --- /dev/null +++ b/lib/ice_cube/i18n.rb @@ -0,0 +1,24 @@ +module IceCube + module I18n + def self.t(*args) + backend.t(*args) + end + + def self.l(*args) + backend.l(*args) + end + + def self.backend + @backend + end + + def self.detect_backend! + require 'i18n' + ::I18n.load_path += Dir[File.expand_path('../../../config/locales/*{rb,yml}', __FILE__)] + @backend = ::I18n + rescue LoadError + require 'ice_cube/null_i18n' + @backend = NullI18n + end + end +end diff --git a/lib/ice_cube/null_i18n.rb b/lib/ice_cube/null_i18n.rb new file mode 100644 index 00000000..1dd37fcf --- /dev/null +++ b/lib/ice_cube/null_i18n.rb @@ -0,0 +1,28 @@ +require 'yaml' + +module IceCube + module NullI18n + def self.t(key, options = {}) + base = key.to_s.split('.').reduce(config) { |hash, current_key| hash[current_key] } + + base = base[options[:count] == 1 ? "one" : "other"] if options[:count] + + if base.is_a?(Hash) + return base.each_with_object({}) do |(key, value), hash| + hash[key.is_a?(String) ? key.to_sym : key] = value + end + end + + options.reduce(base) { |result, (find, replace)| result.gsub("%{#{find}}", "#{replace}") } + end + + def self.l(date_or_time, options = {}) + return date_or_time.strftime(options[:format]) if options[:format] + date_or_time.strftime(t('ice_cube.date.formats.default')) + end + + def self.config + @config ||= YAML.load(File.read(File.join(File.dirname(__FILE__), '..', '..', 'config', 'locales', 'en.yml')))['en'] + end + end +end diff --git a/lib/ice_cube/schedule.rb b/lib/ice_cube/schedule.rb index 73303ff3..903c28fa 100644 --- a/lib/ice_cube/schedule.rb +++ b/lib/ice_cube/schedule.rb @@ -313,11 +313,14 @@ def last(n = nil) def to_s pieces = [] rd = recurrence_times_with_start_time - extimes - pieces.concat rd.sort.map { |t| t.strftime(IceCube.to_s_time_format) } + pieces.concat rd.sort.map { |t| IceCube::I18n.l(t, format: 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| IceCube::I18n.t('ice_cube.not', target: t.to_s) } + pieces.concat extimes.sort.map { |t| + target = IceCube::I18n.l(t, format: IceCube.to_s_time_format) + IceCube::I18n.t('ice_cube.not_on', target: target) + } + pieces.join(IceCube::I18n.t('ice_cube.pieces_connector')) end # Serialize this schedule to_ical diff --git a/lib/ice_cube/validations/count.rb b/lib/ice_cube/validations/count.rb index 61099433..be22fd9e 100644 --- a/lib/ice_cube/validations/count.rb +++ b/lib/ice_cube/validations/count.rb @@ -51,7 +51,7 @@ def build_ical(builder) StringBuilder.register_formatter(:count) do |segments| count = segments.first - "#{count} #{count == 1 ? 'time' : 'times'}" + IceCube::I18n.t('ice_cube.times', count: count) end end diff --git a/lib/ice_cube/validations/daily_interval.rb b/lib/ice_cube/validations/daily_interval.rb index edf8ce72..635e77f1 100644 --- a/lib/ice_cube/validations/daily_interval.rb +++ b/lib/ice_cube/validations/daily_interval.rb @@ -35,7 +35,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Daily' : "Every #{interval} days" + builder.base = IceCube::I18n.t('ice_cube.each_day', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/day.rb b/lib/ice_cube/validations/day.rb index c29396f0..4c6bc56e 100644 --- a/lib/ice_cube/validations/day.rb +++ b/lib/ice_cube/validations/day.rb @@ -54,12 +54,13 @@ def build_ical(builder) validation_days.sort! # pick the right shortening, if applicable if validation_days == [0, 6] - 'on Weekends' + IceCube::I18n.t('ice_cube.on_weekends') elsif validation_days == (1..5).to_a - 'on Weekdays' + IceCube::I18n.t('ice_cube.on_weekdays') else - segments = validation_days.map { |d| "#{Date::DAYNAMES[d]}s" } - "on #{StringBuilder.sentence(segments)}" + day_names = ->(d){ "#{IceCube::I18n.t("ice_cube.days_on")[d]}" } + segments = validation_days.map(&day_names) + IceCube::I18n.t('ice_cube.on_days', days: StringBuilder.sentence(segments)) end end diff --git a/lib/ice_cube/validations/day_of_month.rb b/lib/ice_cube/validations/day_of_month.rb index ef2cd2ad..3acccd7a 100644 --- a/lib/ice_cube/validations/day_of_month.rb +++ b/lib/ice_cube/validations/day_of_month.rb @@ -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 = IceCube::I18n.t('ice_cube.days_of_month', count: entries.size, segments: sentence) + IceCube::I18n.t('ice_cube.on', sentence: str) end end diff --git a/lib/ice_cube/validations/day_of_week.rb b/lib/ice_cube/validations/day_of_week.rb index de40040e..a35ba258 100644 --- a/lib/ice_cube/validations/day_of_week.rb +++ b/lib/ice_cube/validations/day_of_week.rb @@ -45,7 +45,11 @@ def validate(step_time, schedule) end def build_s(builder) - builder.piece(:day_of_week) << "#{StringBuilder.nice_number(occ)} #{Date::DAYNAMES[day]}" + builder.piece(:day_of_week) << IceCube::I18n.t( + 'ice_cube.days_of_week', + segments: StringBuilder.nice_number(occ), + day: IceCube::I18n.t('ice_cube.date.day_names')[day] + ) end def build_hash(builder) @@ -62,7 +66,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_week) do |segments| - 'on the ' + segments.join(' and ') + sentence = segments.join(IceCube::I18n.t('ice_cube.array.two_words_connector')) + IceCube::I18n.t('ice_cube.on', sentence: sentence) end end diff --git a/lib/ice_cube/validations/day_of_year.rb b/lib/ice_cube/validations/day_of_year.rb index b68c980b..e4eba5e8 100644 --- a/lib/ice_cube/validations/day_of_year.rb +++ b/lib/ice_cube/validations/day_of_year.rb @@ -49,9 +49,9 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_year) do |entries| - str = "on the #{StringBuilder.sentence(entries)} " - str << (entries.size == 1 ? 'day of the year' : 'days of the year') - str + str = StringBuilder.sentence(entries) + sentence = IceCube::I18n.t('ice_cube.days_of_year', count: entries.size, segments: str) + IceCube::I18n.t('ice_cube.on', sentence: sentence) end end diff --git a/lib/ice_cube/validations/hour_of_day.rb b/lib/ice_cube/validations/hour_of_day.rb index a171722b..e5258006 100644 --- a/lib/ice_cube/validations/hour_of_day.rb +++ b/lib/ice_cube/validations/hour_of_day.rb @@ -44,8 +44,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:hour_of_day) do |segments| - str = "on the #{StringBuilder.sentence(segments)} " - str << (segments.size == 1 ? 'hour of the day' : 'hours of the day') + str = StringBuilder.sentence(segments) + IceCube::I18n.t('ice_cube.at_hours_of_the_day', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/hourly_interval.rb b/lib/ice_cube/validations/hourly_interval.rb index ce2484c4..789b82c1 100644 --- a/lib/ice_cube/validations/hourly_interval.rb +++ b/lib/ice_cube/validations/hourly_interval.rb @@ -35,7 +35,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Hourly' : "Every #{interval} hours" + builder.base = IceCube::I18n.t("ice_cube.each_hour", count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/minute_of_hour.rb b/lib/ice_cube/validations/minute_of_hour.rb index bf1e26f9..dcc70c6f 100644 --- a/lib/ice_cube/validations/minute_of_hour.rb +++ b/lib/ice_cube/validations/minute_of_hour.rb @@ -43,8 +43,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:minute_of_hour) do |segments| - str = "on the #{StringBuilder.sentence(segments)} " - str << (segments.size == 1 ? 'minute of the hour' : 'minutes of the hour') + str = StringBuilder.sentence(segments) + IceCube::I18n.t('ice_cube.on_minutes_of_hour', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/minutely_interval.rb b/lib/ice_cube/validations/minutely_interval.rb index eb03f366..63c8894b 100644 --- a/lib/ice_cube/validations/minutely_interval.rb +++ b/lib/ice_cube/validations/minutely_interval.rb @@ -35,7 +35,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Minutely' : "Every #{interval} minutes" + builder.base = IceCube::I18n.t('ice_cube.each_minute', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/month_of_year.rb b/lib/ice_cube/validations/month_of_year.rb index 5c9c0f98..d7431f2f 100644 --- a/lib/ice_cube/validations/month_of_year.rb +++ b/lib/ice_cube/validations/month_of_year.rb @@ -32,7 +32,7 @@ def dst_adjust? end def build_s(builder) - builder.piece(:month_of_year) << Date::MONTHNAMES[month] + builder.piece(:month_of_year) << IceCube::I18n.t("ice_cube.date.month_names")[month] end def build_hash(builder) @@ -44,7 +44,7 @@ def build_ical(builder) end StringBuilder.register_formatter(:month_of_year) do |segments| - "in #{StringBuilder.sentence(segments)}" + IceCube::I18n.t("ice_cube.in", target: StringBuilder.sentence(segments)) end end diff --git a/lib/ice_cube/validations/monthly_interval.rb b/lib/ice_cube/validations/monthly_interval.rb index 61fde0c5..3939d4a9 100644 --- a/lib/ice_cube/validations/monthly_interval.rb +++ b/lib/ice_cube/validations/monthly_interval.rb @@ -34,7 +34,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Monthly' : "Every #{interval} months" + builder.base = IceCube::I18n.t('ice_cube.each_month', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/second_of_minute.rb b/lib/ice_cube/validations/second_of_minute.rb index 6985623c..63b9a818 100644 --- a/lib/ice_cube/validations/second_of_minute.rb +++ b/lib/ice_cube/validations/second_of_minute.rb @@ -43,8 +43,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:second_of_minute) do |segments| - str = "on the #{StringBuilder.sentence(segments)} " - str << (segments.size == 1 ? 'second of the minute' : 'seconds of the minute') + str = StringBuilder.sentence(segments) + IceCube::I18n.t('ice_cube.on_seconds_of_minute', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/secondly_interval.rb b/lib/ice_cube/validations/secondly_interval.rb index 4e79b56e..a8a3d77d 100644 --- a/lib/ice_cube/validations/secondly_interval.rb +++ b/lib/ice_cube/validations/secondly_interval.rb @@ -32,7 +32,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Secondly' : "Every #{interval} seconds" + builder.base = IceCube::I18n.t("ice_cube.each_second", count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/until.rb b/lib/ice_cube/validations/until.rb index d834e431..748f2bdd 100644 --- a/lib/ice_cube/validations/until.rb +++ b/lib/ice_cube/validations/until.rb @@ -38,7 +38,8 @@ def validate(step_time, schedule) end def build_s(builder) - builder.piece(:until) << "until #{time.strftime(IceCube.to_s_time_format)}" + date = IceCube::I18n.l(time, format: IceCube.to_s_time_format) + builder.piece(:until) << IceCube::I18n.t('ice_cube.until', date: date) end def build_hash(builder) diff --git a/lib/ice_cube/validations/weekly_interval.rb b/lib/ice_cube/validations/weekly_interval.rb index 5bdf33be..50e7a6cb 100644 --- a/lib/ice_cube/validations/weekly_interval.rb +++ b/lib/ice_cube/validations/weekly_interval.rb @@ -44,7 +44,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Weekly' : "Every #{interval} weeks" + builder.base = IceCube::I18n.t('ice_cube.each_week', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/yearly_interval.rb b/lib/ice_cube/validations/yearly_interval.rb index 9daab221..3fa01673 100644 --- a/lib/ice_cube/validations/yearly_interval.rb +++ b/lib/ice_cube/validations/yearly_interval.rb @@ -32,7 +32,7 @@ def validate(step_time, schedule) end def build_s(builder) - builder.base = interval == 1 ? 'Yearly' : "Every #{interval} years" + builder.base = IceCube::I18n.t('ice_cube.each_year', count: interval) end def build_hash(builder) diff --git a/spec/examples/occurrence_spec.rb b/spec/examples/occurrence_spec.rb index e46f29ca..7498a476 100644 --- a/spec/examples/occurrence_spec.rb +++ b/spec/examples/occurrence_spec.rb @@ -28,7 +28,7 @@ end it "accepts a format option to comply with ActiveSupport" do - require 'active_support/core_ext/time' + # require 'active_support/core_ext/time' time_now = Time.current occurrence = Occurrence.new(time_now) diff --git a/spec/examples/schedule_spec.rb b/spec/examples/schedule_spec.rb index ea49c951..0f353aaf 100644 --- a/spec/examples/schedule_spec.rb +++ b/spec/examples/schedule_spec.rb @@ -457,8 +457,8 @@ t0 = Time.utc(2013, 5, 18, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily - previous = schedule.previous_occurrence(t0 + 2 * ONE_DAY) - previous.should == t0 + ONE_DAY + previous = schedule.previous_occurrence(t0 + 2 * IceCube::ONE_DAY) + previous.should == t0 + IceCube::ONE_DAY end it 'returns nil given the start time' do @@ -485,16 +485,16 @@ t0 = Time.utc(2013, 5, 18, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily - previous = schedule.previous_occurrences(2, t0 + 3 * ONE_DAY) - previous.should == [t0 + ONE_DAY, t0 + 2 * ONE_DAY] + previous = schedule.previous_occurrences(2, t0 + 3 * IceCube::ONE_DAY) + previous.should == [t0 + IceCube::ONE_DAY, t0 + 2 * IceCube::ONE_DAY] end it 'limits the returned occurrences to a given count' do t0 = Time.utc(2013, 5, 18, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily - previous = schedule.previous_occurrences(999, t0 + 2 * ONE_DAY) - previous.should == [t0, t0 + ONE_DAY] + previous = schedule.previous_occurrences(999, t0 + 2 * IceCube::ONE_DAY) + previous.should == [t0, t0 + IceCube::ONE_DAY] end it 'returns empty array given the start time' do @@ -531,7 +531,7 @@ t1 = Time.utc(2013, 5, 31, 12, 34) schedule = IceCube::Schedule.new(t0) schedule.add_recurrence_rule IceCube::Rule.daily.until(t1 + 1) - schedule.last(2).should == [t1 - ONE_DAY, t1] + schedule.last(2).should == [t1 - IceCube::ONE_DAY, t1] end it 'raises an error for a non-terminating schedule' do diff --git a/spec/examples/to_s_en_spec.rb b/spec/examples/to_s_en_spec.rb new file mode 100644 index 00000000..b6ca73f6 --- /dev/null +++ b/spec/examples/to_s_en_spec.rb @@ -0,0 +1,224 @@ +require File.dirname(__FILE__) + '/../spec_helper' +require 'i18n' +require 'ice_cube/null_i18n' + +describe IceCube::Schedule, 'to_s' do + + shared_examples "to_s in English" do + + it 'should represent its start time by default' do + t0 = Time.local(2013, 2, 14) + IceCube::Schedule.new(t0).to_s.should == 'February 14, 2013' + end + + it 'should have a useful base to_s representation for a secondly rule' do + IceCube::Rule.secondly.to_s.should == 'Secondly' + IceCube::Rule.secondly(2).to_s.should == 'Every 2 seconds' + end + + it 'should have a useful base to_s representation for a minutely rule' do + IceCube::Rule.minutely.to_s.should == 'Minutely' + IceCube::Rule.minutely(2).to_s.should == 'Every 2 minutes' + end + + it 'should have a useful base to_s representation for a hourly rule' do + IceCube::Rule.hourly.to_s.should == 'Hourly' + IceCube::Rule.hourly(2).to_s.should == 'Every 2 hours' + end + + it 'should have a useful base to_s representation for a daily rule' do + IceCube::Rule.daily.to_s.should == 'Daily' + IceCube::Rule.daily(2).to_s.should == 'Every 2 days' + end + + it 'should have a useful base to_s representation for a weekly rule' do + IceCube::Rule.weekly.to_s.should == 'Weekly' + IceCube::Rule.weekly(2).to_s.should == 'Every 2 weeks' + end + + it 'should have a useful base to_s representation for a monthly rule' do + IceCube::Rule.monthly.to_s.should == 'Monthly' + IceCube::Rule.monthly(2).to_s.should == 'Every 2 months' + end + + it 'should have a useful base to_s representation for a yearly rule' do + IceCube::Rule.yearly.to_s.should == 'Yearly' + IceCube::Rule.yearly(2).to_s.should == 'Every 2 years' + end + + it 'should work with various sentence types properly' do + IceCube::Rule.weekly.to_s.should == 'Weekly' + IceCube::Rule.weekly.day(:monday).to_s.should == 'Weekly on Mondays' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'Weekly on Mondays and Tuesdays' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'Weekly on Mondays, Tuesdays, and Wednesdays' + end + + it 'should show saturday and sunday as weekends' do + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'Weekly on Weekends' + end + + it 'should not show saturday and sunday as weekends when other days are present also' do + IceCube::Rule.weekly.day(:sunday, :monday, :saturday).to_s.should == + 'Weekly on Sundays, Mondays, and Saturdays' + end + + it 'should reorganize days to be in order' do + IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == + 'Weekly on Mondays and Tuesdays' + end + + it 'should show weekdays as such' do + IceCube::Rule.weekly.day( + :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Weekly on Weekdays' + end + + it 'should not show weekdays as such when a weekend day is present' do + IceCube::Rule.weekly.day( + :sunday, :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Weekly on Sundays, Mondays, Tuesdays, Wednesdays, Thursdays, and Fridays' + end + + it 'should show start time for an empty schedule' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.to_s.should == "March 20, 2010" + end + + it 'should work with a single date' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == "March 20, 2010" + end + + it 'should work with additional dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 21) + schedule.to_s.should == 'March 20, 2010 / March 21, 2010' + end + + it 'should order dates that are out of order' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 19) + schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + end + + it 'should remove duplicated start time' do + schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 20) + schedule.add_recurrence_time t0 + schedule.to_s.should == 'March 20, 2010' + end + + it 'should remove duplicate rtimes' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 19) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + end + + it 'should work with rules and dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 19) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.to_s.should == 'March 20, 2010 / Weekly' + end + + it 'should work with rules and times and exception times' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_exception_time Time.local(2010, 3, 20) # ignored + schedule.add_exception_time Time.local(2010, 3, 21) + schedule.to_s.should == 'Weekly / not on March 20, 2010 / not on March 21, 2010' + end + + it 'should work with a single rrule' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly.day_of_week(:monday => [1]) + schedule.to_s.should == schedule.rrules[0].to_s + end + + it 'should be able to say the last Thursday of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-1]).to_s + rule_str.should == 'Monthly on the last Thursday' + end + + it 'should be able to say what months of the year something happens' do + rule_str = IceCube::Rule.yearly.month_of_year(:june, :july).to_s + rule_str.should == 'Yearly in June and July' + end + + it 'should be able to say the second to last monday of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-2]).to_s + rule_str.should == 'Monthly on the 2nd to last Thursday' + end + + it 'should join the first and last weekdays of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [1, -1]).to_s + rule_str.should == 'Monthly on the 1st Thursday and last Thursday' + end + + it 'should be able to say the days of the month something happens' do + rule_str = IceCube::Rule.monthly.day_of_month(1, 15, 30).to_s + rule_str.should == 'Monthly on the 1st, 15th, and 30th days of the month' + end + + it 'should be able to say what day of the year something happens' do + rule_str = IceCube::Rule.yearly.day_of_year(30).to_s + rule_str.should == 'Yearly on the 30th day of the year' + end + + it 'should be able to say what hour of the day something happens' do + rule_str = IceCube::Rule.daily.hour_of_day(6, 12).to_s + rule_str.should == 'Daily on the 6th and 12th hours of the day' + end + + it 'should be able to say what minute of an hour something happens - with special suffix minutes' do + rule_str = IceCube::Rule.hourly.minute_of_hour(10, 11, 12, 13, 14, 15).to_s + rule_str.should == 'Hourly on the 10th, 11th, 12th, 13th, 14th, and 15th minutes of the hour' + end + + it 'should be able to say what seconds of the minute something happens' do + rule_str = IceCube::Rule.minutely.second_of_minute(10, 11).to_s + rule_str.should == 'Minutely on the 10th and 11th seconds of the minute' + end + + it 'should be able to reflect until dates' do + schedule = IceCube::Schedule.new(Time.now) + schedule.rrule IceCube::Rule.weekly.until(Time.local(2012, 2, 3)) + schedule.to_s.should == 'Weekly until February 3, 2012' + end + + it 'should be able to reflect count' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(1) + schedule.to_s.should == 'Weekly 1 time' + end + + it 'should be able to reflect count (proper pluralization)' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(2) + schedule.to_s.should == 'Weekly 2 times' + end + + end + + context "without I18n" do + before { IceCube::I18n.stub(:backend) { IceCube::NullI18n } } + + it_behaves_like "to_s in English" + end + + context "with I18n" do + before(:each) { I18n.locale = :en } + + it "uses I18n" do + IceCube::I18n.backend.should == I18n + end + + it_behaves_like "to_s in English" + end + +end diff --git a/spec/examples/to_s_es_spec.rb b/spec/examples/to_s_es_spec.rb new file mode 100644 index 00000000..dadaa87e --- /dev/null +++ b/spec/examples/to_s_es_spec.rb @@ -0,0 +1,209 @@ +# encoding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +describe IceCube::Schedule, 'to_s' do + before :each do + I18n.locale = :es + end + + after :all do + I18n.locale = :en + end + + it 'should represent its start time by default' do + t0 = Time.local(2013, 2, 14) + IceCube::Schedule.new(t0).to_s.should == '14 de Febrero de 2013' + end + + it 'should have a useful base to_s representation for a secondly rule' do + IceCube::Rule.secondly.to_s.should == 'Cada segundo' + IceCube::Rule.secondly(2).to_s.should == 'Cada 2 segundos' + end + + it 'should have a useful base to_s representation for a minutely rule' do + IceCube::Rule.minutely.to_s.should == 'Cada minuto' + IceCube::Rule.minutely(2).to_s.should == 'Cada 2 minutos' + end + + it 'should have a useful base to_s representation for a hourly rule' do + IceCube::Rule.hourly.to_s.should == 'Cada hora' + IceCube::Rule.hourly(2).to_s.should == 'Cada 2 horas' + end + + it 'should have a useful base to_s representation for a daily rule' do + IceCube::Rule.daily.to_s.should == 'Diariamente' + IceCube::Rule.daily(2).to_s.should == 'Cada 2 días' + end + + it 'should have a useful base to_s representation for a weekly rule' do + IceCube::Rule.weekly.to_s.should == 'Semanalmente' + IceCube::Rule.weekly(2).to_s.should == 'Cada 2 semanas' + end + + it 'should have a useful base to_s representation for a monthly rule' do + IceCube::Rule.monthly.to_s.should == 'Mensualmente' + IceCube::Rule.monthly(2).to_s.should == 'Cada 2 meses' + end + + it 'should have a useful base to_s representation for a yearly rule' do + IceCube::Rule.yearly.to_s.should == 'Anualmente' + IceCube::Rule.yearly(2).to_s.should == 'Cada 2 años' + end + + it 'should work with various sentence types properly' do + IceCube::Rule.weekly.to_s.should == 'Semanalmente' + IceCube::Rule.weekly.day(:monday).to_s.should == 'Semanalmente los lunes' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'Semanalmente los lunes y los martes' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'Semanalmente los lunes, los martes y los miércoles' + end + + it 'should show saturday and sunday as weekends' do + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'Semanalmente en fin de semana' + end + + it 'should not show saturday and sunday as weekends when other days are present also' do + IceCube::Rule.weekly.day(:sunday, :monday, :saturday).to_s.should == + 'Semanalmente los domingos, los lunes y los sábados' + end + + it 'should reorganize days to be in order' do + IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == + 'Semanalmente los lunes y los martes' + end + + it 'should show weekdays as such' do + IceCube::Rule.weekly.day( + :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Semanalmente en días laborables' + end + + it 'should not show weekdays as such when a weekend day is present' do + IceCube::Rule.weekly.day( + :sunday, :monday, :tuesday, :wednesday, + :thursday, :friday + ).to_s.should == 'Semanalmente los domingos, los lunes, los martes, los miércoles, los jueves y los viernes' + end + + it 'should work with a single date' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == "20 de Marzo de 2010" + end + + it 'should work with additional dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 21) + schedule.to_s.should == '20 de Marzo de 2010, 21 de Marzo de 2010' + end + + it 'should order dates that are out of order' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 19) + schedule.to_s.should == '19 de Marzo de 2010, 20 de Marzo de 2010' + end + + it 'should remove duplicated start time' do + schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 20) + schedule.add_recurrence_time t0 + schedule.to_s.should == '20 de Marzo de 2010' + end + + it 'should remove duplicate rtimes' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.to_s.should == '20 de Marzo de 2010' + end + + it 'should work with rules and dates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.to_s.should == '20 de Marzo de 2010, Semanalmente' + end + + it 'should work with rules and dates and exdates' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_exception_date Time.local(2010, 3, 20) # ignored + schedule.add_exception_date Time.local(2010, 3, 21) + # TODO: this text should be improved to add sentence connector + schedule.to_s.should == 'Semanalmente, excepto el 20 de Marzo de 2010, excepto el 21 de Marzo de 2010' + end + + it 'should work with a single rrule' do + pending 'remove dependency' + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + schedule.add_recurrence_rule IceCube::Rule.weekly.day_of_week(:monday => [1]) + schedule.to_s.should == schedule.rrules[0].to_s + end + + it 'should be able to say the last monday of the month' do + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-1]).to_s + rule_str.should == 'Mensualmente en el último Jueves' + end + + it 'should be able to say what months of the year something happens' do + rule_str = IceCube::Rule.yearly.month_of_year(:june, :july).to_s + rule_str.should == 'Anualmente en Junio y Julio' + end + + it 'should be able to say the second to last monday of the month' do + pending 'penultimo' + rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-2]).to_s + rule_str.should == 'Mensualmente del segundo al último Jueves del mes' + end + + it 'should be able to say the days of the month something happens' do + rule_str = IceCube::Rule.monthly.day_of_month(1, 15, 30).to_s + rule_str.should == 'Mensualmente en los días 1º, 15º y 30º del mes' + end + + it 'should be able to say what day of the year something happens' do + rule_str = IceCube::Rule.yearly.day_of_year(30).to_s + rule_str.should == 'Anualmente en el día 30º' + end + + it 'should be able to say what hour of the day something happens' do + rule_str = IceCube::Rule.daily.hour_of_day(6, 12).to_s + rule_str.should == 'Diariamente en las horas 6º y 12º' + end + + it 'should be able to say what minute of an hour something happens - with special suffix minutes' do + rule_str = IceCube::Rule.hourly.minute_of_hour(10, 11, 12, 13, 14, 15).to_s + rule_str.should == 'Cada hora en los minutos 10º, 11º, 12º, 13º, 14º y 15º' + end + + it 'should be able to say what seconds of the minute something happens' do + rule_str = IceCube::Rule.minutely.second_of_minute(10, 11).to_s + rule_str.should == 'Cada minuto en los segundos 10º y 11º del minuto' + end + + it 'should be able to reflect until dates' do + schedule = IceCube::Schedule.new(Time.now) + schedule.rrule IceCube::Rule.weekly.until(Time.local(2012, 2, 3)) + schedule.to_s.should == 'Semanalmente hasta el 3 de Febrero de 2012' + end + + it 'should be able to reflect count' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(1) + schedule.to_s.should == 'Semanalmente 1 vez' + end + + it 'should be able to reflect count (proper pluralization)' do + schedule = IceCube::Schedule.new(Time.now) + schedule.add_recurrence_rule IceCube::Rule.weekly.count(2) + schedule.to_s.should == 'Semanalmente 2 veces' + end + + # it 'should work when an end_time is set' do + # schedule = IceCube::Schedule.new(Time.local(2012, 8, 31), :end_time => Time.local(2012, 10, 31)) + # schedule.add_recurrence_rule IceCube::Rule.daily.count(2) + # schedule.to_s.should == 'Diariamente 2 veces, hasta el 31 de Octubre 2012' + # end + +end diff --git a/spec/examples/to_s_spec.rb b/spec/examples/to_s_ja_spec.rb similarity index 68% rename from spec/examples/to_s_spec.rb rename to spec/examples/to_s_ja_spec.rb index fef6e531..011213e3 100644 --- a/spec/examples/to_s_spec.rb +++ b/spec/examples/to_s_ja_spec.rb @@ -1,124 +1,133 @@ +# encoding: utf-8 require File.dirname(__FILE__) + '/../spec_helper' describe IceCube::Schedule, 'to_s' do + before :each do + I18n.locale = :ja + end + + after :all do + I18n.locale = :en + end + it 'should represent its start time by default' do t0 = Time.local(2013, 2, 14) - IceCube::Schedule.new(t0).to_s.should == 'February 14, 2013' + IceCube::Schedule.new(t0).to_s.should == '2013年02月14日' end it 'should have a useful base to_s representation for a secondly rule' do - IceCube::Rule.secondly.to_s.should == 'Secondly' - IceCube::Rule.secondly(2).to_s.should == 'Every 2 seconds' + IceCube::Rule.secondly.to_s.should == '毎秒' + IceCube::Rule.secondly(2).to_s.should == '2秒ごと' end it 'should have a useful base to_s representation for a minutely rule' do - IceCube::Rule.minutely.to_s.should == 'Minutely' - IceCube::Rule.minutely(2).to_s.should == 'Every 2 minutes' + IceCube::Rule.minutely.to_s.should == '毎分' + IceCube::Rule.minutely(2).to_s.should == '2分ごと' end it 'should have a useful base to_s representation for a hourly rule' do - IceCube::Rule.hourly.to_s.should == 'Hourly' - IceCube::Rule.hourly(2).to_s.should == 'Every 2 hours' + IceCube::Rule.hourly.to_s.should == '毎時間' + IceCube::Rule.hourly(2).to_s.should == '2時間ごと' end it 'should have a useful base to_s representation for a daily rule' do - IceCube::Rule.daily.to_s.should == 'Daily' - IceCube::Rule.daily(2).to_s.should == 'Every 2 days' + IceCube::Rule.daily.to_s.should == '毎日' + IceCube::Rule.daily(2).to_s.should == '2日ごと' end it 'should have a useful base to_s representation for a weekly rule' do - IceCube::Rule.weekly.to_s.should == 'Weekly' - IceCube::Rule.weekly(2).to_s.should == 'Every 2 weeks' + IceCube::Rule.weekly.to_s.should == '毎週' + IceCube::Rule.weekly(2).to_s.should == '2週間ごと' end it 'should have a useful base to_s representation for a monthly rule' do - IceCube::Rule.monthly.to_s.should == 'Monthly' - IceCube::Rule.monthly(2).to_s.should == 'Every 2 months' + IceCube::Rule.monthly.to_s.should == '毎月' + IceCube::Rule.monthly(2).to_s.should == '2ヶ月ごと' end it 'should have a useful base to_s representation for a yearly rule' do - IceCube::Rule.yearly.to_s.should == 'Yearly' - IceCube::Rule.yearly(2).to_s.should == 'Every 2 years' + IceCube::Rule.yearly.to_s.should == '毎年' + IceCube::Rule.yearly(2).to_s.should == '2年ごと' end it 'should work with various sentence types properly' do - IceCube::Rule.weekly.to_s.should == 'Weekly' - IceCube::Rule.weekly.day(:monday).to_s.should == 'Weekly on Mondays' - IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'Weekly on Mondays and Tuesdays' - IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'Weekly on Mondays, Tuesdays, and Wednesdays' + IceCube::Rule.weekly.to_s.should == '毎週' + IceCube::Rule.weekly.day(:monday).to_s.should == '毎週月曜' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == '毎週月曜、火曜' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == '毎週月曜、火曜、水曜' end it 'should show saturday and sunday as weekends' do - IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'Weekly on Weekends' + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == '毎週週末' end it 'should not show saturday and sunday as weekends when other days are present also' do IceCube::Rule.weekly.day(:sunday, :monday, :saturday).to_s.should == - 'Weekly on Sundays, Mondays, and Saturdays' + '毎週日曜、月曜、土曜' end it 'should reorganize days to be in order' do IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == - 'Weekly on Mondays and Tuesdays' + '毎週月曜、火曜' end it 'should show weekdays as such' do IceCube::Rule.weekly.day( :monday, :tuesday, :wednesday, :thursday, :friday - ).to_s.should == 'Weekly on Weekdays' + ).to_s.should == '毎週平日' end it 'should not show weekdays as such when a weekend day is present' do IceCube::Rule.weekly.day( :sunday, :monday, :tuesday, :wednesday, :thursday, :friday - ).to_s.should == 'Weekly on Sundays, Mondays, Tuesdays, Wednesdays, Thursdays, and Fridays' + ).to_s.should == '毎週日曜、月曜、火曜、水曜、木曜、金曜' end it 'should show start time for an empty schedule' do schedule = IceCube::Schedule.new Time.local(2010, 3, 20) - schedule.to_s.should == "March 20, 2010" + schedule.to_s.should == "2010年03月20日" end it 'should work with a single date' do schedule = IceCube::Schedule.new Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 20) - schedule.to_s.should == "March 20, 2010" + schedule.to_s.should == "2010年03月20日" end it 'should work with additional dates' do schedule = IceCube::Schedule.new Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 21) - schedule.to_s.should == 'March 20, 2010 / March 21, 2010' + schedule.to_s.should == '2010年03月20日 / 2010年03月21日' end it 'should order dates that are out of order' do schedule = IceCube::Schedule.new(t0 = Time.local(2010, 3, 20)) schedule.add_recurrence_time t1 = Time.local(2010, 3, 19) - schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + schedule.to_s.should == '2010年03月19日 / 2010年03月20日' end it 'should remove duplicated start time' do schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 20) schedule.add_recurrence_time t0 - schedule.to_s.should == 'March 20, 2010' + schedule.to_s.should == '2010年03月20日' end it 'should remove duplicate rtimes' do schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 19) schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_recurrence_time Time.local(2010, 3, 20) - schedule.to_s.should == 'March 19, 2010 / March 20, 2010' + schedule.to_s.should == '2010年03月19日 / 2010年03月20日' end it 'should work with rules and dates' do schedule = IceCube::Schedule.new Time.local(2010, 3, 19) schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_recurrence_rule IceCube::Rule.weekly - schedule.to_s.should == 'March 20, 2010 / Weekly' + schedule.to_s.should == '2010年03月20日 / 毎週' end it 'should work with rules and times and exception times' do @@ -127,7 +136,7 @@ schedule.add_recurrence_time Time.local(2010, 3, 20) schedule.add_exception_time Time.local(2010, 3, 20) # ignored schedule.add_exception_time Time.local(2010, 3, 21) - schedule.to_s.should == 'Weekly / not on March 20, 2010 / not on March 21, 2010' + schedule.to_s.should == '毎週 / 2010年03月20日以外 / 2010年03月21日以外' end it 'should work with a single rrule' do @@ -138,65 +147,65 @@ it 'should be able to say the last Thursday of the month' do rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-1]).to_s - rule_str.should == 'Monthly on the last Thursday' + rule_str.should == '毎月最終木曜' end it 'should be able to say what months of the year something happens' do rule_str = IceCube::Rule.yearly.month_of_year(:june, :july).to_s - rule_str.should == 'Yearly in June and July' + rule_str.should == '毎年6月、7月' end it 'should be able to say the second to last monday of the month' do rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [-2]).to_s - rule_str.should == 'Monthly on the 2nd to last Thursday' + rule_str.should == '毎月最後から2番目の木曜' end it 'should join the first and last weekdays of the month' do rule_str = IceCube::Rule.monthly.day_of_week(:thursday => [1, -1]).to_s - rule_str.should == 'Monthly on the 1st Thursday and last Thursday' + rule_str.should == '毎月1木曜、最終木曜' end it 'should be able to say the days of the month something happens' do rule_str = IceCube::Rule.monthly.day_of_month(1, 15, 30).to_s - rule_str.should == 'Monthly on the 1st, 15th, and 30th days of the month' + rule_str.should == '毎月1、15、30日' end it 'should be able to say what day of the year something happens' do rule_str = IceCube::Rule.yearly.day_of_year(30).to_s - rule_str.should == 'Yearly on the 30th day of the year' + rule_str.should == '毎年30日' end it 'should be able to say what hour of the day something happens' do rule_str = IceCube::Rule.daily.hour_of_day(6, 12).to_s - rule_str.should == 'Daily on the 6th and 12th hours of the day' + rule_str.should == '毎日6、12時' end it 'should be able to say what minute of an hour something happens - with special suffix minutes' do rule_str = IceCube::Rule.hourly.minute_of_hour(10, 11, 12, 13, 14, 15).to_s - rule_str.should == 'Hourly on the 10th, 11th, 12th, 13th, 14th, and 15th minutes of the hour' + rule_str.should == '毎時間10、11、12、13、14、15分' end it 'should be able to say what seconds of the minute something happens' do rule_str = IceCube::Rule.minutely.second_of_minute(10, 11).to_s - rule_str.should == 'Minutely on the 10th and 11th seconds of the minute' + rule_str.should == '毎分10、11秒' end it 'should be able to reflect until dates' do schedule = IceCube::Schedule.new(Time.now) schedule.rrule IceCube::Rule.weekly.until(Time.local(2012, 2, 3)) - schedule.to_s.should == 'Weekly until February 3, 2012' + schedule.to_s.should == '2012年02月03日まで毎週' end it 'should be able to reflect count' do schedule = IceCube::Schedule.new(Time.now) schedule.add_recurrence_rule IceCube::Rule.weekly.count(1) - schedule.to_s.should == 'Weekly 1 time' + schedule.to_s.should == '毎週1回' end it 'should be able to reflect count (proper pluralization)' do schedule = IceCube::Schedule.new(Time.now) schedule.add_recurrence_rule IceCube::Rule.weekly.count(2) - schedule.to_s.should == 'Weekly 2 times' + schedule.to_s.should == '毎週2回' end end