From db3729df18bea29646559c79611e65b97c263e85 Mon Sep 17 00:00:00 2001 From: Tomo Matsumoto Date: Sun, 5 Oct 2014 23:13:50 +0900 Subject: [PATCH 1/4] 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/4] 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/4] 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/4] 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