Skip to content

Commit 5c6cf58

Browse files
isaacseymourgreysteil
authored andcommitted
Add NullI18n and IceCube::I18n to remove runtime dependency
1 parent c2c4626 commit 5c6cf58

26 files changed

+380
-248
lines changed

config/locales/en.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,65 @@ en:
114114
until: '%{rest} %{current}'
115115
count: '%{rest} %{current}'
116116
default: '%{rest} %{current}'
117+
118+
date:
119+
abbr_day_names:
120+
- Sun
121+
- Mon
122+
- Tue
123+
- Wed
124+
- Thu
125+
- Fri
126+
- Sat
127+
abbr_month_names:
128+
-
129+
- Jan
130+
- Feb
131+
- Mar
132+
- Apr
133+
- May
134+
- Jun
135+
- Jul
136+
- Aug
137+
- Sep
138+
- Oct
139+
- Nov
140+
- Dec
141+
day_names:
142+
- Sunday
143+
- Monday
144+
- Tuesday
145+
- Wednesday
146+
- Thursday
147+
- Friday
148+
- Saturday
149+
formats:
150+
default: "%Y-%m-%d"
151+
long: "%B %d, %Y"
152+
short: "%b %d"
153+
month_names:
154+
-
155+
- January
156+
- February
157+
- March
158+
- April
159+
- May
160+
- June
161+
- July
162+
- August
163+
- September
164+
- October
165+
- November
166+
- December
167+
order:
168+
- :year
169+
- :month
170+
- :day
171+
172+
time:
173+
am: am
174+
formats:
175+
default: "%a, %d %b %Y %H:%M:%S %z"
176+
long: "%B %d, %Y %H:%M"
177+
short: "%d %b %H:%M"
178+
pm: pm

ice_cube.gemspec

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

lib/ice_cube.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'date'
22
require 'ice_cube/deprecated'
3-
require 'i18n'
3+
require 'ice_cube/i18n'
44

5-
I18n.load_path += Dir[File.expand_path('../../config/locales/*{rb,yml}', __FILE__)]
5+
IceCube::I18n.detect_backend!
66

77
module IceCube
88

@@ -72,7 +72,7 @@ module Validations
7272
# Defines the format used by IceCube when printing out Schedule#to_s.
7373
# Defaults to '%B %e, %Y'
7474
def self.to_s_time_format
75-
I18n.t("ice_cube.date.formats.default")
75+
IceCube::I18n.t("ice_cube.date.formats.default")
7676
end
7777

7878
# Sets the format used by IceCube when printing out Schedule#to_s.

lib/ice_cube/builders/ical_builder.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ def to_s
3232

3333
def self.ical_utc_format(time)
3434
time = time.dup.utc
35-
I18n.l(time, format: '%Y%m%dT%H%M%SZ') # utc time
35+
IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ') # utc time
3636
end
3737

3838
def self.ical_format(time, force_utc)
3939
time = time.dup.utc if force_utc
4040
if time.utc?
41-
":#{I18n.l(time, format: '%Y%m%dT%H%M%SZ')}" # utc time
41+
":#{IceCube::I18n.l(time, format: '%Y%m%dT%H%M%SZ')}" # utc time
4242
else
43-
";TZID=#{I18n.l(time, format: '%Z:%Y%m%dT%H%M%S')}" # local time specified
43+
";TZID=#{IceCube::I18n.l(time, format: '%Z:%Y%m%dT%H%M%S')}" # local time specified
4444
end
4545
end
4646

lib/ice_cube/builders/string_builder.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def to_s
2121
next if segments.empty?
2222
current = self.class.sentence(segments)
2323
end
24-
f = I18n.t('ice_cube.string.format')[type] ? type : 'default'
25-
string = I18n.t("ice_cube.string.format.#{f}", rest: string, current: current)
24+
f = IceCube::I18n.t('ice_cube.string.format')[type] ? type : 'default'
25+
string = IceCube::I18n.t("ice_cube.string.format.#{f}", rest: string, current: current)
2626
end
2727
string
2828
end
@@ -43,8 +43,8 @@ def sentence(array)
4343
case array.length
4444
when 0 ; ''
4545
when 1 ; array[0].to_s
46-
when 2 ; "#{array[0]}#{I18n.t('ice_cube.array.two_words_connector')}#{array[1]}"
47-
else ; "#{array[0...-1].join(I18n.t('ice_cube.array.words_connector'))}#{I18n.t('ice_cube.array.last_word_connector')}#{array[-1]}"
46+
when 2 ; "#{array[0]}#{IceCube::I18n.t('ice_cube.array.two_words_connector')}#{array[1]}"
47+
else ; "#{array[0...-1].join(IceCube::I18n.t('ice_cube.array.words_connector'))}#{IceCube::I18n.t('ice_cube.array.last_word_connector')}#{array[-1]}"
4848
end
4949
end
5050

@@ -53,18 +53,18 @@ def nice_number(number)
5353
end
5454

