Class: Spider::I18n::CLDR

Inherits:
Provider show all
Defined in:
lib/spiderfw/i18n/cldr.rb

Overview

Formats: short, medium, full, long

Instance Method Summary collapse

Methods inherited from Provider

#default_calendar, #localize

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/spiderfw/i18n/cldr.rb', line 119

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

#localize_date_time(object, format = :default, 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
# File 'lib/spiderfw/i18n/cldr.rb', line 16

def localize_date_time(object, format = :default, options={})
    options[:calendar] ||= 'gregorian'
    
    if (format == :default)
        format = @cldr.calendar.dateformat_defaults[options[:calendar]]
    end
    
    time_format = nil
    date_format = nil
    format_string = nil
    if (object.respond_to?(:sec) && !options[:no_time])
        time_format = @cldr.calendar.timeformats[options[:calendar].to_sym][format.to_s].dup
    end
    if (object.is_a?(Date))
        date_format = @cldr.calendar.dateformats[options[:calendar].to_sym][format.to_s].dup
    end
    if (date_format && time_format)
        dt_f = @cldr.calendar.datetimeformats[options[:calendar].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[options[:calendar].to_sym]
    months = @cldr.calendar.months[options[: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[options[:calendar].to_s]
        pm = @cldr.calendar.pm[options[: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



158
159
160
161
162
# File 'lib/spiderfw/i18n/cldr.rb', line 158

def localize_number(object, precision=nil, options={})
    delimiter = @cldr.number.symbol_group
    separator = @cldr.number.symbol_decimal
    Spider::I18n.do_localize_number(object, delimiter, separator, precision, options)
end

#mgsub(string, key_value_pairs) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/spiderfw/i18n/cldr.rb', line 111

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



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/spiderfw/i18n/cldr.rb', line 130

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 = :default, options = {}) ⇒ Object

FIXME: add extended format handling like in localize



68
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
104
# File 'lib/spiderfw/i18n/cldr.rb', line 68

def parse_dt(string, format = :default, options = {})
    options[:calendar] ||= 'gregorian'
    
    if (format == :default)
        format = @cldr.calendar.dateformat_defaults[options[:calendar]]
    end
    time_format = @cldr.calendar.timeformats[options[:calendar].to_sym][format.to_s].dup
    date_format = @cldr.calendar.dateformats[options[:calendar].to_sym][format.to_s].dup
    if (options[:return] == :datetime)
        dt_f = @cldr.calendar.datetimeformats[options[:calendar].to_s]
        format_string = dt_f.sub('{1}', date_format).sub('{0}', time_format)
        klass = DateTime
    elsif (options[:return] == :date)
        format_string = date_format
        klass = Date
    elsif (options[: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 options[: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



164
165
166
167
168
# File 'lib/spiderfw/i18n/cldr.rb', line 164

def parse_number(string, options={})
    delimiter = @cldr.number.symbol_group
    separator = @cldr.number.symbol_decimal
    Spider::I18n.do_parse_number(string, delimiter, separator, options)
end

#prepare_format_string(obj, string) ⇒ Object



107
108
109
# File 'lib/spiderfw/i18n/cldr.rb', line 107

def prepare_format_string(obj, string)
 
end

#week_start(calendar = self.default_calendar) ⇒ Object



143
144
145
146
# File 'lib/spiderfw/i18n/cldr.rb', line 143

def week_start(calendar = self.default_calendar)
    wdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
    wdays.index @cldr.calendar.week_firstdays[calendar.to_s]
end

#weekend_end(calendar = self.default_calendar) ⇒ Object



153
154
155
156
# File 'lib/spiderfw/i18n/cldr.rb', line 153

def weekend_end(calendar = self.default_calendar)
    wdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
    wdays.index @cldr.calendar.weekend_ends[calendar.to_s]
end

#weekend_start(calendar = self.default_calendar) ⇒ Object



148
149
150
151
# File 'lib/spiderfw/i18n/cldr.rb', line 148

def weekend_start(calendar = self.default_calendar)
    wdays = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']
    wdays.index @cldr.calendar.weekend_starts[calendar.to_s]
end