Class: Spider::I18n::CLDR
- Defined in:
- lib/spiderfw/i18n/cldr.rb
Overview
Formats: short, medium, full, long
Instance Method Summary collapse
- #day_names(format = :wide, calendar = self.default_calendar) ⇒ Object
-
#initialize(locale) ⇒ CLDR
constructor
A new instance of CLDR.
- #list(enumerable) ⇒ Object
- #localize_date_time(object, format = 'medium', options = {}) ⇒ Object
- #localize_number(object, precision = nil, options = {}) ⇒ Object
- #mgsub(string, key_value_pairs) ⇒ Object
- #month_names(format = :wide, calendar = self.default_calendar) ⇒ Object
-
#parse_dt(string, format = 'medium', options = {}) ⇒ Object
FIXME: add extended format handling like in localize.
- #parse_number(string, options = {}) ⇒ Object
- #prepare_format_string(obj, string) ⇒ Object
- #sub_pattern(pattern, items) ⇒ Object
- #week_start(calendar = self.default_calendar) ⇒ Object
- #weekend_end(calendar = self.default_calendar) ⇒ Object
- #weekend_start(calendar = self.default_calendar) ⇒ Object
Methods inherited from Provider
Constructor Details
#initialize(locale) ⇒ CLDR
Returns a new instance of CLDR.
10 11 12 13 14 |
# File 'lib/spiderfw/i18n/cldr.rb', line 10 def initialize(locale) @locale = locale @cldr = ::CLDR::Object.new(:locale => locale.to_cldr) end |
Instance Method Details
#day_names(format = :wide, calendar = self.default_calendar) ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/spiderfw/i18n/cldr.rb', line 118 def day_names(format = :wide, calendar = self.default_calendar) begin days = @cldr.calendar.days[calendar][format] return [days['sun'], days['mon'], days['tue'], days['wed'], days['thu'], days['fri'], days['sat']] rescue NoMethodError raise ArgumentError, "Calendar #{calendar} not found" unless @cldr.days[calendar] raise ArgumentError, "Format #{format} not found" end end |
#list(enumerable) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/spiderfw/i18n/cldr.rb', line 188 def list(enumerable) return enumerable.join(', ') unless @cldr.core.respond_to?(:list_patterns) # old cldr version patterns = @cldr.core.list_patterns str = "" def sub_pattern(pattern, items) str = pattern.clone items.each_index do |i| str.sub!("{#{i}}", items[i]) end str end if pattern = patterns[enumerable.length.to_s] return sub_pattern(pattern, enumerable) end length = enumerable.length str = enumerable.last.to_s (length-2).downto(0) do |i| pattern = nil if i == length -2 pattern = patterns['end'] elsif i == 0 pattern = patterns['start'] end pattern ||= patterns['middle'] str = sub_pattern(pattern, [enumerable[i].to_s, str]) end return str end |
#localize_date_time(object, format = 'medium', options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/spiderfw/i18n/cldr.rb', line 16 def localize_date_time(object, format = 'medium', ={}) [:calendar] ||= 'gregorian' format = 'medium' if format == :default time_format = nil date_format = nil format_string = nil calendar = [:calendar].to_sym if (object.respond_to?(:sec) && ![:no_time]) time_format = @cldr.calendar.timeformats[calendar][format.to_s].dup end if (object.is_a?(Date)) date_format = @cldr.calendar.dateformats[calendar][format.to_s].dup end if (date_format && time_format) # in CLDR 1.x, datetimeformats is an hash of strings indexed by strings; # in CLDR 2.x, it is a hash of hashes indexed by symbols fts = @cldr.calendar.datetimeformats[calendar] || @cldr.calendar.datetimeformats[calendar.to_s] dt_f = fts.is_a?(String) ? fts : fts[format.to_s] format_string = dt_f.sub('{1}', date_format).sub('{0}', time_format) else format_string = date_format ? date_format : time_format end # FIXME: handle more efficiently d = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] obj_d = d[object.wday] days = @cldr.calendar.days[[:calendar].to_sym] months = @cldr.calendar.months[[:calendar].to_sym] replacements = [ [/y{1,4}/, '%Y'], # year don't use two digits year, they cause confusion [/y{1,2}/, '%y'] [/M{5}/, months[:narrow][object.month.to_s]], [/M{4}/, months[:wide][object.month.to_s]], #month [/M{3}/, months[:abbreviated][object.month.to_s]], [/M{1,2}/, '%m'], [/L{5}/, months[:narrow][object.month.to_s]], [/L{4}/, months[:wide][object.month.to_s]], #month [/L{3}/, months[:abbreviated][object.month.to_s]], [/L{1,2}/, '%m'], [/E{5}/, days[:narrow][obj_d]], [/E{4}/, days[:wide][obj_d]], [/E{1,3}/, days[:abbreviated][obj_d]], #day of the week [/e{1,5}/, '%w'], #day of the week (numeric) [/d{1,2}/, '%d'], # day of the month [/h{1,2}/, '%I'], [/H{1,2}/, '%H'], [/a/, '%p'], #hour [/m{1,2}/, '%M'], [/s{1,2}/, '%S'], # seconds [/z{1,4}/, '%Z'], [/Z{1,4}/, '%Z'], [/V{1,4}/, '%Z'] # time zone ] format_string = mgsub(format_string, replacements) if (time_format) am = @cldr.calendar.am[[:calendar].to_s] pm = @cldr.calendar.pm[[:calendar].to_s] format_string.gsub!(/%p/, object.hour < 12 ? 'am' : 'pm') if object.respond_to? :hour end object.strftime(format_string) end |
#localize_number(object, precision = nil, options = {}) ⇒ Object
176 177 178 179 180 |
# File 'lib/spiderfw/i18n/cldr.rb', line 176 def localize_number(object, precision=nil, ={}) delimiter = @cldr.number.symbol_group separator = @cldr.number.symbol_decimal Spider::I18n.do_localize_number(object, delimiter, separator, precision, ) end |
#mgsub(string, key_value_pairs) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/spiderfw/i18n/cldr.rb', line 110 def mgsub(string, key_value_pairs) regexp_fragments = key_value_pairs.collect { |k,v| k } string.gsub( Regexp.union(*regexp_fragments)) do |match| key_value_pairs.detect{|k,v| k =~ match}[1] end end |
#month_names(format = :wide, calendar = self.default_calendar) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/spiderfw/i18n/cldr.rb', line 129 def month_names(format = :wide, calendar = self.default_calendar) months = [] begin @cldr.calendar.months[calendar][format].each do |k, v| months[k.to_i] = v end rescue NoMethodError raise ArgumentError, "Calendar #{calendar} not found" unless @cldr.months[calendar] raise ArgumentError, "Format #{format} not found" end months end |
#parse_dt(string, format = 'medium', options = {}) ⇒ Object
FIXME: add extended format handling like in localize
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/spiderfw/i18n/cldr.rb', line 69 def parse_dt(string, format = 'medium', = {}) format = 'medium' if format == :default [:calendar] ||= 'gregorian' time_format = @cldr.calendar.timeformats[[:calendar].to_sym][format.to_s].dup date_format = @cldr.calendar.dateformats[[:calendar].to_sym][format.to_s].dup if ([:return] == :datetime) dt_f = @cldr.calendar.datetimeformats[[:calendar].to_sym][format.to_s] format_string = dt_f.sub('{1}', date_format).sub('{0}', time_format) klass = DateTime elsif ([:return] == :date) format_string = date_format klass = Date elsif ([:return] == :time) format_string = time_format klass = Time end replacements = [ [/y{1,4}/, '%Y'], # year don't use two digits year [/y{1,2}/, '%y'], [/M{1,5}/, '%m'], [/L{1,5}/, '%m'], [/E{1,5}/, ''], #day of the week [/e{1,5}/, '%w'], #day of the week (numeric) [/d{1,2}/, '%d'], # day of the month [/h{1,2}/, '%I'], [/H{1,2}/, '%H'], [/a/, '%p'], #hour [/m{1,2}/, '%M'], [/s{1,2}/, '%S'], # seconds [/z{1,4}/, '%Z'], [/Z{1,4}/, '%Z'], [/V{1,4}/, '%Z'] # time zone ] format_string = mgsub(format_string, replacements) if [:return] == :time DateTime.strptime("01-01-2000T#{string}#{Time.now.strftime('%Z')}", "%d-%m-%YT#{format_string}%Z").to_local_time else klass.strptime(string, format_string) end end |
#parse_number(string, options = {}) ⇒ Object
182 183 184 185 186 |
# File 'lib/spiderfw/i18n/cldr.rb', line 182 def parse_number(string, ={}) delimiter = @cldr.number.symbol_group separator = @cldr.number.symbol_decimal Spider::I18n.do_parse_number(string, delimiter, separator, ) end |
#prepare_format_string(obj, string) ⇒ Object
106 107 108 |
# File 'lib/spiderfw/i18n/cldr.rb', line 106 def prepare_format_string(obj, string) end |
#sub_pattern(pattern, items) ⇒ Object
193 194 195 196 197 198 199 |
# File 'lib/spiderfw/i18n/cldr.rb', line 193 def sub_pattern(pattern, items) str = pattern.clone items.each_index do |i| str.sub!("{#{i}}", items[i]) end str end |
#week_start(calendar = self.default_calendar) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/spiderfw/i18n/cldr.rb', line 142 def week_start(calendar = self.default_calendar) wdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] day = if @cldr.respond_to?(:supplemental) @cldr.supplemental.week_data["firstDay"] elsif @cldr.calendar.respond_to?(:week_firstdays) # CLDR 1 @cldr.calendar.week_firstdays[calendar.to_s] end day ||= 'mon' wdays.index day end |
#weekend_end(calendar = self.default_calendar) ⇒ Object
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/spiderfw/i18n/cldr.rb', line 165 def weekend_end(calendar = self.default_calendar) wdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] day = if @cldr.respond_to?(:supplemental) @cldr.supplemental.week_data["weekendEnd"] elsif @cldr.calendar.respond_to?(:weekend_ends) # CLDR 1 @cldr.calendar.weekend_ends[calendar.to_s] end day ||= 'sun' wdays.index day end |
#weekend_start(calendar = self.default_calendar) ⇒ Object
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/spiderfw/i18n/cldr.rb', line 154 def weekend_start(calendar = self.default_calendar) wdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] day = if @cldr.respond_to?(:supplemental) @cldr.supplemental.week_data["weekendStart"] elsif @cldr.calendar.respond_to?(:weekend_starts) # CLDR 1 @cldr.calendar.weekend_starts[calendar.to_s] end day ||= 'sat' wdays.index day end |