Module: Spider::I18n
- Defined in:
- lib/spiderfw/i18n/cldr.rb,
lib/spiderfw/i18n/i18n.rb,
lib/spiderfw/i18n/rails.rb,
lib/spiderfw/i18n/provider.rb,
lib/spiderfw/i18n/shtml_parser.rb,
lib/spiderfw/i18n/javascript_parser.rb
Defined Under Namespace
Modules: JavascriptParser, SHTMLParser
Classes: CLDR, Provider, Rails
Constant Summary
collapse
- DefaultLocale =
'en'
Class Method Summary
collapse
-
.distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) ⇒ Object
-
.do_localize_number(object, delimiter, separator, precision = nil, options = {}) ⇒ Object
-
.do_parse_number(string, delimiter, separator, options = {}) ⇒ Object
-
.init ⇒ Object
-
.load_locale(locale) ⇒ Object
-
.localize(*args) ⇒ Object
-
.localize_date_time(locale, object, format = :default, options = {}) ⇒ Object
-
.localize_number(locale, object, precision = nil, options = {}) ⇒ Object
-
.parse_date(locale, string, format = :default, options = {}) ⇒ Object
-
.parse_datetime(locale, string, format = :default, options = {}) ⇒ Object
-
.parse_dt(locale, string, format = :default, options = {}) ⇒ Object
-
.parse_number(locale, string) ⇒ Object
-
.provider(locale) ⇒ Object
-
.providers ⇒ Object
-
.try_provider(const, locale) ⇒ Object
Class Method Details
.distance_of_time_in_words(from_time, to_time = 0, include_seconds = false) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/spiderfw/i18n/i18n.rb', line 130
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false)
from_time = from_time.to_time if from_time.respond_to?(:to_time)
from_time = from_time.to_local_time if from_time.respond_to?(:to_local_time)
to_time = to_time.to_time if to_time.respond_to?(:to_time)
to_time = to_time.to_local_time if to_time.respond_to?(:to_local_time)
distance_in_minutes = (((to_time - from_time).abs)/60).round
distance_in_seconds = ((to_time - from_time).abs).round
case distance_in_minutes
when 0..1
return (distance_in_minutes == 0) ? _('less than a minute') : _('1 minute') unless include_seconds
case distance_in_seconds
when 0..4 then _('less than %s seconds') % '5'
when 5..9 then _('less than %s seconds') % '10'
when 10..19 then _('less than %s seconds') % '20'
when 20..39 then _('half a minute')
when 40..59 then _('less than a minute')
else _('1 minute')
end
when 2..44 then _("%s minutes") % distance_in_minutes
when 45..89 then _('about 1 hour')
when 90..1439 then _("about %d hours") % (distance_in_minutes.to_f / 60.0).round
when 1440..2879 then _('1 day')
when 2880..43199 then _("%d days") % (distance_in_minutes / 1440).round
when 43200..86399 then _('about 1 month')
when 86400..525599 then _("%d months") % (distance_in_minutes / 43200).round
when 525600..1051199 then _('about 1 year')
else _("over %d years") % (distance_in_minutes / 525600).round
end
end
|
.do_localize_number(object, delimiter, separator, precision = nil, options = {}) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/spiderfw/i18n/i18n.rb', line 73
def do_localize_number(object, delimiter, separator, precision=nil, options={})
options = ({
:use_delimiter => true
}).merge(options)
unless precision
precision = object.class <= Fixnum ? 0 : 2
end
rounded_number = (Float(object) * (10 ** precision)).round.to_f / 10 ** precision
parts = rounded_number.to_s.split('.')
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
return parts[0] if precision == 0
parts[1] += "0"*(precision-parts[1].length)
parts.join(separator)
end
|
.do_parse_number(string, delimiter, separator, options = {}) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/spiderfw/i18n/i18n.rb', line 112
def do_parse_number(string, delimiter, separator, options={})
parts = string.to_s.gsub(delimiter, "").split(separator)
unless options[:return]
options[:return] = parts[1] ? Float : Fixnum
end
if options[:return] <= Fixnum
return parts[0].to_i
elsif options[:return] <= Float
return "#{parts[0]}.#{parts[1]}".to_f
elsif options[:return] <= BigDecimal
return BigDecimal.new("#{parts[0]}.#{parts[1]}")
else
raise ArgumentError, "Don't know how to transform a number to a #{options[:return]}"
end
end
|
15
16
17
|
# File 'lib/spiderfw/i18n/i18n.rb', line 15
def init
@locales = {}
end
|
.load_locale(locale) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/spiderfw/i18n/i18n.rb', line 36
def load_locale(locale)
self.providers.each do |p|
res = try_provider(p, locale)
return res if res
end
return nil
end
|
.localize(*args) ⇒ Object
52
53
54
|
# File 'lib/spiderfw/i18n/i18n.rb', line 52
def localize(*args)
localize_date_time(*args)
end
|
.localize_date_time(locale, object, format = :default, options = {}) ⇒ Object
56
57
58
59
60
61
62
63
|
# File 'lib/spiderfw/i18n/i18n.rb', line 56
def localize_date_time(locale, object, format = :default, options={})
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
p = provider(locale)
unless p
return object.to_s
end
return p.localize_date_time(object, format, options)
end
|
.localize_number(locale, object, precision = nil, options = {}) ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/spiderfw/i18n/i18n.rb', line 65
def localize_number(locale, object, precision=nil, options={})
p = provider(locale)
unless p
return object.to_s
end
return p.localize_number(object, precision, options)
end
|
.parse_date(locale, string, format = :default, options = {}) ⇒ Object
96
97
98
|
# File 'lib/spiderfw/i18n/i18n.rb', line 96
def parse_date(locale, string, format = :default, options = {})
parse_dt(locale, string, format, options.merge({:return => :date}))
end
|
.parse_datetime(locale, string, format = :default, options = {}) ⇒ Object
100
101
102
|
# File 'lib/spiderfw/i18n/i18n.rb', line 100
def parse_datetime(locale, string, format = :default, options = {})
parse_dt(locale, string, format, options.merge({:return => :datetime}))
end
|
.parse_dt(locale, string, format = :default, options = {}) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/spiderfw/i18n/i18n.rb', line 88
def parse_dt(locale, string, format = :default, options={})
p = provider(locale)
unless p
return Date.parse(string)
end
return p.parse_dt(string, format, options)
end
|
.parse_number(locale, string) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/spiderfw/i18n/i18n.rb', line 104
def parse_number(locale, string)
p = provider(locale)
unless p
return nil
end
return p.parse_number(string)
end
|
.provider(locale) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/spiderfw/i18n/i18n.rb', line 23
def provider(locale)
@locales[locale] = load_locale(locale) unless @locales[locale]
Spider::Logger.warn("No provider found for locale #{locale}") unless @locales[locale]
unless @locales[locale]
default = Spider.conf.get('i18n.default_locale')
load_locale(default) unless @locales[default]
return @locales[default]
end
return @locales[locale]
end
|
.providers ⇒ Object
19
20
21
|
# File 'lib/spiderfw/i18n/i18n.rb', line 19
def providers
[:CLDR, :Rails]
end
|
.try_provider(const, locale) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/spiderfw/i18n/i18n.rb', line 44
def try_provider(const, locale)
begin
res = const_get(const).new(locale)
rescue => exc
return nil
end
end
|