Skip to content

Add raise_on_missing_params option to interpreter #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/message_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
module MessageFormat
class MessageFormat

def initialize ( pattern, locale=nil )
def initialize ( pattern, locale=nil, raise_on_missing_params: false )
@locale = (locale || TwitterCldr.locale).to_sym
@format = Interpreter.interpret(
Parser.parse(pattern),
{ :locale => @locale }
{
:locale => @locale,
:raise_on_missing_params => raise_on_missing_params,
},
)
end

Expand All @@ -22,15 +25,18 @@ def format ( args=nil )

class << self

def new ( pattern, locale=nil )
MessageFormat.new(pattern, locale)
def new ( pattern, locale=nil, raise_on_missing_params: false )
MessageFormat.new(pattern, locale, raise_on_missing_params)
end

def format_message ( pattern, args=nil, locale=nil )
def format_message ( pattern, args=nil, locale=nil, raise_on_missing_params: false )
locale ||= TwitterCldr.locale
Interpreter.interpret(
Parser.parse(pattern),
{ :locale => locale.to_sym }
{
:locale => locale.to_sym,
:raise_on_missing_params => raise_on_missing_params,
}
).call(args)
end

Expand Down
34 changes: 33 additions & 1 deletion lib/message_format/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,35 @@ def initialize ( options=nil )
else
@locale = TwitterCldr.locale
end
@raise_on_missing_params = options[:raise_on_missing_params]
end

#
# MissingParametersError
# Holds information about parameters that were accessed during interpretation but were not
# provided. Only raised if the `raise_on_missing_params` option is set to `true`.
#
# Example:
# message = MessageFormat.new('Hello { place } and { player }!', 'en-US', raise_on_missing_params: true)
# formatted = message.format({ :place => 'World' }) # raises with "player" identified as a missing parameter
#
# Note that only parameters that were actually accessed during interpretation will be reported.
#
class MissingParametersError < StandardError
attr_reader :missing_params

def initialize ( message, missing_params )
super(message)
@missing_params = missing_params
end
end

def interpret ( elements )
@missing_ids = []
interpret_subs(elements)
if @raise_on_missing_params && !@missing_ids.empty?
raise MissingParametersError.new('Missing parameters detected during interpretation', @missing_ids.compact)
end
end

def interpret_subs ( elements, parent=nil )
Expand Down Expand Up @@ -82,6 +107,7 @@ def interpret_element ( element, parent=nil )
def interpret_number ( id, offset, style )
locale = @locale
lambda do |args|
@missing_ids.push(id) unless args.key?(id)
number = TwitterCldr::Localized::LocalizedNumber.new(args[id] - offset, locale)
if style == 'integer'
number.to_decimal.to_s(:precision => 0)
Expand All @@ -102,6 +128,7 @@ def interpret_number ( id, offset, style )
def interpret_date_time ( id, type, style='medium' )
locale = @locale
lambda do |args|
@missing_ids.push(id) unless args.key?(id)
datetime = TwitterCldr::Localized::LocalizedDateTime.new(args[id], locale)
datetime = type == 'date' ? datetime.to_date : datetime.to_time
if style == 'medium'
Expand All @@ -128,6 +155,7 @@ def interpret_plural ( id, type, offset, children )
locale = @locale
plural_type = type == 'selectordinal' ? :ordinal : :cardinal
lambda do |args|
@missing_ids.push(id) unless args.key?(id)
arg = args[id]
exactSelector = ('=' + arg.to_s).to_sym
keywordSelector = TwitterCldr::Formatters::Plurals::Rules.rule_for(arg - offset, locale, plural_type)
Expand All @@ -145,6 +173,7 @@ def interpret_select ( id, children )
options[key.to_sym] = interpret_subs(value, nil)
end
lambda do |args|
@missing_ids.push(id) unless args.key?(id)
selector = args[id].to_sym
func =
options[selector] ||
Expand All @@ -154,7 +183,10 @@ def interpret_select ( id, children )
end

def interpret_simple ( id )
lambda { |args| args[id].to_s }
lambda do |args|
@missing_ids.push(id) unless args.key?(id)
args[id].to_s
end
end

def self.interpret ( elements, options=nil )
Expand Down