5555
def ordinalize(number)
56-
I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number))
56+
IceCube::I18n.t('ice_cube.integer.ordinal', number: number, ordinal: ordinal(number))
5757
end
5858

5959
def literal_ordinal(number)
60-
I18n.t("ice_cube.integer.literal_ordinals")[number]
60+
IceCube::I18n.t("ice_cube.integer.literal_ordinals")[number]
6161
end
6262

6363
def ordinal(number)
64-
ord = I18n.t("ice_cube.integer.ordinals")[number] ||
65-
I18n.t("ice_cube.integer.ordinals")[number % 10] ||
66-
I18n.t('ice_cube.integer.ordinals')[:default]
67-
number >= 0 ? ord : I18n.t("ice_cube.integer.negative", ordinal: ord)
64+
ord = IceCube::I18n.t("ice_cube.integer.ordinals")[number] ||
65+
IceCube::I18n.t("ice_cube.integer.ordinals")[number % 10] ||
66+
IceCube::I18n.t('ice_cube.integer.ordinals')[:default]
67+
number >= 0 ? ord : IceCube::I18n.t("ice_cube.integer.negative", ordinal: ord)
6868
end
6969

7070
end

lib/ice_cube/i18n.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module IceCube
2+
module I18n
3+
def self.t(*args)
4+
backend.t(*args)
5+
end
6+
7+
def self.l(*args)
8+
backend.l(*args)
9+
end
10+
11+
def self.backend
12+
@backend
13+
end
14+
15+
def self.detect_backend!
16+
require 'i18n'
17+
::I18n.load_path += Dir[File.expand_path('../../../config/locales/*{rb,yml}', __FILE__)]
18+
@backend = ::I18n
19+
rescue LoadError
20+
require 'ice_cube/null_i18n'
21+
@backend = NullI18n
22+
end
23+
end
24+
end

lib/ice_cube/null_i18n.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'yaml'
2+
3+
module IceCube
4+
module NullI18n
5+
def self.t(key, options = {})
6+
base = key.to_s.split('.').reduce(config) { |hash, current_key| hash[current_key] }
7+
8+
base = base[options[:count] == 1 ? "one" : "other"] if options[:count]
9+
10+
if base.is_a?(Hash)
11+
return base.each_with_object({}) do |(key, value), hash|
12+
hash[key.is_a?(String) ? key.to_sym : key] = value
13+
end
14+
end
15+
16+
options.reduce(base) { |result, (find, replace)| result.gsub("%{#{find}}", "#{replace}") }
17+
end
18+
19+
def self.l(date_or_time, options = {})
20+
return date_or_time.strftime(options[:format]) if options[:format]
21+
date_or_time.strftime(t('ice_cube.date.formats.default'))
22+
end
23+
24+
def self.config
25+
@config ||= YAML.load(File.read(File.join(File.dirname(__FILE__), '..', '..', 'config', 'locales', 'en.yml')))['en']
26+
end
27+
end
28+
end

lib/ice_cube/schedule.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,14 +313,14 @@ def last(n = nil)
313313
def to_s
314314
pieces = []
315315
rd = recurrence_times_with_start_time - extimes
316-
pieces.concat rd.sort.map { |t| I18n.l(t, format: IceCube.to_s_time_format) }
316+
pieces.concat rd.sort.map { |t| IceCube::I18n.l(t, format: IceCube.to_s_time_format) }
317317
pieces.concat rrules.map { |t| t.to_s }
318-
pieces.concat exrules.map { |t| I18n.t('ice_cube.not', target: t.to_s) }
318+
pieces.concat exrules.map { |t| IceCube::I18n.t('ice_cube.not', target: t.to_s) }
319319
pieces.concat extimes.sort.map { |t|
320-
target = I18n.l(t, format: IceCube.to_s_time_format)
321-
I18n.t('ice_cube.not_on', target: target)
320+
target = IceCube::I18n.l(t, format: IceCube.to_s_time_format)
321+
IceCube::I18n.t('ice_cube.not_on', target: target)
322322
}
323-
pieces.join(I18n.t('ice_cube.pieces_connector'))
323+
pieces.join(IceCube::I18n.t('ice_cube.pieces_connector'))
324324
end
325325

326326
# Serialize this schedule to_ical

lib/ice_cube/validations/count.rb

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

5252
StringBuilder.register_formatter(:count) do |segments|
5353
count = segments.first
54-
I18n.t('ice_cube.times', count: count)
54+
IceCube::I18n.t('ice_cube.times', count: count)
5555
end
5656

5757
end

lib/ice_cube/validations/daily_interval.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def validate(step_time, schedule)
3535
end
3636

3737
def build_s(builder)
38-
builder.base = I18n.t('ice_cube.each_day', count: interval)
38+
builder.base = IceCube::I18n.t('ice_cube.each_day', count: interval)
3939
end
4040

4141
def build_hash(builder)

0 commit comments

Comments
 (0)