From db3729df18bea29646559c79611e65b97c263e85 Mon Sep 17 00:00:00 2001 From: Tomo Matsumoto Date: Sun, 5 Oct 2014 23:13:50 +0900 Subject: [PATCH 1/8] use I18n --- config/locales/en.yml | 52 ++++++++++++++++++++ ice_cube.gemspec | 1 + lib/ice_cube.rb | 3 ++ lib/ice_cube/builders/string_builder.rb | 26 ++++++---- lib/ice_cube/validations/count.rb | 2 +- lib/ice_cube/validations/day.rb | 9 ++-- lib/ice_cube/validations/day_of_month.rb | 6 +-- lib/ice_cube/validations/day_of_week.rb | 3 +- lib/ice_cube/validations/day_of_year.rb | 6 +-- lib/ice_cube/validations/hour_of_day.rb | 4 +- lib/ice_cube/validations/minute_of_hour.rb | 4 +- lib/ice_cube/validations/month_of_year.rb | 2 +- lib/ice_cube/validations/second_of_minute.rb | 4 +- spec/examples/to_s_spec.rb | 4 ++ 14 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 config/locales/en.yml diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 00000000..315cd568 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,52 @@ +en: + ice_cube: + times: + other: '%{count} times' + one: '%{count} time' + 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 + 'on': on the %{sentence} + in: in + integer: + negative: '%{ordinal} to last' + literal_ordinals: + -1: last + -2: 2nd to last + 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} + support: + array: + two_words_connector: ' and ' 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 623ba62c..d36f2dfe 100644 --- a/lib/ice_cube.rb +++ b/lib/ice_cube.rb @@ -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 diff --git a/lib/ice_cube/builders/string_builder.rb b/lib/ice_cube/builders/string_builder.rb index de4585d7..65fbe702 100644 --- a/lib/ice_cube/builders/string_builder.rb +++ b/lib/ice_cube/builders/string_builder.rb @@ -34,9 +34,6 @@ 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 @@ -48,13 +45,22 @@ def sentence(array) 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) + "#{number}#{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 diff --git a/lib/ice_cube/validations/count.rb b/lib/ice_cube/validations/count.rb index 61099433..b35e9729 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'}" + I18n.t('ice_cube.times', count: count) end end diff --git a/lib/ice_cube/validations/day.rb b/lib/ice_cube/validations/day.rb index c29396f0..1fbef4bc 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' + 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 diff --git a/lib/ice_cube/validations/day_of_month.rb b/lib/ice_cube/validations/day_of_month.rb index ef2cd2ad..cca1e7ae 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 = I18n.t('ice_cube.days_of_month', count: entries.size, segments: sentence) + 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..637b6b8b 100644 --- a/lib/ice_cube/validations/day_of_week.rb +++ b/lib/ice_cube/validations/day_of_week.rb @@ -62,7 +62,8 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_week) do |segments| - 'on the ' + segments.join(' and ') + sentence = segments.join(I18n.t('support.array.two_words_connector')) + 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..120a565a 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 = I18n.t('ice_cube.days_of_year', count: entries.size, segments: str) + 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..877e4eeb 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) + I18n.t('ice_cube.at_hours_of_the_day', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/minute_of_hour.rb b/lib/ice_cube/validations/minute_of_hour.rb index bf1e26f9..d4fbf14a 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) + I18n.t('ice_cube.on_minutes_of_hour', count: segments.size, segments: str) end end diff --git a/lib/ice_cube/validations/month_of_year.rb b/lib/ice_cube/validations/month_of_year.rb index 5c9c0f98..1d1c0d65 100644 --- a/lib/ice_cube/validations/month_of_year.rb +++ b/lib/ice_cube/validations/month_of_year.rb @@ -44,7 +44,7 @@ def build_ical(builder) end StringBuilder.register_formatter(:month_of_year) do |segments| - "in #{StringBuilder.sentence(segments)}" + "#{I18n.t("ice_cube.in")} #{StringBuilder.sentence(segments)}" end end diff --git a/lib/ice_cube/validations/second_of_minute.rb b/lib/ice_cube/validations/second_of_minute.rb index 6985623c..0ad30198 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) + I18n.t('ice_cube.on_seconds_of_minute', count: segments.size, segments: str) end end diff --git a/spec/examples/to_s_spec.rb b/spec/examples/to_s_spec.rb index fef6e531..e74785b6 100644 --- a/spec/examples/to_s_spec.rb +++ b/spec/examples/to_s_spec.rb @@ -2,6 +2,10 @@ describe IceCube::Schedule, 'to_s' do + before :each 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' From 77f45ea1af8c0bb2215282840df21d9a718cf813 Mon Sep 17 00:00:00 2001 From: Tomo Matsumoto Date: Mon, 6 Oct 2014 23:56:24 +0900 Subject: [PATCH 2/8] add Japanese locale --- config/locales/en.yml | 68 +++++- config/locales/ja.yml | 107 +++++++++ lib/ice_cube.rb | 2 +- lib/ice_cube/builders/string_builder.rb | 16 +- lib/ice_cube/schedule.rb | 8 +- lib/ice_cube/validations/daily_interval.rb | 2 +- lib/ice_cube/validations/day_of_week.rb | 8 +- lib/ice_cube/validations/hourly_interval.rb | 2 +- lib/ice_cube/validations/minutely_interval.rb | 2 +- lib/ice_cube/validations/month_of_year.rb | 4 +- lib/ice_cube/validations/monthly_interval.rb | 2 +- lib/ice_cube/validations/secondly_interval.rb | 2 +- lib/ice_cube/validations/until.rb | 3 +- lib/ice_cube/validations/weekly_interval.rb | 2 +- lib/ice_cube/validations/yearly_interval.rb | 2 +- .../{to_s_spec.rb => to_s_en_spec.rb} | 2 +- spec/examples/to_s_ja_spec.rb | 207 ++++++++++++++++++ 17 files changed, 414 insertions(+), 25 deletions(-) create mode 100644 config/locales/ja.yml rename spec/examples/{to_s_spec.rb => to_s_en_spec.rb} (99%) create mode 100644 spec/examples/to_s_ja_spec.rb diff --git a/config/locales/en.yml b/config/locales/en.yml index 315cd568..3bc1d465 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,8 +1,38 @@ 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' @@ -21,13 +51,35 @@ en: 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 + in: 'in %{target}' integer: negative: '%{ordinal} to last' literal_ordinals: -1: last -2: 2nd to last + ordinal: '%{number}%{ordinal}' ordinals: default: th 1: st @@ -47,6 +99,18 @@ en: - Fridays - Saturdays on_days: on %{days} - support: 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}' 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/lib/ice_cube.rb b/lib/ice_cube.rb index d36f2dfe..88f8a98a 100644 --- a/lib/ice_cube.rb +++ b/lib/ice_cube.rb @@ -71,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. diff --git a/lib/ice_cube/builders/string_builder.rb b/lib/ice_cube/builders/string_builder.rb index 65fbe702..7809f6bf 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 = 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) @@ -39,8 +43,8 @@ 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 @@ -49,7 +53,7 @@ def nice_number(number) end def ordinalize(number) - "#{number}#{ordinal(number)}" + I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number)) end def literal_ordinal(number) diff --git a/lib/ice_cube/schedule.rb b/lib/ice_cube/schedule.rb index c6a8e100..9cbd1525 100644 --- a/lib/ice_cube/schedule.rb +++ b/lib/ice_cube/schedule.rb @@ -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 diff --git a/lib/ice_cube/validations/daily_interval.rb b/lib/ice_cube/validations/daily_interval.rb index edf8ce72..f75d207a 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 = I18n.t('ice_cube.each_day', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/day_of_week.rb b/lib/ice_cube/validations/day_of_week.rb index 637b6b8b..a9ad49f9 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) << I18n.t( + 'ice_cube.days_of_week', + segments: StringBuilder.nice_number(occ), + day: I18n.t('ice_cube.date.day_names')[day] + ) end def build_hash(builder) @@ -62,7 +66,7 @@ def build_ical(builder) end StringBuilder.register_formatter(:day_of_week) do |segments| - sentence = segments.join(I18n.t('support.array.two_words_connector')) + sentence = segments.join(I18n.t('ice_cube.array.two_words_connector')) I18n.t('ice_cube.on', sentence: sentence) end diff --git a/lib/ice_cube/validations/hourly_interval.rb b/lib/ice_cube/validations/hourly_interval.rb index ce2484c4..1b526953 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 = I18n.t("ice_cube.each_hour", count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/minutely_interval.rb b/lib/ice_cube/validations/minutely_interval.rb index eb03f366..1363a573 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 = 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 1d1c0d65..4628c386 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) << 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| - "#{I18n.t("ice_cube.in")} #{StringBuilder.sentence(segments)}" + 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..b5d75b4b 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 = I18n.t('ice_cube.each_month', count: interval) end def build_hash(builder) diff --git a/lib/ice_cube/validations/secondly_interval.rb b/lib/ice_cube/validations/secondly_interval.rb index 4e79b56e..37d018df 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 = 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..56a264b6 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 = time.strftime(IceCube.to_s_time_format) + builder.piece(:until) << 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..73a9d3d4 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 = 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..09027368 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 = I18n.t('ice_cube.each_year', count: interval) end def build_hash(builder) diff --git a/spec/examples/to_s_spec.rb b/spec/examples/to_s_en_spec.rb similarity index 99% rename from spec/examples/to_s_spec.rb rename to spec/examples/to_s_en_spec.rb index e74785b6..d41193ce 100644 --- a/spec/examples/to_s_spec.rb +++ b/spec/examples/to_s_en_spec.rb @@ -188,7 +188,7 @@ 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 == 'Weekly until February 3, 2012' end it 'should be able to reflect count' do diff --git a/spec/examples/to_s_ja_spec.rb b/spec/examples/to_s_ja_spec.rb new file mode 100644 index 00000000..6e79b27d --- /dev/null +++ b/spec/examples/to_s_ja_spec.rb @@ -0,0 +1,207 @@ +# -*- coding: utf-8 -*- +require File.dirname(__FILE__) + '/../spec_helper' + +describe IceCube::Schedule, 'to_s' do + + before :each do + I18n.locale = :ja + end + + it 'should represent its start time by default' do + t0 = Time.local(2013, 2, 14) + 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 == '毎秒' + 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 == '毎分' + 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 == '毎時間' + 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 == '毎日' + 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 == '毎週' + 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 == '毎月' + 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 == '毎年' + IceCube::Rule.yearly(2).to_s.should == '2年ごと' + end + + it 'should work with various sentence types properly' do + 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 == '毎週週末' + 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 == + '毎週日曜、月曜、土曜' + end + + it 'should reorganize days to be in order' do + IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == + '毎週月曜、火曜' + end + + it 'should show weekdays as such' do + IceCube::Rule.weekly.day( + :monday, :tuesday, :wednesday, + :thursday, :friday + ).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 == '毎週日曜、月曜、火曜、水曜、木曜、金曜' + end + + it 'should show start time for an empty schedule' do + schedule = IceCube::Schedule.new Time.local(2010, 3, 20) + 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 == "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 == '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 == '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 == '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 == '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 == '2010年03月20日 / 毎週' + 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 == '毎週 / 2010年03月20日以外 / 2010年03月21日以外' + 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 == '毎月最終木曜' + 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 == '毎年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 == '毎月最後から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 == '毎月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 == '毎月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 == '毎年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 == '毎日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 == '毎時間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 == '毎分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 == '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 == '毎週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 == '毎週2回' + end + +end From 9be09f725062594e711dfa4a4608c39331ce7457 Mon Sep 17 00:00:00 2001 From: Tomo Matsumoto Date: Fri, 7 Nov 2014 08:56:52 +0900 Subject: [PATCH 3/8] delete `s.add_development_dependency('i18n')` and add `gem 'i18n'` in Gemfile --- Gemfile | 2 ++ ice_cube.gemspec | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index fa75df15..40fb1292 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org' gemspec + +gem 'i18n' \ No newline at end of file diff --git a/ice_cube.gemspec b/ice_cube.gemspec index 6f89238d..9854163d 100644 --- a/ice_cube.gemspec +++ b/ice_cube.gemspec @@ -21,5 +21,4 @@ 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 From e09e2a43c506ef89a6133e9ac00c4b999863eba6 Mon Sep 17 00:00:00 2001 From: Tomo Matsumoto Date: Sat, 8 Nov 2014 10:35:21 +0900 Subject: [PATCH 4/8] s.add_runtime_dependency('i18n') --- Gemfile | 4 +--- ice_cube.gemspec | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 40fb1292..cd8aa9e0 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,3 @@ source 'https://rubygems.org' -gemspec - -gem 'i18n' \ No newline at end of file +gemspec \ No newline at end of file diff --git a/ice_cube.gemspec b/ice_cube.gemspec index 9854163d..6f41e090 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_runtime_dependency('i18n') end From 4e61edb89e4b6342568d2c052381a45b916b18e3 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 9 Dec 2014 11:14:51 +0100 Subject: [PATCH 5/8] un-memoize IceCube.to_s_time_format to allow changing locale in runtime; fix tests --- lib/ice_cube.rb | 2 +- spec/examples/occurrence_spec.rb | 2 +- spec/examples/schedule_spec.rb | 14 +++++++------- spec/examples/to_s_ja_spec.rb | 5 ++++- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/ice_cube.rb b/lib/ice_cube.rb index 88f8a98a..645209ff 100644 --- a/lib/ice_cube.rb +++ b/lib/ice_cube.rb @@ -71,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 ||= I18n.t("ice_cube.date.formats.default") + I18n.t("ice_cube.date.formats.default") end # Sets the format used by IceCube when printing out Schedule#to_s. 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_ja_spec.rb b/spec/examples/to_s_ja_spec.rb index 6e79b27d..57139a7b 100644 --- a/spec/examples/to_s_ja_spec.rb +++ b/spec/examples/to_s_ja_spec.rb @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- require File.dirname(__FILE__) + '/../spec_helper' describe IceCube::Schedule, 'to_s' do @@ -7,6 +6,10 @@ 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 == '2013年02月14日' From 0cb27c13e65a5c3d3d12082260a0dea885a38dcf Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 9 Dec 2014 11:37:48 +0100 Subject: [PATCH 6/8] add encoding magic comment to ja_spec --- spec/examples/to_s_ja_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/examples/to_s_ja_spec.rb b/spec/examples/to_s_ja_spec.rb index 57139a7b..011213e3 100644 --- a/spec/examples/to_s_ja_spec.rb +++ b/spec/examples/to_s_ja_spec.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require File.dirname(__FILE__) + '/../spec_helper' describe IceCube::Schedule, 'to_s' do From 6ab5b60dac88b24ba6b5eb8271a2e2b243e4936a Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 9 Dec 2014 11:45:37 +0100 Subject: [PATCH 7/8] adding spanish (es) locale and test --- spec/examples/to_s_es_spec.rb | 203 ++++++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 spec/examples/to_s_es_spec.rb diff --git a/spec/examples/to_s_es_spec.rb b/spec/examples/to_s_es_spec.rb new file mode 100644 index 00000000..0af717f4 --- /dev/null +++ b/spec/examples/to_s_es_spec.rb @@ -0,0 +1,203 @@ +# 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/02/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 el Lunes' + IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'semanalmente el Lunes y el Martes' + IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'semanalmente el Lunes, el Martes y el Miércoles' + end + + it 'should show saturday and sunday as weekends' do + IceCube::Rule.weekly.day(:saturday, :sunday).to_s.should == 'semanalmente el 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 el Domingo, el Lunes y el Sábado' + end + + it 'should reorganize days to be in order' do + IceCube::Rule.weekly.day(:tuesday, :monday).to_s.should == + 'semanalmente el Lunes y el 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 el Lunes, el Martes, el Miércoles, el Jueves, el Viernes y el Domingo' + 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 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 2010, 21 de Marzo 2010' + end + + it 'should order dates that are out of order' do + schedule = IceCube::Schedule.new Time.now + schedule.add_recurrence_time Time.local(2010, 3, 20) + schedule.add_recurrence_time Time.local(2010, 3, 19) + schedule.to_s.should == '19 de Marzo 2010, 20 de Marzo 2010' + end + + it 'should remove duplicate rdates' do + schedule = IceCube::Schedule.new Time.now + 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 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 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) + schedule.to_s.should == 'semanalmente, excepto el 20 de Marzo 2010 y 21 de Marzo 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' + 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 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 From 4926bf972f7c24c280e652dbac79348e5b9b9744 Mon Sep 17 00:00:00 2001 From: David Gil Date: Tue, 9 Dec 2014 12:48:49 +0100 Subject: [PATCH 8/8] change strftime into I18n.l accross the code and add spanish ES locale and test --- config/locales/es.yml | 176 ++++++++++++++++++++++++++ lib/ice_cube/builders/ical_builder.rb | 6 +- lib/ice_cube/schedule.rb | 5 +- lib/ice_cube/validations/until.rb | 2 +- spec/examples/to_s_en_spec.rb | 6 +- spec/examples/to_s_es_spec.rb | 106 ++++++++-------- 6 files changed, 242 insertions(+), 59 deletions(-) create mode 100644 config/locales/es.yml 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/lib/ice_cube/builders/ical_builder.rb b/lib/ice_cube/builders/ical_builder.rb index d6592a6a..67e87cd8 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 + 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 + ":#{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=#{I18n.l(time, format: '%Z:%Y%m%dT%H%M%S')}" # local time specified end end diff --git a/lib/ice_cube/schedule.rb b/lib/ice_cube/schedule.rb index 9cbd1525..b2e1d74d 100644 --- a/lib/ice_cube/schedule.rb +++ b/lib/ice_cube/schedule.rb @@ -313,11 +313,12 @@ 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| I18n.l(t, format: IceCube.to_s_time_format) } pieces.concat rrules.map { |t| t.to_s } 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)) + target = I18n.l(t, format: IceCube.to_s_time_format) + I18n.t('ice_cube.not_on', target: target) } pieces.join(I18n.t('ice_cube.pieces_connector')) end diff --git a/lib/ice_cube/validations/until.rb b/lib/ice_cube/validations/until.rb index 56a264b6..8771734f 100644 --- a/lib/ice_cube/validations/until.rb +++ b/lib/ice_cube/validations/until.rb @@ -38,7 +38,7 @@ def validate(step_time, schedule) end def build_s(builder) - date = time.strftime(IceCube.to_s_time_format) + date = I18n.l(time, format: IceCube.to_s_time_format) builder.piece(:until) << I18n.t('ice_cube.until', date: date) end diff --git a/spec/examples/to_s_en_spec.rb b/spec/examples/to_s_en_spec.rb index d41193ce..148b969f 100644 --- a/spec/examples/to_s_en_spec.rb +++ b/spec/examples/to_s_en_spec.rb @@ -100,8 +100,8 @@ 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 = 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 @@ -112,7 +112,7 @@ end it 'should remove duplicate rtimes' do - schedule = IceCube::Schedule.new t0 = Time.local(2010, 3, 19) + 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' diff --git a/spec/examples/to_s_es_spec.rb b/spec/examples/to_s_es_spec.rb index 0af717f4..dadaa87e 100644 --- a/spec/examples/to_s_es_spec.rb +++ b/spec/examples/to_s_es_spec.rb @@ -12,111 +12,116 @@ it 'should represent its start time by default' do t0 = Time.local(2013, 2, 14) - IceCube::Schedule.new(t0).to_s.should == '14/02/2013' + 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' + 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' + 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' + 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' + 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' + 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' + 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' + 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 el Lunes' - IceCube::Rule.weekly.day(:monday, :tuesday).to_s.should == 'semanalmente el Lunes y el Martes' - IceCube::Rule.weekly.day(:monday, :tuesday, :wednesday).to_s.should == 'semanalmente el Lunes, el Martes y el Miércoles' + 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 el fin de semana' + 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 el Domingo, el Lunes y el Sábado' + '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 el Lunes y el Martes' + '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' + ).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 el Lunes, el Martes, el Miércoles, el Jueves, el Viernes y el Domingo' + ).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 2010" + 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 2010, 21 de Marzo 2010' + 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.now - schedule.add_recurrence_time Time.local(2010, 3, 20) + 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 2010, 20 de Marzo 2010' + 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 rdates' do - schedule = IceCube::Schedule.new Time.now + 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 2010' + 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 2010, semanalmente' + schedule.to_s.should == '20 de Marzo de 2010, Semanalmente' end it 'should work with rules and dates and exdates' do @@ -125,7 +130,8 @@ 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) - schedule.to_s.should == 'semanalmente, excepto el 20 de Marzo 2010 y 21 de Marzo 2010' + # 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 @@ -137,67 +143,67 @@ 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' + 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' + 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' + 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' + 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' + 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' + 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' + 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' + 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 2012' + 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' + 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' + 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 + # 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