@@ -23,10 +23,35 @@ def initialize ( options=nil )
23
23
else
24
24
@locale = TwitterCldr . locale
25
25
end
26
+ @raise_on_missing_params = options [ :raise_on_missing_params ]
27
+ end
28
+
29
+ #
30
+ # MissingParametersError
31
+ # Holds information about parameters that were accessed during interpretation but were not
32
+ # provided. Only raised if the `raise_on_missing_params` option is set to `true`.
33
+ #
34
+ # Example:
35
+ # message = MessageFormat.new('Hello { place } and { player }!', 'en-US', raise_on_missing_params: true)
36
+ # formatted = message.format({ :place => 'World' }) # raises with "player" identified as a missing parameter
37
+ #
38
+ # Note that only parameters that were actually accessed during interpretation will be reported.
39
+ #
40
+ class MissingParametersError < StandardError
41
+ attr_reader :missing_params
42
+
43
+ def initialize ( message , missing_params )
44
+ super ( message )
45
+ @missing_params = missing_params
46
+ end
26
47
end
27
48
28
49
def interpret ( elements )
50
+ @missing_ids = [ ]
29
51
interpret_subs ( elements )
52
+ if @raise_on_missing_params && !@missing_ids . empty?
53
+ raise MissingParametersError . new ( 'Missing parameters detected during interpretation' , @missing_ids . compact )
54
+ end
30
55
end
31
56
32
57
def interpret_subs ( elements , parent = nil )
@@ -82,6 +107,7 @@ def interpret_element ( element, parent=nil )
82
107
def interpret_number ( id , offset , style )
83
108
locale = @locale
84
109
lambda do |args |
110
+ @missing_ids . push ( id ) unless args . key? ( id )
85
111
number = TwitterCldr ::Localized ::LocalizedNumber . new ( args [ id ] - offset , locale )
86
112
if style == 'integer'
87
113
number . to_decimal . to_s ( :precision => 0 )
@@ -102,6 +128,7 @@ def interpret_number ( id, offset, style )
102
128
def interpret_date_time ( id , type , style = 'medium' )
103
129
locale = @locale
104
130
lambda do |args |
131
+ @missing_ids . push ( id ) unless args . key? ( id )
105
132
datetime = TwitterCldr ::Localized ::LocalizedDateTime . new ( args [ id ] , locale )
106
133
datetime = type == 'date' ? datetime . to_date : datetime . to_time
107
134
if style == 'medium'
@@ -128,6 +155,7 @@ def interpret_plural ( id, type, offset, children )
128
155
locale = @locale
129
156
plural_type = type == 'selectordinal' ? :ordinal : :cardinal
130
157
lambda do |args |
158
+ @missing_ids . push ( id ) unless args . key? ( id )
131
159
arg = args [ id ]
132
160
exactSelector = ( '=' + arg . to_s ) . to_sym
133
161
keywordSelector = TwitterCldr ::Formatters ::Plurals ::Rules . rule_for ( arg - offset , locale , plural_type )
@@ -145,6 +173,7 @@ def interpret_select ( id, children )
145
173
options [ key . to_sym ] = interpret_subs ( value , nil )
146
174
end
147
175
lambda do |args |
176
+ @missing_ids . push ( id ) unless args . key? ( id )
148
177
selector = args [ id ] . to_sym
149
178
func =
150
179
options [ selector ] ||
@@ -154,7 +183,10 @@ def interpret_select ( id, children )
154
183
end
155
184
156
185
def interpret_simple ( id )
157
- lambda { |args | args [ id ] . to_s }
186
+ lambda do |args |
187
+ @missing_ids . push ( id ) unless args . key? ( id )
188
+ args [ id ] . to_s
189
+ end
158
190
end
159
191
160
192
def self . interpret ( elements , options = nil )
0 commit comments