Class: MessageFormat::Interpreter
- Inherits:
-
Object
- Object
- MessageFormat::Interpreter
- Defined in:
- lib/message_format/interpreter.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = nil) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #interpret(elements) ⇒ Object
- #interpret_date_time(id, type, style = 'medium') ⇒ Object
- #interpret_element(element, parent = nil) ⇒ Object
- #interpret_number(id, offset, style) ⇒ Object
- #interpret_plural(id, type, offset, children) ⇒ Object
- #interpret_select(id, children) ⇒ Object
- #interpret_simple(id) ⇒ Object
- #interpret_subs(elements, parent = nil) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Interpreter
Returns a new instance of Interpreter.
20 21 22 23 24 25 26 |
# File 'lib/message_format/interpreter.rb', line 20 def initialize ( =nil ) if and .has_key?(:locale) @locale = [:locale] else @locale = TwitterCldr.locale end end |
Class Method Details
.interpret(elements, options = nil) ⇒ Object
160 161 162 |
# File 'lib/message_format/interpreter.rb', line 160 def self.interpret ( elements, =nil ) Interpreter.new().interpret(elements) end |
Instance Method Details
#interpret(elements) ⇒ Object
28 29 30 |
# File 'lib/message_format/interpreter.rb', line 28 def interpret ( elements ) interpret_subs(elements) end |
#interpret_date_time(id, type, style = 'medium') ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/message_format/interpreter.rb', line 102 def interpret_date_time ( id, type, style='medium' ) locale = @locale lambda do |args| datetime = TwitterCldr::Localized::LocalizedDateTime.new(args[id], locale) datetime = type == 'date' ? datetime.to_date : datetime.to_time if style == 'medium' datetime.to_medium_s elsif style == 'long' datetime.to_long_s elsif style == 'short' datetime.to_short_s elsif style == 'full' datetime.to_full_s else datetime.to_additional_s(style) end end end |
#interpret_element(element, parent = nil) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/message_format/interpreter.rb', line 47 def interpret_element ( element, parent=nil ) if element.is_a?(String) return lambda { |_=nil| element } end id, type, style = element offset = 0 if id == '#' id = parent[0] type = 'number' offset = parent[2] || 0 style = nil end id = id.to_sym # actual arguments should always be keyed by symbols case type when 'number' interpret_number(id, offset, style) when 'date', 'time' interpret_date_time(id, type, style) when 'plural', 'selectordinal' offset = element[2] = element[3] interpret_plural(id, type, offset, ) when 'select' interpret_select(id, style) when 'spellout', 'ordinal', 'duration' interpret_number(id, offset, type) else interpret_simple(id) end end |
#interpret_number(id, offset, style) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/message_format/interpreter.rb', line 82 def interpret_number ( id, offset, style ) locale = @locale lambda do |args| number = TwitterCldr::Localized::LocalizedNumber.new(args[id] - offset, locale) if style == 'integer' number.to_decimal.to_s(:precision => 0) elsif style == 'percent' number.to_percent.to_s elsif style == 'currency' number.to_currency.to_s elsif style == 'spellout' number.spellout elsif style == 'ordinal' number.to_rbnf_s('OrdinalRules', 'digits-ordinal') else number.to_s end end end |
#interpret_plural(id, type, offset, children) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/message_format/interpreter.rb', line 121 def interpret_plural ( id, type, offset, children ) parent = [ id, type, offset ] = {} children.each do |key, value| [key.to_sym] = interpret_subs(value, parent) end locale = @locale plural_type = type == 'selectordinal' ? :ordinal : :cardinal lambda do |args| arg = args[id] exactSelector = ('=' + arg.to_s).to_sym keywordSelector = TwitterCldr::Formatters::Plurals::Rules.rule_for(arg - offset, locale, plural_type) func = [exactSelector] || [keywordSelector] || [:other] func.call(args) end end |
#interpret_select(id, children) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/message_format/interpreter.rb', line 142 def interpret_select ( id, children ) = {} children.each do |key, value| [key.to_sym] = interpret_subs(value, nil) end lambda do |args| selector = args[id].to_sym func = [selector] || [:other] func.call(args) end end |
#interpret_simple(id) ⇒ Object
156 157 158 |
# File 'lib/message_format/interpreter.rb', line 156 def interpret_simple ( id ) lambda { |args| args[id].to_s } end |
#interpret_subs(elements, parent = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/message_format/interpreter.rb', line 32 def interpret_subs ( elements, parent=nil ) elements = elements.map do |element| interpret_element(element, parent) end # optimize common case if elements.length == 1 return elements[0] end lambda do |args| elements.map { |element| element.call(args) }.join '' end end |